ProjectUtils.cs 16 KB

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