2017-10-02 21:24:00 +00:00
|
|
|
using System;
|
|
|
|
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;
|
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
|
|
|
|
{
|
2021-03-17 13:46:20 +00:00
|
|
|
public const string GodotSdkVersionToUse = "3.3.0";
|
2020-12-05 23:56:47 +00:00
|
|
|
public const string GodotSdkNameToUse = "Godot.NET.Sdk";
|
2020-06-23 19:01:54 +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-12-05 23:56:47 +00:00
|
|
|
root.Sdk = $"{GodotSdkNameToUse}/{GodotSdkVersionToUse}";
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
var mainGroup = root.AddPropertyGroup();
|
|
|
|
mainGroup.AddProperty("TargetFramework", "net472");
|
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
|
|
|
|
2020-07-20 13:48:12 +00:00
|
|
|
return Guid.NewGuid().ToString().ToUpper();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|