Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using JetBrains.Rider.Unity.Editor;
  6. using UnityEditor;
  7. using UnityEngine;
  8.  
  9. namespace Editor
  10. {
  11. public class CsprojAssetPostprocessor : AssetPostprocessor
  12. {
  13. internal static string[] GetCsprojLinesInSln()
  14. {
  15. var projectDirectory = Directory.GetParent(Application.dataPath).FullName;
  16.  
  17. var projectName = Path.GetFileName(projectDirectory);
  18. var slnFile = Path.GetFullPath(string.Format("{0}.sln", projectName));
  19. if (!File.Exists(slnFile))
  20. return new string[0];
  21.  
  22. var slnAllText = File.ReadAllText(slnFile);
  23. var lines = slnAllText.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  24. .Where(a => a.StartsWith("Project(")).ToArray();
  25. return lines;
  26. }
  27.  
  28. public static void OnGeneratedCSProjectFiles()
  29. {
  30. if (!PluginEntryPoint.Enabled)
  31. return;
  32.  
  33. try
  34. {
  35. // get only csproj files, which are mentioned in sln
  36. var lines = GetCsprojLinesInSln();
  37. var currentDirectory = Directory.GetCurrentDirectory();
  38. var projectFiles = Directory.GetFiles(currentDirectory, "*.csproj")
  39. .Where(csprojFile => lines.Any(line => line.Contains("\"" + Path.GetFileName(csprojFile) + "\""))).ToArray();
  40.  
  41. foreach (var file in projectFiles)
  42. {
  43. UpgradeProjectFile(file);
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. // unhandled exception kills editor
  49. Debug.LogError(e);
  50. }
  51. }
  52.  
  53. private static void UpgradeProjectFile(string projectFile)
  54. {
  55. XDocument doc;
  56. try
  57. {
  58. doc = XDocument.Load(projectFile);
  59. }
  60. catch (Exception)
  61. {
  62. return;
  63. }
  64.  
  65. var projectContentElement = doc.Root;
  66. XNamespace xmlns = projectContentElement.Name.NamespaceName; // do not use var
  67.  
  68. SetBooDllReference(projectContentElement, xmlns);
  69.  
  70. doc.Save(projectFile);
  71. }
  72.  
  73. private static void SetBooDllReference(XElement projectContentElement, XNamespace xmlns)
  74. {
  75. var unityAppBaseFolder = Path.GetDirectoryName(EditorApplication.applicationPath);
  76. if (string.IsNullOrEmpty(unityAppBaseFolder))
  77. {
  78. return;
  79. }
  80.  
  81. var name = "Boo.Lang.dll";
  82. var booDllPath = Path.Combine(unityAppBaseFolder, Path.Combine("Data/MonoBleedingEdge/lib/mono/2.0-api", name));
  83.  
  84. if (!File.Exists(booDllPath))
  85. return;
  86.  
  87. var itemGroup = new XElement(xmlns + "ItemGroup");
  88. var reference = new XElement(xmlns + "Reference");
  89. reference.Add(new XAttribute("Include", Path.GetFileNameWithoutExtension(booDllPath)));
  90. reference.Add(new XElement(xmlns + "HintPath", booDllPath));
  91. itemGroup.Add(reference);
  92. projectContentElement.Add(itemGroup);
  93. }
  94.  
  95. private static int ourScriptingRuntimeCached = -1;
  96. private static string SlnFile;
  97. }
  98. }
Add Comment
Please, Sign In to add comment