2020-07-20 21:48:12 +08:00
|
|
|
using System;
|
2023-10-14 20:07:45 +08:00
|
|
|
using System.Linq;
|
2017-10-03 05:24:00 +08:00
|
|
|
using Microsoft.Build.Construction;
|
2023-10-14 20:07:45 +08:00
|
|
|
using Microsoft.Build.Locator;
|
2017-10-03 05:24:00 +08:00
|
|
|
|
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
|
|
|
|
{
|
2023-10-14 20:07:45 +08:00
|
|
|
public static void MSBuildLocatorRegisterLatest(out Version version, out string path)
|
2021-12-29 06:25:16 +08:00
|
|
|
{
|
2023-10-14 20:07:45 +08:00
|
|
|
var instance = MSBuildLocator.QueryVisualStudioInstances()
|
|
|
|
.OrderByDescending(x => x.Version)
|
|
|
|
.First();
|
|
|
|
MSBuildLocator.RegisterInstance(instance);
|
2021-12-29 06:25:16 +08:00
|
|
|
version = instance.Version;
|
|
|
|
path = instance.MSBuildPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void MSBuildLocatorRegisterMSBuildPath(string msbuildPath)
|
2023-10-14 20:07:45 +08:00
|
|
|
=> MSBuildLocator.RegisterMSBuildPath(msbuildPath);
|
2021-09-13 02:23:05 +08:00
|
|
|
|
2024-01-13 12:24:12 +08:00
|
|
|
public static MSBuildProject? Open(string path)
|
2020-04-22 20:39:42 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|