ProjectUtils.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using Microsoft.Build.Construction;
  6. using Microsoft.Build.Evaluation;
  7. using Microsoft.Build.Locator;
  8. using NuGet.Frameworks;
  9. namespace GodotTools.ProjectEditor
  10. {
  11. public sealed class MSBuildProject
  12. {
  13. internal ProjectRootElement Root { get; set; }
  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 partial class ProjectUtils
  22. {
  23. [GeneratedRegex(@"\s*'\$\(GodotTargetPlatform\)'\s*==\s*'(?<platform>[A-z]+)'\s*", RegexOptions.IgnoreCase)]
  24. private static partial Regex GodotTargetPlatformConditionRegex();
  25. private static readonly string[] _platformNames =
  26. {
  27. "windows",
  28. "linuxbsd",
  29. "macos",
  30. "android",
  31. "ios",
  32. "web",
  33. };
  34. public static void MSBuildLocatorRegisterLatest(out Version version, out string path)
  35. {
  36. var instance = MSBuildLocator.QueryVisualStudioInstances()
  37. .OrderByDescending(x => x.Version)
  38. .First();
  39. MSBuildLocator.RegisterInstance(instance);
  40. version = instance.Version;
  41. path = instance.MSBuildPath;
  42. }
  43. public static void MSBuildLocatorRegisterMSBuildPath(string msbuildPath)
  44. => MSBuildLocator.RegisterMSBuildPath(msbuildPath);
  45. public static MSBuildProject? Open(string path)
  46. {
  47. var root = ProjectRootElement.Open(path, ProjectCollection.GlobalProjectCollection, preserveFormatting: true);
  48. return root != null ? new MSBuildProject(root) : null;
  49. }
  50. public static void UpgradeProjectIfNeeded(MSBuildProject project, string projectName)
  51. {
  52. // NOTE: The order in which changes are made to the project is important.
  53. // Migrate to MSBuild project Sdks style if using the old style.
  54. MigrateToProjectSdksStyle(project, projectName);
  55. EnsureGodotSdkIsUpToDate(project);
  56. EnsureTargetFrameworkMatchesMinimumRequirement(project);
  57. }
  58. private static void MigrateToProjectSdksStyle(MSBuildProject project, string projectName)
  59. {
  60. var origRoot = project.Root;
  61. if (!string.IsNullOrEmpty(origRoot.Sdk))
  62. return;
  63. project.Root = ProjectGenerator.GenGameProject(projectName);
  64. project.Root.FullPath = origRoot.FullPath;
  65. project.HasUnsavedChanges = true;
  66. }
  67. public static void EnsureGodotSdkIsUpToDate(MSBuildProject project)
  68. {
  69. var root = project.Root;
  70. string godotSdkAttrValue = ProjectGenerator.GodotSdkAttrValue;
  71. if (!string.IsNullOrEmpty(root.Sdk) &&
  72. root.Sdk.Trim().Equals(godotSdkAttrValue, StringComparison.OrdinalIgnoreCase))
  73. return;
  74. root.Sdk = godotSdkAttrValue;
  75. project.HasUnsavedChanges = true;
  76. }
  77. private static void EnsureTargetFrameworkMatchesMinimumRequirement(MSBuildProject project)
  78. {
  79. var root = project.Root;
  80. string minTfmValue = ProjectGenerator.GodotMinimumRequiredTfm;
  81. var minTfmVersion = NuGetFramework.Parse(minTfmValue).Version;
  82. ProjectPropertyGroupElement? mainPropertyGroup = null;
  83. ProjectPropertyElement? mainTargetFrameworkProperty = null;
  84. var propertiesToChange = new List<ProjectPropertyElement>();
  85. foreach (var propertyGroup in root.PropertyGroups)
  86. {
  87. bool groupHasCondition = !string.IsNullOrEmpty(propertyGroup.Condition);
  88. // Check if the property group should be excluded from checking for 'TargetFramework' properties.
  89. if (groupHasCondition && !ConditionMatchesGodotPlatform(propertyGroup.Condition))
  90. {
  91. continue;
  92. }
  93. // Store a reference to the first property group without conditions,
  94. // in case we need to add a new 'TargetFramework' property later.
  95. if (mainPropertyGroup == null && !groupHasCondition)
  96. {
  97. mainPropertyGroup = propertyGroup;
  98. }
  99. foreach (var property in propertyGroup.Properties)
  100. {
  101. // We are looking for 'TargetFramework' properties.
  102. if (property.Name != "TargetFramework")
  103. {
  104. continue;
  105. }
  106. bool propertyHasCondition = !string.IsNullOrEmpty(property.Condition);
  107. // Check if the property should be excluded.
  108. if (propertyHasCondition && !ConditionMatchesGodotPlatform(property.Condition))
  109. {
  110. continue;
  111. }
  112. if (!groupHasCondition && !propertyHasCondition)
  113. {
  114. // Store a reference to the 'TargetFramework' that has no conditions
  115. // because it applies to all platforms.
  116. if (mainTargetFrameworkProperty == null)
  117. {
  118. mainTargetFrameworkProperty = property;
  119. }
  120. continue;
  121. }
  122. // If the 'TargetFramework' property is conditional, it may no longer be needed
  123. // when the main one is upgraded to the new minimum version.
  124. var tfmVersion = NuGetFramework.Parse(property.Value).Version;
  125. if (tfmVersion <= minTfmVersion)
  126. {
  127. propertiesToChange.Add(property);
  128. }
  129. }
  130. }
  131. if (mainTargetFrameworkProperty == null)
  132. {
  133. // We haven't found a 'TargetFramework' property without conditions,
  134. // we'll just add one in the first property group without conditions.
  135. if (mainPropertyGroup == null)
  136. {
  137. // We also don't have a property group without conditions,
  138. // so we'll add a new one to the project.
  139. mainPropertyGroup = root.AddPropertyGroup();
  140. }
  141. mainTargetFrameworkProperty = mainPropertyGroup.AddProperty("TargetFramework", minTfmValue);
  142. project.HasUnsavedChanges = true;
  143. }
  144. else
  145. {
  146. var tfmVersion = NuGetFramework.Parse(mainTargetFrameworkProperty.Value).Version;
  147. if (tfmVersion < minTfmVersion)
  148. {
  149. mainTargetFrameworkProperty.Value = minTfmValue;
  150. project.HasUnsavedChanges = true;
  151. }
  152. }
  153. var mainTfmVersion = NuGetFramework.Parse(mainTargetFrameworkProperty.Value).Version;
  154. foreach (var property in propertiesToChange)
  155. {
  156. // If the main 'TargetFramework' property targets a version newer than
  157. // the minimum required by Godot, we don't want to remove the conditional
  158. // 'TargetFramework' properties, only upgrade them to the new minimum.
  159. // Otherwise, it can be removed.
  160. if (mainTfmVersion > minTfmVersion)
  161. {
  162. property.Value = minTfmValue;
  163. }
  164. else
  165. {
  166. property.Parent.RemoveChild(property);
  167. }
  168. project.HasUnsavedChanges = true;
  169. }
  170. static bool ConditionMatchesGodotPlatform(string condition)
  171. {
  172. // Check if the condition is checking the 'GodotTargetPlatform' for one of the
  173. // Godot platforms with built-in support in the Godot.NET.Sdk.
  174. var match = GodotTargetPlatformConditionRegex().Match(condition);
  175. if (match.Success)
  176. {
  177. string platform = match.Groups["platform"].Value;
  178. return _platformNames.Contains(platform, StringComparer.OrdinalIgnoreCase);
  179. }
  180. return false;
  181. }
  182. }
  183. }
  184. }