ProjectUtils.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using GodotTools.Core;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using DotNet.Globbing;
  8. using Microsoft.Build.Construction;
  9. namespace GodotTools.ProjectEditor
  10. {
  11. public static class ProjectUtils
  12. {
  13. public static void AddItemToProjectChecked(string projectPath, string itemType, string include)
  14. {
  15. var dir = Directory.GetParent(projectPath).FullName;
  16. var root = ProjectRootElement.Open(projectPath);
  17. Debug.Assert(root != null);
  18. var normalizedInclude = include.RelativeToPath(dir).Replace("/", "\\");
  19. if (root.AddItemChecked(itemType, normalizedInclude))
  20. root.Save();
  21. }
  22. public static void RenameItemInProjectChecked(string projectPath, string itemType, string oldInclude, string newInclude)
  23. {
  24. var dir = Directory.GetParent(projectPath).FullName;
  25. var root = ProjectRootElement.Open(projectPath);
  26. Debug.Assert(root != null);
  27. var normalizedOldInclude = oldInclude.NormalizePath();
  28. var normalizedNewInclude = newInclude.NormalizePath();
  29. var item = root.FindItemOrNullAbs(itemType, normalizedOldInclude);
  30. if (item == null)
  31. return;
  32. item.Include = normalizedNewInclude.RelativeToPath(dir).Replace("/", "\\");
  33. root.Save();
  34. }
  35. public static void RemoveItemFromProjectChecked(string projectPath, string itemType, string include)
  36. {
  37. var dir = Directory.GetParent(projectPath).FullName;
  38. var root = ProjectRootElement.Open(projectPath);
  39. Debug.Assert(root != null);
  40. var normalizedInclude = include.NormalizePath();
  41. if (root.RemoveItemChecked(itemType, normalizedInclude))
  42. root.Save();
  43. }
  44. public static void RenameItemsToNewFolderInProjectChecked(string projectPath, string itemType, string oldFolder, string newFolder)
  45. {
  46. var dir = Directory.GetParent(projectPath).FullName;
  47. var root = ProjectRootElement.Open(projectPath);
  48. Debug.Assert(root != null);
  49. bool dirty = false;
  50. var oldFolderNormalized = oldFolder.NormalizePath();
  51. var newFolderNormalized = newFolder.NormalizePath();
  52. string absOldFolderNormalized = Path.GetFullPath(oldFolderNormalized).NormalizePath();
  53. string absNewFolderNormalized = Path.GetFullPath(newFolderNormalized).NormalizePath();
  54. foreach (var item in root.FindAllItemsInFolder(itemType, oldFolderNormalized))
  55. {
  56. string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath();
  57. string absNewIncludeNormalized = absNewFolderNormalized + absPathNormalized.Substring(absOldFolderNormalized.Length);
  58. item.Include = absNewIncludeNormalized.RelativeToPath(dir).Replace("/", "\\");
  59. dirty = true;
  60. }
  61. if (dirty)
  62. root.Save();
  63. }
  64. public static void RemoveItemsInFolderFromProjectChecked(string projectPath, string itemType, string folder)
  65. {
  66. var root = ProjectRootElement.Open(projectPath);
  67. Debug.Assert(root != null);
  68. var folderNormalized = folder.NormalizePath();
  69. var itemsToRemove = root.FindAllItemsInFolder(itemType, folderNormalized).ToList();
  70. if (itemsToRemove.Count > 0)
  71. {
  72. foreach (var item in itemsToRemove)
  73. item.Parent.RemoveChild(item);
  74. root.Save();
  75. }
  76. }
  77. private static string[] GetAllFilesRecursive(string rootDirectory, string mask)
  78. {
  79. string[] files = Directory.GetFiles(rootDirectory, mask, SearchOption.AllDirectories);
  80. // We want relative paths
  81. for (int i = 0; i < files.Length; i++)
  82. {
  83. files[i] = files[i].RelativeToPath(rootDirectory);
  84. }
  85. return files;
  86. }
  87. public static string[] GetIncludeFiles(string projectPath, string itemType)
  88. {
  89. var result = new List<string>();
  90. var existingFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
  91. var globOptions = new GlobOptions();
  92. globOptions.Evaluation.CaseInsensitive = false;
  93. var root = ProjectRootElement.Open(projectPath);
  94. Debug.Assert(root != null);
  95. foreach (var itemGroup in root.ItemGroups)
  96. {
  97. if (itemGroup.Condition.Length != 0)
  98. continue;
  99. foreach (var item in itemGroup.Items)
  100. {
  101. if (item.ItemType != itemType)
  102. continue;
  103. string normalizedInclude = item.Include.NormalizePath();
  104. var glob = Glob.Parse(normalizedInclude, globOptions);
  105. // TODO Check somehow if path has no blob to avoid the following loop...
  106. foreach (var existingFile in existingFiles)
  107. {
  108. if (glob.IsMatch(existingFile))
  109. {
  110. result.Add(existingFile);
  111. }
  112. }
  113. }
  114. }
  115. return result.ToArray();
  116. }
  117. /// Simple function to make sure the Api assembly references are configured correctly
  118. public static void FixApiHintPath(string projectPath)
  119. {
  120. var root = ProjectRootElement.Open(projectPath);
  121. Debug.Assert(root != null);
  122. bool dirty = false;
  123. void AddPropertyIfNotPresent(string name, string condition, string value)
  124. {
  125. if (root.PropertyGroups
  126. .Any(g => (g.Condition == string.Empty || g.Condition.Trim() == condition) &&
  127. g.Properties
  128. .Any(p => p.Name == name &&
  129. p.Value == value &&
  130. (p.Condition.Trim() == condition || g.Condition.Trim() == condition))))
  131. {
  132. return;
  133. }
  134. root.AddProperty(name, value).Condition = " " + condition + " ";
  135. dirty = true;
  136. }
  137. AddPropertyIfNotPresent(name: "ApiConfiguration",
  138. condition: "'$(Configuration)' != 'ExportRelease'",
  139. value: "Debug");
  140. AddPropertyIfNotPresent(name: "ApiConfiguration",
  141. condition: "'$(Configuration)' == 'ExportRelease'",
  142. value: "Release");
  143. void SetReferenceHintPath(string referenceName, string condition, string hintPath)
  144. {
  145. foreach (var itemGroup in root.ItemGroups.Where(g =>
  146. g.Condition.Trim() == string.Empty || g.Condition.Trim() == condition))
  147. {
  148. var references = itemGroup.Items.Where(item =>
  149. item.ItemType == "Reference" &&
  150. item.Include == referenceName &&
  151. (item.Condition.Trim() == condition || itemGroup.Condition.Trim() == condition));
  152. var referencesWithHintPath = references.Where(reference =>
  153. reference.Metadata.Any(m => m.Name == "HintPath"));
  154. if (referencesWithHintPath.Any(reference => reference.Metadata
  155. .Any(m => m.Name == "HintPath" && m.Value == hintPath)))
  156. {
  157. // Found a Reference item with the right HintPath
  158. return;
  159. }
  160. var referenceWithHintPath = referencesWithHintPath.FirstOrDefault();
  161. if (referenceWithHintPath != null)
  162. {
  163. // Found a Reference item with a wrong HintPath
  164. foreach (var metadata in referenceWithHintPath.Metadata.ToList()
  165. .Where(m => m.Name == "HintPath"))
  166. {
  167. // Safe to remove as we duplicate with ToList() to loop
  168. referenceWithHintPath.RemoveChild(metadata);
  169. }
  170. referenceWithHintPath.AddMetadata("HintPath", hintPath);
  171. dirty = true;
  172. return;
  173. }
  174. var referenceWithoutHintPath = references.FirstOrDefault();
  175. if (referenceWithoutHintPath != null)
  176. {
  177. // Found a Reference item without a HintPath
  178. referenceWithoutHintPath.AddMetadata("HintPath", hintPath);
  179. dirty = true;
  180. return;
  181. }
  182. }
  183. // Found no Reference item at all. Add it.
  184. root.AddItem("Reference", referenceName).Condition = " " + condition + " ";
  185. dirty = true;
  186. }
  187. const string coreProjectName = "GodotSharp";
  188. const string editorProjectName = "GodotSharpEditor";
  189. const string coreCondition = "";
  190. const string editorCondition = "'$(Configuration)' == 'Debug'";
  191. var coreHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{coreProjectName}.dll";
  192. var editorHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{editorProjectName}.dll";
  193. SetReferenceHintPath(coreProjectName, coreCondition, coreHintPath);
  194. SetReferenceHintPath(editorProjectName, editorCondition, editorHintPath);
  195. if (dirty)
  196. root.Save();
  197. }
  198. public static void MigrateFromOldConfigNames(string projectPath)
  199. {
  200. var root = ProjectRootElement.Open(projectPath);
  201. Debug.Assert(root != null);
  202. bool dirty = false;
  203. bool hasGodotProjectGeneratorVersion = false;
  204. bool foundOldConfiguration = false;
  205. foreach (var propertyGroup in root.PropertyGroups.Where(g => g.Condition == string.Empty))
  206. {
  207. if (!hasGodotProjectGeneratorVersion && propertyGroup.Properties.Any(p => p.Name == "GodotProjectGeneratorVersion"))
  208. hasGodotProjectGeneratorVersion = true;
  209. foreach (var configItem in propertyGroup.Properties
  210. .Where(p => p.Condition.Trim() == "'$(Configuration)' == ''" && p.Value == "Tools"))
  211. {
  212. configItem.Value = "Debug";
  213. foundOldConfiguration = true;
  214. dirty = true;
  215. }
  216. }
  217. if (!hasGodotProjectGeneratorVersion)
  218. {
  219. root.PropertyGroups.First(g => g.Condition == string.Empty)?
  220. .AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString());
  221. dirty = true;
  222. }
  223. if (!foundOldConfiguration)
  224. {
  225. var toolsConditions = new[]
  226. {
  227. "'$(Configuration)|$(Platform)' == 'Tools|AnyCPU'",
  228. "'$(Configuration)|$(Platform)' != 'Tools|AnyCPU'",
  229. "'$(Configuration)' == 'Tools'",
  230. "'$(Configuration)' != 'Tools'"
  231. };
  232. foundOldConfiguration = root.PropertyGroups
  233. .Any(g => toolsConditions.Any(c => c == g.Condition.Trim()));
  234. }
  235. if (foundOldConfiguration)
  236. {
  237. void MigrateConfigurationConditions(string oldConfiguration, string newConfiguration)
  238. {
  239. void MigrateConditions(string oldCondition, string newCondition)
  240. {
  241. foreach (var propertyGroup in root.PropertyGroups.Where(g => g.Condition.Trim() == oldCondition))
  242. {
  243. propertyGroup.Condition = " " + newCondition + " ";
  244. dirty = true;
  245. }
  246. foreach (var propertyGroup in root.PropertyGroups)
  247. {
  248. foreach (var prop in propertyGroup.Properties.Where(p => p.Condition.Trim() == oldCondition))
  249. {
  250. prop.Condition = " " + newCondition + " ";
  251. dirty = true;
  252. }
  253. }
  254. foreach (var itemGroup in root.ItemGroups.Where(g => g.Condition.Trim() == oldCondition))
  255. {
  256. itemGroup.Condition = " " + newCondition + " ";
  257. dirty = true;
  258. }
  259. foreach (var itemGroup in root.ItemGroups)
  260. {
  261. foreach (var item in itemGroup.Items.Where(item => item.Condition.Trim() == oldCondition))
  262. {
  263. item.Condition = " " + newCondition + " ";
  264. dirty = true;
  265. }
  266. }
  267. }
  268. foreach (var op in new[] {"==", "!="})
  269. {
  270. MigrateConditions($"'$(Configuration)|$(Platform)' {op} '{oldConfiguration}|AnyCPU'", $"'$(Configuration)|$(Platform)' {op} '{newConfiguration}|AnyCPU'");
  271. MigrateConditions($"'$(Configuration)' {op} '{oldConfiguration}'", $"'$(Configuration)' {op} '{newConfiguration}'");
  272. }
  273. }
  274. MigrateConfigurationConditions("Debug", "ExportDebug");
  275. MigrateConfigurationConditions("Release", "ExportRelease");
  276. MigrateConfigurationConditions("Tools", "Debug"); // Must be last
  277. }
  278. if (dirty)
  279. root.Save();
  280. }
  281. }
  282. }