2020-07-20 13:48:12 +00:00
|
|
|
using System;
|
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;
|
2017-10-02 21:24:00 +00:00
|
|
|
using Microsoft.Build.Construction;
|
2020-05-09 18:55:50 +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
|
|
|
|
{
|
2020-07-20 13:48:12 +00:00
|
|
|
internal ProjectRootElement Root { get; set; }
|
2020-04-22 12:39:42 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
private static List<string> GetAllFilesRecursive(string rootDirectory, string mask)
|
2018-10-22 17:43:19 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
return new List<string>(files);
|
2018-10-22 17:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
// NOTE: Assumes auto-including items. Only used by the scripts metadata generator, which will be replaced with source generators in the future.
|
|
|
|
public static IEnumerable<string> GetIncludeFiles(string projectPath, string itemType)
|
2018-10-22 17:43:19 +00:00
|
|
|
{
|
2020-07-20 13:48:12 +00:00
|
|
|
var excluded = new List<string>();
|
|
|
|
var includedFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
|
2018-10-22 17:43:19 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
foreach (var item in root.Items)
|
2018-10-22 17:43:19 +00:00
|
|
|
{
|
2020-07-20 13:48:12 +00:00
|
|
|
if (string.IsNullOrEmpty(item.Condition))
|
2018-10-22 17:43:19 +00:00
|
|
|
continue;
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
if (item.ItemType != itemType)
|
|
|
|
continue;
|
2018-10-22 17:43:19 +00:00
|
|
|
|
2020-09-05 11:29:04 +00:00
|
|
|
string normalizedRemove = item.Remove.NormalizePath();
|
2018-10-22 17:43:19 +00:00
|
|
|
|
2020-09-05 11:29:04 +00:00
|
|
|
var glob = MSBuildGlob.Parse(normalizedRemove);
|
2020-07-20 13:48:12 +00:00
|
|
|
excluded.AddRange(includedFiles.Where(includedFile => glob.IsMatch(includedFile)));
|
2018-10-22 17:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
includedFiles.RemoveAll(f => excluded.Contains(f));
|
|
|
|
|
|
|
|
return includedFiles;
|
2018-10-22 17:43:19 +00:00
|
|
|
}
|
2019-07-10 19:53:49 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
public static void MigrateToProjectSdksStyle(MSBuildProject project, string projectName)
|
2020-06-15 19:29:16 +00:00
|
|
|
{
|
2020-07-20 13:48:12 +00:00
|
|
|
var origRoot = project.Root;
|
2020-06-15 19:29:16 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
if (!string.IsNullOrEmpty(origRoot.Sdk))
|
2020-06-15 19:29:16 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
project.Root = ProjectGenerator.GenGameProject(projectName);
|
|
|
|
project.Root.FullPath = origRoot.FullPath;
|
2020-06-15 19:29:16 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
public static void EnsureGodotSdkIsUpToDate(MSBuildProject project)
|
2020-05-10 20:56:35 +00:00
|
|
|
{
|
|
|
|
var root = project.Root;
|
2020-07-20 13:48:12 +00:00
|
|
|
string godotSdkAttrValue = ProjectGenerator.GodotSdkAttrValue;
|
2020-05-10 20:56:35 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
if (!string.IsNullOrEmpty(root.Sdk) && root.Sdk.Trim().Equals(godotSdkAttrValue, StringComparison.OrdinalIgnoreCase))
|
2020-05-10 20:56:35 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
root.Sdk = godotSdkAttrValue;
|
2020-05-10 20:56:35 +00:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|