2017-10-02 21:24:00 +00:00
|
|
|
using System;
|
2021-12-28 22:25:16 +00:00
|
|
|
using System.Globalization;
|
2017-10-02 21:24:00 +00:00
|
|
|
using System.IO;
|
2020-08-20 20:39:44 +00:00
|
|
|
using System.Text;
|
2017-10-02 21:24:00 +00:00
|
|
|
using Microsoft.Build.Construction;
|
2020-07-20 13:48:12 +00:00
|
|
|
using Microsoft.Build.Evaluation;
|
2020-10-23 07:25:16 +00:00
|
|
|
using GodotTools.Shared;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
namespace GodotTools.ProjectEditor
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
public static class ProjectGenerator
|
|
|
|
{
|
2020-10-23 07:25:16 +00:00
|
|
|
public static string GodotSdkAttrValue => $"Godot.NET.Sdk/{GeneratedGodotNupkgsVersions.GodotNETSdk}";
|
2020-06-15 19:29:16 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
public static ProjectRootElement GenGameProject(string name)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2020-07-20 13:48:12 +00:00
|
|
|
if (name.Length == 0)
|
|
|
|
throw new ArgumentException("Project name is empty", nameof(name));
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
var root = ProjectRootElement.Create(NewProjectFileOptions.None);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
root.Sdk = GodotSdkAttrValue;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
var mainGroup = root.AddPropertyGroup();
|
2022-02-27 20:57:50 +00:00
|
|
|
mainGroup.AddProperty("TargetFramework", "net6.0");
|
2021-09-12 18:23:05 +00:00
|
|
|
mainGroup.AddProperty("EnableDynamicLoading", "true");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
string sanitizedName = IdentifierUtils.SanitizeQualifiedIdentifier(name, allowEmptyIdentifiers: true);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
// If the name is not a valid namespace, manually set RootNamespace to a sanitized one.
|
|
|
|
if (sanitizedName != name)
|
|
|
|
mainGroup.AddProperty("RootNamespace", sanitizedName);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
return root;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
public static string GenAndSaveGameProject(string dir, string name)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2020-07-20 13:48:12 +00:00
|
|
|
if (name.Length == 0)
|
|
|
|
throw new ArgumentException("Project name is empty", nameof(name));
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
string path = Path.Combine(dir, name + ".csproj");
|
2020-05-10 20:56:35 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
var root = GenGameProject(name);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-08-20 20:39:44 +00:00
|
|
|
// Save (without BOM)
|
|
|
|
root.Save(path, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-12-28 22:25:16 +00:00
|
|
|
return Guid.NewGuid().ToString().ToUpperInvariant();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|