ProjectUtils.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. public static void RenameItemInProjectChecked(string projectPath, string itemType, string oldInclude, string newInclude)
  22. {
  23. var dir = Directory.GetParent(projectPath).FullName;
  24. var root = ProjectRootElement.Open(projectPath);
  25. Debug.Assert(root != null);
  26. var normalizedOldInclude = oldInclude.NormalizePath();
  27. var normalizedNewInclude = newInclude.NormalizePath();
  28. var item = root.FindItemOrNullAbs(itemType, normalizedOldInclude);
  29. if (item == null)
  30. return;
  31. item.Include = normalizedNewInclude.RelativeToPath(dir).Replace("/", "\\");
  32. root.Save();
  33. }
  34. public static void RemoveItemFromProjectChecked(string projectPath, string itemType, string include)
  35. {
  36. var dir = Directory.GetParent(projectPath).FullName;
  37. var root = ProjectRootElement.Open(projectPath);
  38. Debug.Assert(root != null);
  39. var normalizedInclude = include.NormalizePath();
  40. if (root.RemoveItemChecked(itemType, normalizedInclude))
  41. root.Save();
  42. }
  43. public static void RenameItemsToNewFolderInProjectChecked(string projectPath, string itemType, string oldFolder, string newFolder)
  44. {
  45. var dir = Directory.GetParent(projectPath).FullName;
  46. var root = ProjectRootElement.Open(projectPath);
  47. Debug.Assert(root != null);
  48. bool dirty = false;
  49. var oldFolderNormalized = oldFolder.NormalizePath();
  50. var newFolderNormalized = newFolder.NormalizePath();
  51. string absOldFolderNormalized = Path.GetFullPath(oldFolderNormalized).NormalizePath();
  52. string absNewFolderNormalized = Path.GetFullPath(newFolderNormalized).NormalizePath();
  53. foreach (var item in root.FindAllItemsInFolder(itemType, oldFolderNormalized))
  54. {
  55. string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath();
  56. string absNewIncludeNormalized = absNewFolderNormalized + absPathNormalized.Substring(absOldFolderNormalized.Length);
  57. item.Include = absNewIncludeNormalized.RelativeToPath(dir).Replace("/", "\\");
  58. dirty = true;
  59. }
  60. if (dirty)
  61. root.Save();
  62. }
  63. public static void RemoveItemsInFolderFromProjectChecked(string projectPath, string itemType, string folder)
  64. {
  65. var root = ProjectRootElement.Open(projectPath);
  66. Debug.Assert(root != null);
  67. var folderNormalized = folder.NormalizePath();
  68. var itemsToRemove = root.FindAllItemsInFolder(itemType, folderNormalized).ToList();
  69. if (itemsToRemove.Count > 0)
  70. {
  71. foreach (var item in itemsToRemove)
  72. item.Parent.RemoveChild(item);
  73. root.Save();
  74. }
  75. }
  76. private static string[] GetAllFilesRecursive(string rootDirectory, string mask)
  77. {
  78. string[] files = Directory.GetFiles(rootDirectory, mask, SearchOption.AllDirectories);
  79. // We want relative paths
  80. for (int i = 0; i < files.Length; i++)
  81. {
  82. files[i] = files[i].RelativeToPath(rootDirectory);
  83. }
  84. return files;
  85. }
  86. public static string[] GetIncludeFiles(string projectPath, string itemType)
  87. {
  88. var result = new List<string>();
  89. var existingFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
  90. var globOptions = new GlobOptions();
  91. globOptions.Evaluation.CaseInsensitive = false;
  92. var root = ProjectRootElement.Open(projectPath);
  93. foreach (var itemGroup in root.ItemGroups)
  94. {
  95. if (itemGroup.Condition.Length != 0)
  96. continue;
  97. foreach (var item in itemGroup.Items)
  98. {
  99. if (item.ItemType != itemType)
  100. continue;
  101. string normalizedInclude = item.Include.NormalizePath();
  102. var glob = Glob.Parse(normalizedInclude, globOptions);
  103. // TODO Check somehow if path has no blob to avoid the following loop...
  104. foreach (var existingFile in existingFiles)
  105. {
  106. if (glob.IsMatch(existingFile))
  107. {
  108. result.Add(existingFile);
  109. }
  110. }
  111. }
  112. }
  113. return result.ToArray();
  114. }
  115. /// Simple function to make sure the Api assembly references are configured correctly
  116. public static void FixApiHintPath(string projectPath)
  117. {
  118. var root = ProjectRootElement.Open(projectPath);
  119. Debug.Assert(root != null);
  120. bool dirty = false;
  121. void AddPropertyIfNotPresent(string name, string condition, string value)
  122. {
  123. if (root.PropertyGroups
  124. .Any(g => (g.Condition == string.Empty || g.Condition == condition) &&
  125. g.Properties
  126. .Any(p => p.Name == name &&
  127. p.Value == value &&
  128. (p.Condition == condition || g.Condition == condition))))
  129. {
  130. return;
  131. }
  132. root.AddProperty(name, value).Condition = condition;
  133. dirty = true;
  134. }
  135. AddPropertyIfNotPresent(name: "ApiConfiguration",
  136. condition: " '$(Configuration)' != 'Release' ",
  137. value: "Debug");
  138. AddPropertyIfNotPresent(name: "ApiConfiguration",
  139. condition: " '$(Configuration)' == 'Release' ",
  140. value: "Release");
  141. void SetReferenceHintPath(string referenceName, string condition, string hintPath)
  142. {
  143. foreach (var itemGroup in root.ItemGroups.Where(g =>
  144. g.Condition == string.Empty || g.Condition == condition))
  145. {
  146. var references = itemGroup.Items.Where(item =>
  147. item.ItemType == "Reference" &&
  148. item.Include == referenceName &&
  149. (item.Condition == condition || itemGroup.Condition == condition));
  150. var referencesWithHintPath = references.Where(reference =>
  151. reference.Metadata.Any(m => m.Name == "HintPath"));
  152. if (referencesWithHintPath.Any(reference => reference.Metadata
  153. .Any(m => m.Name == "HintPath" && m.Value == hintPath)))
  154. {
  155. // Found a Reference item with the right HintPath
  156. return;
  157. }
  158. var referenceWithHintPath = referencesWithHintPath.FirstOrDefault();
  159. if (referenceWithHintPath != null)
  160. {
  161. // Found a Reference item with a wrong HintPath
  162. foreach (var metadata in referenceWithHintPath.Metadata.ToList()
  163. .Where(m => m.Name == "HintPath"))
  164. {
  165. // Safe to remove as we duplicate with ToList() to loop
  166. referenceWithHintPath.RemoveChild(metadata);
  167. }
  168. referenceWithHintPath.AddMetadata("HintPath", hintPath);
  169. dirty = true;
  170. return;
  171. }
  172. var referenceWithoutHintPath = references.FirstOrDefault();
  173. if (referenceWithoutHintPath != null)
  174. {
  175. // Found a Reference item without a HintPath
  176. referenceWithoutHintPath.AddMetadata("HintPath", hintPath);
  177. dirty = true;
  178. return;
  179. }
  180. }
  181. // Found no Reference item at all. Add it.
  182. root.AddItem("Reference", referenceName).Condition = condition;
  183. dirty = true;
  184. }
  185. const string coreProjectName = "GodotSharp";
  186. const string editorProjectName = "GodotSharpEditor";
  187. const string coreCondition = "";
  188. const string editorCondition = " '$(Configuration)' == 'Tools' ";
  189. var coreHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{coreProjectName}.dll";
  190. var editorHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{editorProjectName}.dll";
  191. SetReferenceHintPath(coreProjectName, coreCondition, coreHintPath);
  192. SetReferenceHintPath(editorProjectName, editorCondition, editorHintPath);
  193. if (dirty)
  194. root.Save();
  195. }
  196. }
  197. }