2019-07-03 07:44:53 +00:00
|
|
|
using GodotTools.Core;
|
2018-10-22 17:43:19 +00:00
|
|
|
using System.Collections.Generic;
|
2019-07-10 19:53:49 +00:00
|
|
|
using System.Diagnostics;
|
2017-10-02 21:24:00 +00:00
|
|
|
using System.IO;
|
2019-07-10 19:53:49 +00:00
|
|
|
using System.Linq;
|
2020-03-14 18:01:29 +00:00
|
|
|
using System.Reflection;
|
2017-10-02 21:24:00 +00:00
|
|
|
using Microsoft.Build.Construction;
|
2020-08-20 10:56:36 +00:00
|
|
|
using Microsoft.Build.Globbing;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
namespace GodotTools.ProjectEditor
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2020-04-22 12:39:42 +00:00
|
|
|
public sealed class MSBuildProject
|
|
|
|
{
|
|
|
|
public ProjectRootElement Root { get; }
|
|
|
|
|
2020-04-23 14:12:22 +00:00
|
|
|
public bool HasUnsavedChanges { get; set; }
|
2020-04-22 12:39:42 +00:00
|
|
|
|
|
|
|
public void Save() => Root.Save();
|
|
|
|
|
|
|
|
public MSBuildProject(ProjectRootElement root)
|
|
|
|
{
|
|
|
|
Root = root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
public static class ProjectUtils
|
|
|
|
{
|
2020-04-22 12:39:42 +00:00
|
|
|
public static MSBuildProject Open(string path)
|
|
|
|
{
|
|
|
|
var root = ProjectRootElement.Open(path);
|
|
|
|
return root != null ? new MSBuildProject(root) : null;
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
public static void AddItemToProjectChecked(string projectPath, string itemType, string include)
|
|
|
|
{
|
|
|
|
var dir = Directory.GetParent(projectPath).FullName;
|
|
|
|
var root = ProjectRootElement.Open(projectPath);
|
2019-07-10 19:53:49 +00:00
|
|
|
Debug.Assert(root != null);
|
|
|
|
|
2018-10-22 17:43:19 +00:00
|
|
|
var normalizedInclude = include.RelativeToPath(dir).Replace("/", "\\");
|
|
|
|
|
|
|
|
if (root.AddItemChecked(itemType, normalizedInclude))
|
2017-10-04 22:10:51 +00:00
|
|
|
root.Save();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
2018-10-22 17:43:19 +00:00
|
|
|
|
2020-03-18 14:05:50 +00:00
|
|
|
public static void RenameItemInProjectChecked(string projectPath, string itemType, string oldInclude, string newInclude)
|
|
|
|
{
|
|
|
|
var dir = Directory.GetParent(projectPath).FullName;
|
|
|
|
var root = ProjectRootElement.Open(projectPath);
|
|
|
|
Debug.Assert(root != null);
|
|
|
|
|
|
|
|
var normalizedOldInclude = oldInclude.NormalizePath();
|
|
|
|
var normalizedNewInclude = newInclude.NormalizePath();
|
|
|
|
|
|
|
|
var item = root.FindItemOrNullAbs(itemType, normalizedOldInclude);
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
item.Include = normalizedNewInclude.RelativeToPath(dir).Replace("/", "\\");
|
|
|
|
root.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RemoveItemFromProjectChecked(string projectPath, string itemType, string include)
|
|
|
|
{
|
|
|
|
var root = ProjectRootElement.Open(projectPath);
|
|
|
|
Debug.Assert(root != null);
|
|
|
|
|
|
|
|
var normalizedInclude = include.NormalizePath();
|
|
|
|
|
|
|
|
if (root.RemoveItemChecked(itemType, normalizedInclude))
|
|
|
|
root.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RenameItemsToNewFolderInProjectChecked(string projectPath, string itemType, string oldFolder, string newFolder)
|
|
|
|
{
|
|
|
|
var dir = Directory.GetParent(projectPath).FullName;
|
|
|
|
var root = ProjectRootElement.Open(projectPath);
|
|
|
|
Debug.Assert(root != null);
|
|
|
|
|
2020-04-23 14:12:22 +00:00
|
|
|
bool dirty = false;
|
|
|
|
|
2020-03-18 14:05:50 +00:00
|
|
|
var oldFolderNormalized = oldFolder.NormalizePath();
|
|
|
|
var newFolderNormalized = newFolder.NormalizePath();
|
|
|
|
string absOldFolderNormalized = Path.GetFullPath(oldFolderNormalized).NormalizePath();
|
|
|
|
string absNewFolderNormalized = Path.GetFullPath(newFolderNormalized).NormalizePath();
|
|
|
|
|
|
|
|
foreach (var item in root.FindAllItemsInFolder(itemType, oldFolderNormalized))
|
|
|
|
{
|
|
|
|
string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath();
|
|
|
|
string absNewIncludeNormalized = absNewFolderNormalized + absPathNormalized.Substring(absOldFolderNormalized.Length);
|
|
|
|
item.Include = absNewIncludeNormalized.RelativeToPath(dir).Replace("/", "\\");
|
2020-04-23 14:12:22 +00:00
|
|
|
dirty = true;
|
2020-03-18 14:05:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 14:12:22 +00:00
|
|
|
if (dirty)
|
2020-03-18 14:05:50 +00:00
|
|
|
root.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void RemoveItemsInFolderFromProjectChecked(string projectPath, string itemType, string folder)
|
|
|
|
{
|
|
|
|
var root = ProjectRootElement.Open(projectPath);
|
|
|
|
Debug.Assert(root != null);
|
|
|
|
|
|
|
|
var folderNormalized = folder.NormalizePath();
|
|
|
|
|
|
|
|
var itemsToRemove = root.FindAllItemsInFolder(itemType, folderNormalized).ToList();
|
|
|
|
|
|
|
|
if (itemsToRemove.Count > 0)
|
|
|
|
{
|
|
|
|
foreach (var item in itemsToRemove)
|
|
|
|
item.Parent.RemoveChild(item);
|
|
|
|
|
|
|
|
root.Save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 17:43:19 +00:00
|
|
|
private static string[] GetAllFilesRecursive(string rootDirectory, string mask)
|
|
|
|
{
|
|
|
|
string[] files = Directory.GetFiles(rootDirectory, mask, SearchOption.AllDirectories);
|
|
|
|
|
|
|
|
// We want relative paths
|
2019-07-10 19:53:49 +00:00
|
|
|
for (int i = 0; i < files.Length; i++)
|
|
|
|
{
|
2018-10-22 17:43:19 +00:00
|
|
|
files[i] = files[i].RelativeToPath(rootDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string[] GetIncludeFiles(string projectPath, string itemType)
|
|
|
|
{
|
|
|
|
var result = new List<string>();
|
|
|
|
var existingFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
|
|
|
|
|
|
|
|
var root = ProjectRootElement.Open(projectPath);
|
2020-03-14 18:01:29 +00:00
|
|
|
Debug.Assert(root != null);
|
2018-10-22 17:43:19 +00:00
|
|
|
|
|
|
|
foreach (var itemGroup in root.ItemGroups)
|
|
|
|
{
|
|
|
|
if (itemGroup.Condition.Length != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
foreach (var item in itemGroup.Items)
|
|
|
|
{
|
|
|
|
if (item.ItemType != itemType)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string normalizedInclude = item.Include.NormalizePath();
|
|
|
|
|
2020-08-20 10:56:36 +00:00
|
|
|
var glob = MSBuildGlob.Parse(normalizedInclude);
|
2018-10-22 17:43:19 +00:00
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
// TODO Check somehow if path has no blob to avoid the following loop...
|
2018-10-22 17:43:19 +00:00
|
|
|
|
|
|
|
foreach (var existingFile in existingFiles)
|
|
|
|
{
|
|
|
|
if (glob.IsMatch(existingFile))
|
|
|
|
{
|
|
|
|
result.Add(existingFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
}
|
2019-07-10 19:53:49 +00:00
|
|
|
|
2020-06-23 19:01:54 +00:00
|
|
|
public static void EnsureHasProjectTypeGuids(MSBuildProject project)
|
|
|
|
{
|
|
|
|
var root = project.Root;
|
|
|
|
|
|
|
|
bool found = root.PropertyGroups.Any(pg =>
|
|
|
|
string.IsNullOrEmpty(pg.Condition) && pg.Properties.Any(p => p.Name == "ProjectTypeGuids"));
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return;
|
|
|
|
|
|
|
|
root.AddProperty("ProjectTypeGuids", ProjectGenerator.GodotDefaultProjectTypeGuids);
|
|
|
|
|
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:53:49 +00:00
|
|
|
/// Simple function to make sure the Api assembly references are configured correctly
|
2020-04-22 12:39:42 +00:00
|
|
|
public static void FixApiHintPath(MSBuildProject project)
|
2019-07-10 19:53:49 +00:00
|
|
|
{
|
2020-04-22 12:39:42 +00:00
|
|
|
var root = project.Root;
|
2019-07-10 19:53:49 +00:00
|
|
|
|
|
|
|
void AddPropertyIfNotPresent(string name, string condition, string value)
|
|
|
|
{
|
|
|
|
if (root.PropertyGroups
|
2020-05-10 20:56:35 +00:00
|
|
|
.Any(g => (string.IsNullOrEmpty(g.Condition) || g.Condition.Trim() == condition) &&
|
2019-07-10 19:53:49 +00:00
|
|
|
g.Properties
|
|
|
|
.Any(p => p.Name == name &&
|
|
|
|
p.Value == value &&
|
2020-03-14 18:01:29 +00:00
|
|
|
(p.Condition.Trim() == condition || g.Condition.Trim() == condition))))
|
2019-07-10 19:53:49 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-14 18:01:29 +00:00
|
|
|
root.AddProperty(name, value).Condition = " " + condition + " ";
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
2019-07-10 19:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddPropertyIfNotPresent(name: "ApiConfiguration",
|
2020-03-14 18:01:29 +00:00
|
|
|
condition: "'$(Configuration)' != 'ExportRelease'",
|
2019-07-10 19:53:49 +00:00
|
|
|
value: "Debug");
|
|
|
|
AddPropertyIfNotPresent(name: "ApiConfiguration",
|
2020-03-14 18:01:29 +00:00
|
|
|
condition: "'$(Configuration)' == 'ExportRelease'",
|
2019-07-10 19:53:49 +00:00
|
|
|
value: "Release");
|
|
|
|
|
|
|
|
void SetReferenceHintPath(string referenceName, string condition, string hintPath)
|
|
|
|
{
|
|
|
|
foreach (var itemGroup in root.ItemGroups.Where(g =>
|
2020-03-14 18:01:29 +00:00
|
|
|
g.Condition.Trim() == string.Empty || g.Condition.Trim() == condition))
|
2019-07-10 19:53:49 +00:00
|
|
|
{
|
|
|
|
var references = itemGroup.Items.Where(item =>
|
|
|
|
item.ItemType == "Reference" &&
|
|
|
|
item.Include == referenceName &&
|
2020-03-14 18:01:29 +00:00
|
|
|
(item.Condition.Trim() == condition || itemGroup.Condition.Trim() == condition));
|
2019-07-10 19:53:49 +00:00
|
|
|
|
|
|
|
var referencesWithHintPath = references.Where(reference =>
|
|
|
|
reference.Metadata.Any(m => m.Name == "HintPath"));
|
|
|
|
|
|
|
|
if (referencesWithHintPath.Any(reference => reference.Metadata
|
|
|
|
.Any(m => m.Name == "HintPath" && m.Value == hintPath)))
|
|
|
|
{
|
|
|
|
// Found a Reference item with the right HintPath
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var referenceWithHintPath = referencesWithHintPath.FirstOrDefault();
|
|
|
|
if (referenceWithHintPath != null)
|
|
|
|
{
|
|
|
|
// Found a Reference item with a wrong HintPath
|
|
|
|
foreach (var metadata in referenceWithHintPath.Metadata.ToList()
|
|
|
|
.Where(m => m.Name == "HintPath"))
|
|
|
|
{
|
|
|
|
// Safe to remove as we duplicate with ToList() to loop
|
|
|
|
referenceWithHintPath.RemoveChild(metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
referenceWithHintPath.AddMetadata("HintPath", hintPath);
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
2019-07-10 19:53:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var referenceWithoutHintPath = references.FirstOrDefault();
|
|
|
|
if (referenceWithoutHintPath != null)
|
|
|
|
{
|
|
|
|
// Found a Reference item without a HintPath
|
|
|
|
referenceWithoutHintPath.AddMetadata("HintPath", hintPath);
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
2019-07-10 19:53:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found no Reference item at all. Add it.
|
2020-03-14 18:01:29 +00:00
|
|
|
root.AddItem("Reference", referenceName).Condition = " " + condition + " ";
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
2019-07-10 19:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const string coreProjectName = "GodotSharp";
|
|
|
|
const string editorProjectName = "GodotSharpEditor";
|
|
|
|
|
|
|
|
const string coreCondition = "";
|
2020-03-14 18:01:29 +00:00
|
|
|
const string editorCondition = "'$(Configuration)' == 'Debug'";
|
2019-07-10 19:53:49 +00:00
|
|
|
|
|
|
|
var coreHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{coreProjectName}.dll";
|
|
|
|
var editorHintPath = $"$(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/{editorProjectName}.dll";
|
|
|
|
|
|
|
|
SetReferenceHintPath(coreProjectName, coreCondition, coreHintPath);
|
|
|
|
SetReferenceHintPath(editorProjectName, editorCondition, editorHintPath);
|
2020-03-14 18:01:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 12:39:42 +00:00
|
|
|
public static void MigrateFromOldConfigNames(MSBuildProject project)
|
2020-03-14 18:01:29 +00:00
|
|
|
{
|
2020-04-22 12:39:42 +00:00
|
|
|
var root = project.Root;
|
2020-03-14 18:01:29 +00:00
|
|
|
|
|
|
|
bool hasGodotProjectGeneratorVersion = false;
|
|
|
|
bool foundOldConfiguration = false;
|
|
|
|
|
2020-05-10 20:56:35 +00:00
|
|
|
foreach (var propertyGroup in root.PropertyGroups.Where(g => string.IsNullOrEmpty(g.Condition)))
|
2020-03-14 18:01:29 +00:00
|
|
|
{
|
|
|
|
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;
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
2020-03-14 18:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasGodotProjectGeneratorVersion)
|
|
|
|
{
|
2020-05-10 20:56:35 +00:00
|
|
|
root.PropertyGroups.First(g => string.IsNullOrEmpty(g.Condition))?
|
2020-03-14 18:01:29 +00:00
|
|
|
.AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
2020-03-14 18:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2020-04-23 14:12:22 +00:00
|
|
|
{
|
2020-03-14 18:01:29 +00:00
|
|
|
propertyGroup.Condition = " " + newCondition + " ";
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2020-03-14 18:01:29 +00:00
|
|
|
|
|
|
|
foreach (var propertyGroup in root.PropertyGroups)
|
|
|
|
{
|
|
|
|
foreach (var prop in propertyGroup.Properties.Where(p => p.Condition.Trim() == oldCondition))
|
2020-04-23 14:12:22 +00:00
|
|
|
{
|
2020-03-14 18:01:29 +00:00
|
|
|
prop.Condition = " " + newCondition + " ";
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2020-03-14 18:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var itemGroup in root.ItemGroups.Where(g => g.Condition.Trim() == oldCondition))
|
2020-04-23 14:12:22 +00:00
|
|
|
{
|
2020-03-14 18:01:29 +00:00
|
|
|
itemGroup.Condition = " " + newCondition + " ";
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2020-03-14 18:01:29 +00:00
|
|
|
|
|
|
|
foreach (var itemGroup in root.ItemGroups)
|
|
|
|
{
|
|
|
|
foreach (var item in itemGroup.Items.Where(item => item.Condition.Trim() == oldCondition))
|
2020-04-23 14:12:22 +00:00
|
|
|
{
|
2020-03-14 18:01:29 +00:00
|
|
|
item.Condition = " " + newCondition + " ";
|
2020-04-23 14:12:22 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2020-03-14 18:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-07-10 19:53:49 +00:00
|
|
|
}
|
2020-05-10 20:56:35 +00:00
|
|
|
|
|
|
|
public static void EnsureHasNugetNetFrameworkRefAssemblies(MSBuildProject project)
|
|
|
|
{
|
|
|
|
var root = project.Root;
|
|
|
|
|
|
|
|
bool found = root.ItemGroups.Any(g => string.IsNullOrEmpty(g.Condition) && g.Items.Any(
|
|
|
|
item => item.ItemType == "PackageReference" && item.Include == "Microsoft.NETFramework.ReferenceAssemblies"));
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var frameworkRefAssembliesItem = root.AddItem("PackageReference", "Microsoft.NETFramework.ReferenceAssemblies");
|
|
|
|
|
|
|
|
// Use metadata (child nodes) instead of attributes for the PackageReference.
|
|
|
|
// This is for compatibility with 3.2, where GodotTools uses an old Microsoft.Build.
|
|
|
|
frameworkRefAssembliesItem.AddMetadata("Version", "1.0.0");
|
|
|
|
frameworkRefAssembliesItem.AddMetadata("PrivateAssets", "All");
|
|
|
|
|
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|