2020-07-20 21:48:12 +08:00
|
|
|
using System;
|
2017-10-03 05:24:00 +08:00
|
|
|
using Microsoft.Build.Construction;
|
|
|
|
|
2019-07-03 15:44:53 +08:00
|
|
|
namespace GodotTools.ProjectEditor
|
2017-10-03 05:24:00 +08:00
|
|
|
{
|
2020-04-22 20:39:42 +08:00
|
|
|
public sealed class MSBuildProject
|
|
|
|
{
|
2020-07-20 21:48:12 +08:00
|
|
|
internal ProjectRootElement Root { get; set; }
|
2020-04-22 20:39:42 +08:00
|
|
|
|
2020-04-23 22:12:22 +08:00
|
|
|
public bool HasUnsavedChanges { get; set; }
|
2020-04-22 20:39:42 +08:00
|
|
|
|
|
|
|
public void Save() => Root.Save();
|
|
|
|
|
|
|
|
public MSBuildProject(ProjectRootElement root)
|
|
|
|
{
|
|
|
|
Root = root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 05:24:00 +08:00
|
|
|
public static class ProjectUtils
|
|
|
|
{
|
2021-12-29 06:25:16 +08:00
|
|
|
public static void MSBuildLocatorRegisterDefaults(out Version version, out string path)
|
|
|
|
{
|
|
|
|
var instance = Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults();
|
|
|
|
version = instance.Version;
|
|
|
|
path = instance.MSBuildPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void MSBuildLocatorRegisterMSBuildPath(string msbuildPath)
|
|
|
|
=> Microsoft.Build.Locator.MSBuildLocator.RegisterMSBuildPath(msbuildPath);
|
2021-09-13 02:23:05 +08:00
|
|
|
|
2020-04-22 20:39:42 +08:00
|
|
|
public static MSBuildProject Open(string path)
|
|
|
|
{
|
|
|
|
var root = ProjectRootElement.Open(path);
|
|
|
|
return root != null ? new MSBuildProject(root) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-20 21:48:12 +08:00
|
|
|
public static void MigrateToProjectSdksStyle(MSBuildProject project, string projectName)
|
2020-06-16 03:29:16 +08:00
|
|
|
{
|
2020-07-20 21:48:12 +08:00
|
|
|
var origRoot = project.Root;
|
2020-06-16 03:29:16 +08:00
|
|
|
|
2020-07-20 21:48:12 +08:00
|
|
|
if (!string.IsNullOrEmpty(origRoot.Sdk))
|
2020-06-16 03:29:16 +08:00
|
|
|
return;
|
|
|
|
|
2020-07-20 21:48:12 +08:00
|
|
|
project.Root = ProjectGenerator.GenGameProject(projectName);
|
|
|
|
project.Root.FullPath = origRoot.FullPath;
|
2020-06-16 03:29:16 +08:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
|
|
|
|
2020-07-20 21:48:12 +08:00
|
|
|
public static void EnsureGodotSdkIsUpToDate(MSBuildProject project)
|
2020-05-11 04:56:35 +08:00
|
|
|
{
|
|
|
|
var root = project.Root;
|
2020-07-20 21:48:12 +08:00
|
|
|
string godotSdkAttrValue = ProjectGenerator.GodotSdkAttrValue;
|
2020-05-11 04:56:35 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
if (!string.IsNullOrEmpty(root.Sdk) &&
|
|
|
|
root.Sdk.Trim().Equals(godotSdkAttrValue, StringComparison.OrdinalIgnoreCase))
|
2020-05-11 04:56:35 +08:00
|
|
|
return;
|
|
|
|
|
2020-07-20 21:48:12 +08:00
|
|
|
root.Sdk = godotSdkAttrValue;
|
2020-05-11 04:56:35 +08:00
|
|
|
project.HasUnsavedChanges = true;
|
|
|
|
}
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
|
|
|
}
|