2017-10-02 23:24:00 +02:00
|
|
|
using System;
|
|
|
|
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)
|
|
|
|
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();
|
|
|
|
mainGroup.AddProperty("TargetFramework", "netstandard2.1");
|
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)
|
|
|
|
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
|
|
|
|
2020-07-20 15:48:12 +02:00
|
|
|
return Guid.NewGuid().ToString().ToUpper();
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|