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