Debian Codegraph @

modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectGenerator.cs

raw symbols SCIP
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
9 namespace GodotTools.ProjectEditor
10 {
11 public static class ProjectGenerator
12 {
13 public static string GodotSdkAttrValue => $"Godot.NET.Sdk/{GeneratedGodotNupkgsVersions.GodotNETSdk}";
14
15 public static string GodotMinimumRequiredTfm => "net8.0";
16
17 public static ProjectRootElement GenGameProject(string name)
18 {
19 if (name.Length == 0)
20 throw new ArgumentException("Project name is empty.", nameof(name));
21
22 var root = ProjectRootElement.Create(NewProjectFileOptions.None);
23
24 root.Sdk = GodotSdkAttrValue;
25
26 var mainGroup = root.AddPropertyGroup();
27 mainGroup.AddProperty("TargetFramework", GodotMinimumRequiredTfm);
28
29 // Non-gradle builds require .NET 9 to match the jar libraries included in the export template.
30 var net9 = mainGroup.AddProperty("TargetFramework", "net9.0");
31 net9.Condition = " '$(GodotTargetPlatform)' == 'android' ";
32
33 mainGroup.AddProperty("EnableDynamicLoading", "true");
34
35 string sanitizedName = IdentifierUtils.SanitizeQualifiedIdentifier(name, allowEmptyIdentifiers: true);
36
37 // If the name is not a valid namespace, manually set RootNamespace to a sanitized one.
38 if (sanitizedName != name)
39 mainGroup.AddProperty("RootNamespace", sanitizedName);
40
41 return root;
42 }
43
44 public static string GenAndSaveGameProject(string dir, string name)
45 {
46 if (name.Length == 0)
47 throw new ArgumentException("Project name is empty.", nameof(name));
48
49 string path = Path.Combine(dir, name + ".csproj");
50
51 var root = GenGameProject(name);
52
53 // Save (without BOM)
54 root.Save(path, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
55
56 return Guid.NewGuid().ToString().ToUpperInvariant();
57 }
58 }
59 }