diff options
author | Ignacio Etcheverry <ignalfonsore@gmail.com> | 2020-03-14 19:01:29 +0100 |
---|---|---|
committer | Ignacio Etcheverry <ignalfonsore@gmail.com> | 2020-03-14 19:01:29 +0100 |
commit | 1b634785b50a8be5ff6f06feeb13726750115615 (patch) | |
tree | e7ac2491c2391bca2d592f3ef9796d26180512fc /modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs | |
parent | ce01b83c4af6b61febecea68596a0806d8723d10 (diff) |
C#: Replace uses of old Configuration and update old csprojs
Diffstat (limited to 'modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs')
-rw-r--r-- | modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs | 119 |
1 files changed, 110 insertions, 9 deletions
diff --git a/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs b/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs index 233aab45b3..af36f125f5 100644 --- a/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs +++ b/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Reflection; using DotNet.Globbing; using Microsoft.Build.Construction; @@ -44,6 +45,7 @@ namespace GodotTools.ProjectEditor globOptions.Evaluation.CaseInsensitive = false; var root = ProjectRootElement.Open(projectPath); + Debug.Assert(root != null); foreach (var itemGroup in root.ItemGroups) { @@ -85,35 +87,35 @@ namespace GodotTools.ProjectEditor void AddPropertyIfNotPresent(string name, string condition, string value) { if (root.PropertyGroups - .Any(g => (g.Condition == string.Empty || g.Condition == condition) && + .Any(g => (g.Condition == string.Empty || g.Condition.Trim() == condition) && g.Properties .Any(p => p.Name == name && p.Value == value && - (p.Condition == condition || g.Condition == condition)))) + (p.Condition.Trim() == condition || g.Condition.Trim() == condition)))) { return; } - root.AddProperty(name, value).Condition = condition; + root.AddProperty(name, value).Condition = " " + condition + " "; dirty = true; } AddPropertyIfNotPresent(name: "ApiConfiguration", - condition: " '$(Configuration)' != 'Release' ", + condition: "'$(Configuration)' != 'ExportRelease'", value: "Debug"); AddPropertyIfNotPresent(name: "ApiConfiguration", - condition: " '$(Configuration)' == 'Release' ", + condition: "'$(Configuration)' == 'ExportRelease'", value: "Release"); void SetReferenceHintPath(string referenceName, string condition, string hintPath) { foreach (var itemGroup in root.ItemGroups.Where(g => - g.Condition == string.Empty || g.Condition == condition)) + g.Condition.Trim() == string.Empty || g.Condition.Trim() == condition)) { var references = itemGroup.Items.Where(item => item.ItemType == "Reference" && item.Include == referenceName && - (item.Condition == condition || itemGroup.Condition == condition)); + (item.Condition.Trim() == condition || itemGroup.Condition.Trim() == condition)); var referencesWithHintPath = references.Where(reference => reference.Metadata.Any(m => m.Name == "HintPath")); @@ -152,7 +154,7 @@ namespace GodotTools.ProjectEditor } // Found no Reference item at all. Add it. - root.AddItem("Reference", referenceName).Condition = condition; + root.AddItem("Reference", referenceName).Condition = " " + condition + " "; dirty = true; } @@ -160,7 +162,7 @@ namespace GodotTools.ProjectEditor const string editorProjectName = "GodotSharpEditor"; const string coreCondition = ""; - const string editorCondition = " '$(Configuration)' == 'Tools' "; + const string editorCondition = "'$(Configuration)' == 'Debug'"; var coreHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{coreProjectName}.dll"; var editorHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{editorProjectName}.dll"; @@ -171,5 +173,104 @@ namespace GodotTools.ProjectEditor if (dirty) root.Save(); } + + public static void MigrateFromOldConfigNames(string projectPath) + { + var root = ProjectRootElement.Open(projectPath); + Debug.Assert(root != null); + + bool dirty = false; + + bool hasGodotProjectGeneratorVersion = false; + bool foundOldConfiguration = false; + + foreach (var propertyGroup in root.PropertyGroups.Where(g => g.Condition == string.Empty)) + { + if (!hasGodotProjectGeneratorVersion && propertyGroup.Properties.Any(p => p.Name == "GodotProjectGeneratorVersion")) + hasGodotProjectGeneratorVersion = true; + + foreach (var configItem in propertyGroup.Properties + .Where(p => p.Condition.Trim() == "'$(Configuration)' == ''" && p.Value == "Tools")) + { + configItem.Value = "Debug"; + foundOldConfiguration = true; + dirty = true; + } + } + + if (!hasGodotProjectGeneratorVersion) + { + root.PropertyGroups.First(g => g.Condition == string.Empty)? + .AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString()); + dirty = true; + } + + if (!foundOldConfiguration) + { + var toolsConditions = new[] + { + "'$(Configuration)|$(Platform)' == 'Tools|AnyCPU'", + "'$(Configuration)|$(Platform)' != 'Tools|AnyCPU'", + "'$(Configuration)' == 'Tools'", + "'$(Configuration)' != 'Tools'" + }; + + foundOldConfiguration = root.PropertyGroups + .Any(g => toolsConditions.Any(c => c == g.Condition.Trim())); + } + + if (foundOldConfiguration) + { + void MigrateConfigurationConditions(string oldConfiguration, string newConfiguration) + { + void MigrateConditions(string oldCondition, string newCondition) + { + foreach (var propertyGroup in root.PropertyGroups.Where(g => g.Condition.Trim() == oldCondition)) + { + propertyGroup.Condition = " " + newCondition + " "; + dirty = true; + } + + foreach (var propertyGroup in root.PropertyGroups) + { + foreach (var prop in propertyGroup.Properties.Where(p => p.Condition.Trim() == oldCondition)) + { + prop.Condition = " " + newCondition + " "; + dirty = true; + } + } + + foreach (var itemGroup in root.ItemGroups.Where(g => g.Condition.Trim() == oldCondition)) + { + itemGroup.Condition = " " + newCondition + " "; + dirty = true; + } + + foreach (var itemGroup in root.ItemGroups) + { + foreach (var item in itemGroup.Items.Where(item => item.Condition.Trim() == oldCondition)) + { + item.Condition = " " + newCondition + " "; + dirty = true; + } + } + } + + foreach (var op in new[] {"==", "!="}) + { + MigrateConditions($"'$(Configuration)|$(Platform)' {op} '{oldConfiguration}|AnyCPU'", $"'$(Configuration)|$(Platform)' {op} '{newConfiguration}|AnyCPU'"); + MigrateConditions($"'$(Configuration)' {op} '{oldConfiguration}'", $"'$(Configuration)' {op} '{newConfiguration}'"); + } + } + + MigrateConfigurationConditions("Debug", "ExportDebug"); + MigrateConfigurationConditions("Release", "ExportRelease"); + MigrateConfigurationConditions("Tools", "Debug"); // Must be last + } + + + if (dirty) + root.Save(); + } } } |