| 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 |
|
| 10 |
namespace GodotTools.ProjectEditor |
| 11 |
{ |
| 12 |
public sealed class MSBuildProject |
| 13 |
{ |
| 14 |
internal ProjectRootElement Root { get; set; } |
| 15 |
|
| 16 |
public bool HasUnsavedChanges { get; set; } |
| 17 |
|
| 18 |
public void Save() => Root.Save(); |
| 19 |
|
| 20 |
public MSBuildProject(ProjectRootElement root) |
| 21 |
{ |
| 22 |
Root = root; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
public static partial class ProjectUtils |
| 27 |
{ |
| 28 |
[GeneratedRegex(@"\s*'\$\(GodotTargetPlatform\)'\s*==\s*'(?<platform>[A-z]+)'\s*", RegexOptions.IgnoreCase)] |
| 29 |
private static partial Regex GodotTargetPlatformConditionRegex(); |
| 30 |
|
| 31 |
private static readonly string[] _platformNames = |
| 32 |
{ |
| 33 |
"windows", |
| 34 |
"linuxbsd", |
| 35 |
"macos", |
| 36 |
"android", |
| 37 |
"ios", |
| 38 |
"web", |
| 39 |
}; |
| 40 |
|
| 41 |
public static void MSBuildLocatorRegisterLatest(out Version version, out string path) |
| 42 |
{ |
| 43 |
var instance = MSBuildLocator.QueryVisualStudioInstances() |
| 44 |
.OrderByDescending(x => x.Version) |
| 45 |
.First(); |
| 46 |
MSBuildLocator.RegisterInstance(instance); |
| 47 |
version = instance.Version; |
| 48 |
path = instance.MSBuildPath; |
| 49 |
} |
| 50 |
|
| 51 |
public static void MSBuildLocatorRegisterMSBuildPath(string msbuildPath) |
| 52 |
=> MSBuildLocator.RegisterMSBuildPath(msbuildPath); |
| 53 |
|
| 54 |
public static MSBuildProject? Open(string path) |
| 55 |
{ |
| 56 |
var root = ProjectRootElement.Open(path, ProjectCollection.GlobalProjectCollection, preserveFormatting: true); |
| 57 |
return root != null ? new MSBuildProject(root) : null; |
| 58 |
} |
| 59 |
|
| 60 |
public static void UpgradeProjectIfNeeded(MSBuildProject project, string projectName) |
| 61 |
{ |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
MigrateToProjectSdksStyle(project, projectName); |
| 66 |
|
| 67 |
EnsureGodotSdkIsUpToDate(project); |
| 68 |
EnsureTargetFrameworkMatchesMinimumRequirement(project); |
| 69 |
} |
| 70 |
|
| 71 |
private static void MigrateToProjectSdksStyle(MSBuildProject project, string projectName) |
| 72 |
{ |
| 73 |
var origRoot = project.Root; |
| 74 |
|
| 75 |
if (!string.IsNullOrEmpty(origRoot.Sdk)) |
| 76 |
return; |
| 77 |
|
| 78 |
project.Root = ProjectGenerator.GenGameProject(projectName); |
| 79 |
project.Root.FullPath = origRoot.FullPath; |
| 80 |
project.HasUnsavedChanges = true; |
| 81 |
} |
| 82 |
|
| 83 |
public static void EnsureGodotSdkIsUpToDate(MSBuildProject project) |
| 84 |
{ |
| 85 |
var root = project.Root; |
| 86 |
string godotSdkAttrValue = ProjectGenerator.GodotSdkAttrValue; |
| 87 |
|
| 88 |
if (!string.IsNullOrEmpty(root.Sdk) && |
| 89 |
root.Sdk.Trim().Equals(godotSdkAttrValue, StringComparison.OrdinalIgnoreCase)) |
| 90 |
return; |
| 91 |
|
| 92 |
root.Sdk = godotSdkAttrValue; |
| 93 |
project.HasUnsavedChanges = true; |
| 94 |
} |
| 95 |
|
| 96 |
private static void EnsureTargetFrameworkMatchesMinimumRequirement(MSBuildProject project) |
| 97 |
{ |
| 98 |
var root = project.Root; |
| 99 |
string minTfmValue = ProjectGenerator.GodotMinimumRequiredTfm; |
| 100 |
var minTfmVersion = NuGetFramework.Parse(minTfmValue).Version; |
| 101 |
|
| 102 |
ProjectPropertyGroupElement? mainPropertyGroup = null; |
| 103 |
ProjectPropertyElement? mainTargetFrameworkProperty = null; |
| 104 |
|
| 105 |
var propertiesToChange = new List<ProjectPropertyElement>(); |
| 106 |
|
| 107 |
foreach (var propertyGroup in root.PropertyGroups) |
| 108 |
{ |
| 109 |
bool groupHasCondition = !string.IsNullOrEmpty(propertyGroup.Condition); |
| 110 |
|
| 111 |
|
| 112 |
if (groupHasCondition && !ConditionMatchesGodotPlatform(propertyGroup.Condition)) |
| 113 |
{ |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
if (mainPropertyGroup == null && !groupHasCondition) |
| 120 |
{ |
| 121 |
mainPropertyGroup = propertyGroup; |
| 122 |
} |
| 123 |
|
| 124 |
foreach (var property in propertyGroup.Properties) |
| 125 |
{ |
| 126 |
|
| 127 |
if (property.Name != "TargetFramework") |
| 128 |
{ |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
bool propertyHasCondition = !string.IsNullOrEmpty(property.Condition); |
| 133 |
|
| 134 |
|
| 135 |
if (propertyHasCondition && !ConditionMatchesGodotPlatform(property.Condition)) |
| 136 |
{ |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
if (!groupHasCondition && !propertyHasCondition) |
| 141 |
{ |
| 142 |
|
| 143 |
|
| 144 |
if (mainTargetFrameworkProperty == null) |
| 145 |
{ |
| 146 |
mainTargetFrameworkProperty = property; |
| 147 |
} |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
var tfmVersion = NuGetFramework.Parse(property.Value).Version; |
| 154 |
if (tfmVersion <= minTfmVersion) |
| 155 |
{ |
| 156 |
propertiesToChange.Add(property); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (mainTargetFrameworkProperty == null) |
| 162 |
{ |
| 163 |
|
| 164 |
|
| 165 |
if (mainPropertyGroup == null) |
| 166 |
{ |
| 167 |
|
| 168 |
|
| 169 |
mainPropertyGroup = root.AddPropertyGroup(); |
| 170 |
} |
| 171 |
|
| 172 |
mainTargetFrameworkProperty = mainPropertyGroup.AddProperty("TargetFramework", minTfmValue); |
| 173 |
project.HasUnsavedChanges = true; |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
var tfmVersion = NuGetFramework.Parse(mainTargetFrameworkProperty.Value).Version; |
| 178 |
if (tfmVersion < minTfmVersion) |
| 179 |
{ |
| 180 |
mainTargetFrameworkProperty.Value = minTfmValue; |
| 181 |
project.HasUnsavedChanges = true; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
var mainTfmVersion = NuGetFramework.Parse(mainTargetFrameworkProperty.Value).Version; |
| 186 |
foreach (var property in propertiesToChange) |
| 187 |
{ |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if (mainTfmVersion > minTfmVersion) |
| 193 |
{ |
| 194 |
var propertyTfmVersion = NuGetFramework.Parse(property.Value).Version; |
| 195 |
if (propertyTfmVersion == minTfmVersion) |
| 196 |
{ |
| 197 |
|
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
property.Value = minTfmValue; |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
property.Parent.RemoveChild(property); |
| 206 |
} |
| 207 |
|
| 208 |
project.HasUnsavedChanges = true; |
| 209 |
} |
| 210 |
|
| 211 |
static bool ConditionMatchesGodotPlatform(string condition) |
| 212 |
{ |
| 213 |
|
| 214 |
|
| 215 |
var match = GodotTargetPlatformConditionRegex().Match(condition); |
| 216 |
if (match.Success) |
| 217 |
{ |
| 218 |
string platform = match.Groups["platform"].Value; |
| 219 |
return _platformNames.Contains(platform, StringComparer.OrdinalIgnoreCase); |
| 220 |
} |
| 221 |
|
| 222 |
return false; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |