ProjectGenerator.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text;
  5. using Microsoft.Build.Construction;
  6. using Microsoft.Build.Evaluation;
  7. using GodotTools.Shared;
  8. namespace GodotTools.ProjectEditor
  9. {
  10. public static class ProjectGenerator
  11. {
  12. public static string GodotSdkAttrValue => $"Godot.NET.Sdk/{GeneratedGodotNupkgsVersions.GodotNETSdk}";
  13. public static string GodotMinimumRequiredTfm => "net8.0";
  14. public static ProjectRootElement GenGameProject(string name)
  15. {
  16. if (name.Length == 0)
  17. throw new ArgumentException("Project name is empty.", nameof(name));
  18. var root = ProjectRootElement.Create(NewProjectFileOptions.None);
  19. root.Sdk = GodotSdkAttrValue;
  20. var mainGroup = root.AddPropertyGroup();
  21. mainGroup.AddProperty("TargetFramework", GodotMinimumRequiredTfm);
  22. mainGroup.AddProperty("EnableDynamicLoading", "true");
  23. string sanitizedName = IdentifierUtils.SanitizeQualifiedIdentifier(name, allowEmptyIdentifiers: true);
  24. // If the name is not a valid namespace, manually set RootNamespace to a sanitized one.
  25. if (sanitizedName != name)
  26. mainGroup.AddProperty("RootNamespace", sanitizedName);
  27. return root;
  28. }
  29. public static string GenAndSaveGameProject(string dir, string name)
  30. {
  31. if (name.Length == 0)
  32. throw new ArgumentException("Project name is empty.", nameof(name));
  33. string path = Path.Combine(dir, name + ".csproj");
  34. var root = GenGameProject(name);
  35. // Save (without BOM)
  36. root.Save(path, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
  37. return Guid.NewGuid().ToString().ToUpperInvariant();
  38. }
  39. }
  40. }