C#: Fix always saving copy of csproj even with no changes

This was a regression from 93d7ec8836 (#38110).
Mono's old implementation of Microsoft.Build hardcodes HasUnsavedChanges to
always return true.
This workaround can be reverted once we switch to official Microsoft.Build.

(cherry picked from commit 81f13f6171)
This commit is contained in:
Ignacio Etcheverry 2020-04-23 16:12:22 +02:00 committed by Rémi Verschelde
parent 24265c498b
commit a98d44f654
1 changed files with 23 additions and 2 deletions

View File

@ -13,7 +13,7 @@ namespace GodotTools.ProjectEditor
{ {
public ProjectRootElement Root { get; } public ProjectRootElement Root { get; }
public bool HasUnsavedChanges => Root.HasUnsavedChanges; public bool HasUnsavedChanges { get; set; }
public void Save() => Root.Save(); public void Save() => Root.Save();
@ -78,6 +78,8 @@ namespace GodotTools.ProjectEditor
var root = ProjectRootElement.Open(projectPath); var root = ProjectRootElement.Open(projectPath);
Debug.Assert(root != null); Debug.Assert(root != null);
bool dirty = false;
var oldFolderNormalized = oldFolder.NormalizePath(); var oldFolderNormalized = oldFolder.NormalizePath();
var newFolderNormalized = newFolder.NormalizePath(); var newFolderNormalized = newFolder.NormalizePath();
string absOldFolderNormalized = Path.GetFullPath(oldFolderNormalized).NormalizePath(); string absOldFolderNormalized = Path.GetFullPath(oldFolderNormalized).NormalizePath();
@ -88,9 +90,10 @@ namespace GodotTools.ProjectEditor
string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath(); string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath();
string absNewIncludeNormalized = absNewFolderNormalized + absPathNormalized.Substring(absOldFolderNormalized.Length); string absNewIncludeNormalized = absNewFolderNormalized + absPathNormalized.Substring(absOldFolderNormalized.Length);
item.Include = absNewIncludeNormalized.RelativeToPath(dir).Replace("/", "\\"); item.Include = absNewIncludeNormalized.RelativeToPath(dir).Replace("/", "\\");
dirty = true;
} }
if (root.HasUnsavedChanges) if (dirty)
root.Save(); root.Save();
} }
@ -183,6 +186,7 @@ namespace GodotTools.ProjectEditor
} }
root.AddProperty(name, value).Condition = " " + condition + " "; root.AddProperty(name, value).Condition = " " + condition + " ";
project.HasUnsavedChanges = true;
} }
AddPropertyIfNotPresent(name: "ApiConfiguration", AddPropertyIfNotPresent(name: "ApiConfiguration",
@ -224,6 +228,7 @@ namespace GodotTools.ProjectEditor
} }
referenceWithHintPath.AddMetadata("HintPath", hintPath); referenceWithHintPath.AddMetadata("HintPath", hintPath);
project.HasUnsavedChanges = true;
return; return;
} }
@ -232,12 +237,14 @@ namespace GodotTools.ProjectEditor
{ {
// Found a Reference item without a HintPath // Found a Reference item without a HintPath
referenceWithoutHintPath.AddMetadata("HintPath", hintPath); referenceWithoutHintPath.AddMetadata("HintPath", hintPath);
project.HasUnsavedChanges = true;
return; return;
} }
} }
// Found no Reference item at all. Add it. // Found no Reference item at all. Add it.
root.AddItem("Reference", referenceName).Condition = " " + condition + " "; root.AddItem("Reference", referenceName).Condition = " " + condition + " ";
project.HasUnsavedChanges = true;
} }
const string coreProjectName = "GodotSharp"; const string coreProjectName = "GodotSharp";
@ -270,6 +277,7 @@ namespace GodotTools.ProjectEditor
{ {
configItem.Value = "Debug"; configItem.Value = "Debug";
foundOldConfiguration = true; foundOldConfiguration = true;
project.HasUnsavedChanges = true;
} }
} }
@ -277,6 +285,7 @@ namespace GodotTools.ProjectEditor
{ {
root.PropertyGroups.First(g => g.Condition == string.Empty)? root.PropertyGroups.First(g => g.Condition == string.Empty)?
.AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString()); .AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString());
project.HasUnsavedChanges = true;
} }
if (!foundOldConfiguration) if (!foundOldConfiguration)
@ -300,21 +309,33 @@ namespace GodotTools.ProjectEditor
void MigrateConditions(string oldCondition, string newCondition) void MigrateConditions(string oldCondition, string newCondition)
{ {
foreach (var propertyGroup in root.PropertyGroups.Where(g => g.Condition.Trim() == oldCondition)) foreach (var propertyGroup in root.PropertyGroups.Where(g => g.Condition.Trim() == oldCondition))
{
propertyGroup.Condition = " " + newCondition + " "; propertyGroup.Condition = " " + newCondition + " ";
project.HasUnsavedChanges = true;
}
foreach (var propertyGroup in root.PropertyGroups) foreach (var propertyGroup in root.PropertyGroups)
{ {
foreach (var prop in propertyGroup.Properties.Where(p => p.Condition.Trim() == oldCondition)) foreach (var prop in propertyGroup.Properties.Where(p => p.Condition.Trim() == oldCondition))
{
prop.Condition = " " + newCondition + " "; prop.Condition = " " + newCondition + " ";
project.HasUnsavedChanges = true;
}
} }
foreach (var itemGroup in root.ItemGroups.Where(g => g.Condition.Trim() == oldCondition)) foreach (var itemGroup in root.ItemGroups.Where(g => g.Condition.Trim() == oldCondition))
{
itemGroup.Condition = " " + newCondition + " "; itemGroup.Condition = " " + newCondition + " ";
project.HasUnsavedChanges = true;
}
foreach (var itemGroup in root.ItemGroups) foreach (var itemGroup in root.ItemGroups)
{ {
foreach (var item in itemGroup.Items.Where(item => item.Condition.Trim() == oldCondition)) foreach (var item in itemGroup.Items.Where(item => item.Condition.Trim() == oldCondition))
{
item.Condition = " " + newCondition + " "; item.Condition = " " + newCondition + " ";
project.HasUnsavedChanges = true;
}
} }
} }