2020-07-20 15:48:12 +02:00
|
|
|
using System;
|
2019-07-03 09:44:53 +02:00
|
|
|
using GodotTools.Core;
|
2018-10-22 19:43:19 +02:00
|
|
|
using System.Collections.Generic;
|
2019-07-10 21:53:49 +02:00
|
|
|
using System.Diagnostics;
|
2017-10-02 23:24:00 +02:00
|
|
|
using System.IO;
|
2019-07-10 21:53:49 +02:00
|
|
|
using System.Linq;
|
2017-10-02 23:24:00 +02:00
|
|
|
using Microsoft.Build.Construction;
|
2020-05-09 20:55:50 +02:00
|
|
|
using Microsoft.Build.Globbing;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2019-07-03 09:44:53 +02:00
|
|
|
namespace GodotTools.ProjectEditor
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-04-22 14:39:42 +02:00
|
|
|
public sealed class MSBuildProject
|
|
|
|
{
|
2020-07-20 15:48:12 +02:00
|
|
|
internal ProjectRootElement Root { get; set; }
|
2020-04-22 14:39:42 +02:00
|
|
|
|
2020-04-23 16:12:22 +02:00
|
|
|
public bool HasUnsavedChanges { get; set; }
|
2020-04-22 14:39:42 +02:00
|
|
|
|
|
|
|
public void Save() => Root.Save();
|
|
|
|
|
|
|
|
public MSBuildProject(ProjectRootElement root)
|
|
|
|
{
|
|
|
|
Root = root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
public static class ProjectUtils
|
|
|
|
{
|
2020-04-22 14:39:42 +02:00
|
|
|
public static MSBuildProject Open(string path)
|
|
|
|
{
|
|
|
|
var root = ProjectRootElement.Open(path);
|
|
|
|
return root != null ? new MSBuildProject(root) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
private static List<string> GetAllFilesRecursive(string rootDirectory, string mask)
|
2018-10-22 19:43:19 +02:00
|
|
|
{
|
|
|
|
string[] files = Directory.GetFiles(rootDirectory, mask, SearchOption.AllDirectories);
|
|
|
|
|
|
|
|
// We want relative paths
|
2019-07-10 21:53:49 +02:00
|
|
|
for (int i = 0; i < files.Length; i++)
|
|
|
|
{
|
2018-10-22 19:43:19 +02:00
|
|
|
files[i] = files[i].RelativeToPath(rootDirectory);
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
return new List<string>(files);
|
2018-10-22 19:43:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 15:48:12 +02: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 19:43:19 +02:00
|
|
|
{
|
2020-07-20 15:48:12 +02:00
|
|
|
var excluded = new List<string>();
|
|
|
|
var includedFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
|
2018-10-22 19:43:19 +02:00
|
|
|
|
|
|
|
var root = ProjectRootElement.Open(projectPath);
|
2020-03-14 19:01:29 +01:00
|
|
|
Debug.Assert(root != null);
|
2018-10-22 19:43:19 +02:00
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
foreach (var item in root.Items)
|
2018-10-22 19:43:19 +02:00
|
|
|
{
|
2020-07-20 15:48:12 +02:00
|
|
|
if (string.IsNullOrEmpty(item.Condition))
|
2018-10-22 19:43:19 +02:00
|
|
|
continue;
|
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
if (item.ItemType != itemType)
|
|
|
|
continue;
|
2018-10-22 19:43:19 +02:00
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
string normalizedExclude = item.Exclude.NormalizePath();
|
2018-10-22 19:43:19 +02:00
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
var glob = MSBuildGlob.Parse(normalizedExclude);
|
2018-10-22 19:43:19 +02:00
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
excluded.AddRange(includedFiles.Where(includedFile => glob.IsMatch(includedFile)));
|
2018-10-22 19:43:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
includedFiles.RemoveAll(f => excluded.Contains(f));
|
|
|
|
|
|
|
|
return includedFiles;
|
2018-10-22 19:43:19 +02:00
|
|
|
}
|
2019-07-10 21:53:49 +02:00
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
public static void MigrateToProjectSdksStyle(MSBuildProject project, string projectName)
|
2020-06-15 21:29:16 +02:00
|
|
|
{
|
2020-07-20 15:48:12 +02:00
|
|
|
var origRoot = project.Root;
|
2020-06-15 21:29:16 +02:00
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
if (!string.IsNullOrEmpty(origRoot.Sdk))
|
2020-06-15 21:29:16 +02:00
|
|
|
return;
|
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
project.Root = ProjectGenerator.GenGameProject(projectName);
|
|
|
|
project.Root.FullPath = origRoot.FullPath;
|
2020-06-15 21:29:16 +02:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
public static void EnsureGodotSdkIsUpToDate(MSBuildProject project)
|
2020-05-10 22:56:35 +02:00
|
|
|
{
|
|
|
|
var root = project.Root;
|
2020-07-20 15:48:12 +02:00
|
|
|
string godotSdkAttrValue = ProjectGenerator.GodotSdkAttrValue;
|
2020-05-10 22:56:35 +02:00
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
if (!string.IsNullOrEmpty(root.Sdk) && root.Sdk.Trim().Equals(godotSdkAttrValue, StringComparison.OrdinalIgnoreCase))
|
2020-05-10 22:56:35 +02:00
|
|
|
return;
|
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
root.Sdk = godotSdkAttrValue;
|
2020-05-10 22:56:35 +02:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
}
|