ProjectUtils.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using GodotTools.Core;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using DotNet.Globbing;
  7. using Microsoft.Build.Construction;
  8. namespace GodotTools.ProjectEditor
  9. {
  10. public static class ProjectUtils
  11. {
  12. public static void AddItemToProjectChecked(string projectPath, string itemType, string include)
  13. {
  14. var dir = Directory.GetParent(projectPath).FullName;
  15. var root = ProjectRootElement.Open(projectPath);
  16. Debug.Assert(root != null);
  17. var normalizedInclude = include.RelativeToPath(dir).Replace("/", "\\");
  18. if (root.AddItemChecked(itemType, normalizedInclude))
  19. root.Save();
  20. }
  21. private static string[] GetAllFilesRecursive(string rootDirectory, string mask)
  22. {
  23. string[] files = Directory.GetFiles(rootDirectory, mask, SearchOption.AllDirectories);
  24. // We want relative paths
  25. for (int i = 0; i < files.Length; i++)
  26. {
  27. files[i] = files[i].RelativeToPath(rootDirectory);
  28. }
  29. return files;
  30. }
  31. public static string[] GetIncludeFiles(string projectPath, string itemType)
  32. {
  33. var result = new List<string>();
  34. var existingFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
  35. var globOptions = new GlobOptions();
  36. globOptions.Evaluation.CaseInsensitive = false;
  37. var root = ProjectRootElement.Open(projectPath);
  38. foreach (var itemGroup in root.ItemGroups)
  39. {
  40. if (itemGroup.Condition.Length != 0)
  41. continue;
  42. foreach (var item in itemGroup.Items)
  43. {
  44. if (item.ItemType != itemType)
  45. continue;
  46. string normalizedInclude = item.Include.NormalizePath();
  47. var glob = Glob.Parse(normalizedInclude, globOptions);
  48. // TODO Check somehow if path has no blob to avoid the following loop...
  49. foreach (var existingFile in existingFiles)
  50. {
  51. if (glob.IsMatch(existingFile))
  52. {
  53. result.Add(existingFile);
  54. }
  55. }
  56. }
  57. }
  58. return result.ToArray();
  59. }
  60. /// Simple function to make sure the Api assembly references are configured correctly
  61. public static void FixApiHintPath(string projectPath)
  62. {
  63. var root = ProjectRootElement.Open(projectPath);
  64. Debug.Assert(root != null);
  65. bool dirty = false;
  66. void AddPropertyIfNotPresent(string name, string condition, string value)
  67. {
  68. if (root.PropertyGroups
  69. .Any(g => g.Condition == string.Empty || g.Condition == condition &&
  70. g.Properties
  71. .Any(p => p.Name == name &&
  72. p.Value == value &&
  73. (p.Condition == condition || g.Condition == condition))))
  74. {
  75. return;
  76. }
  77. root.AddProperty(name, value).Condition = condition;
  78. dirty = true;
  79. }
  80. AddPropertyIfNotPresent(name: "ApiConfiguration",
  81. condition: " '$(Configuration)' != 'Release' ",
  82. value: "Debug");
  83. AddPropertyIfNotPresent(name: "ApiConfiguration",
  84. condition: " '$(Configuration)' == 'Release' ",
  85. value: "Release");
  86. void SetReferenceHintPath(string referenceName, string condition, string hintPath)
  87. {
  88. foreach (var itemGroup in root.ItemGroups.Where(g =>
  89. g.Condition == string.Empty || g.Condition == condition))
  90. {
  91. var references = itemGroup.Items.Where(item =>
  92. item.ItemType == "Reference" &&
  93. item.Include == referenceName &&
  94. (item.Condition == condition || itemGroup.Condition == condition));
  95. var referencesWithHintPath = references.Where(reference =>
  96. reference.Metadata.Any(m => m.Name == "HintPath"));
  97. if (referencesWithHintPath.Any(reference => reference.Metadata
  98. .Any(m => m.Name == "HintPath" && m.Value == hintPath)))
  99. {
  100. // Found a Reference item with the right HintPath
  101. return;
  102. }
  103. var referenceWithHintPath = referencesWithHintPath.FirstOrDefault();
  104. if (referenceWithHintPath != null)
  105. {
  106. // Found a Reference item with a wrong HintPath
  107. foreach (var metadata in referenceWithHintPath.Metadata.ToList()
  108. .Where(m => m.Name == "HintPath"))
  109. {
  110. // Safe to remove as we duplicate with ToList() to loop
  111. referenceWithHintPath.RemoveChild(metadata);
  112. }
  113. referenceWithHintPath.AddMetadata("HintPath", hintPath);
  114. dirty = true;
  115. return;
  116. }
  117. var referenceWithoutHintPath = references.FirstOrDefault();
  118. if (referenceWithoutHintPath != null)
  119. {
  120. // Found a Reference item without a HintPath
  121. referenceWithoutHintPath.AddMetadata("HintPath", hintPath);
  122. dirty = true;
  123. return;
  124. }
  125. }
  126. // Found no Reference item at all. Add it.
  127. root.AddItem("Reference", referenceName).Condition = condition;
  128. dirty = true;
  129. }
  130. const string coreProjectName = "GodotSharp";
  131. const string editorProjectName = "GodotSharpEditor";
  132. const string coreCondition = "";
  133. const string editorCondition = " '$(Configuration)' == 'Tools' ";
  134. var coreHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{coreProjectName}.dll";
  135. var editorHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{editorProjectName}.dll";
  136. SetReferenceHintPath(coreProjectName, coreCondition, coreHintPath);
  137. SetReferenceHintPath(editorProjectName, editorCondition, editorHintPath);
  138. if (dirty)
  139. root.Save();
  140. }
  141. }
  142. }