ProjectGenerator.cs 1.6 KB

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