2020-07-20 13:48:12 +00:00
|
|
|
using System;
|
2017-10-02 21:24:00 +00:00
|
|
|
using Microsoft.Build.Construction;
|
|
|
|
|
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
|
|
|
|
{
|
2021-12-28 22:25:16 +00: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-12 18:23:05 +00:00
|
|
|
|
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
|
|
|
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
|
|
|
|
2021-09-12 18:23:05 +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
|
|
|
}
|
|
|
|
}
|