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