ProjectGenerator.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using GodotTools.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using Microsoft.Build.Construction;
  7. namespace GodotTools.ProjectEditor
  8. {
  9. public static class ProjectGenerator
  10. {
  11. private const string CoreApiProjectName = "GodotSharp";
  12. private const string EditorApiProjectName = "GodotSharpEditor";
  13. public static string GenGameProject(string dir, string name, IEnumerable<string> compileItems)
  14. {
  15. string path = Path.Combine(dir, name + ".csproj");
  16. ProjectPropertyGroupElement mainGroup;
  17. var root = CreateLibraryProject(name, "Debug", out mainGroup);
  18. mainGroup.SetProperty("OutputPath", Path.Combine(".mono", "temp", "bin", "$(Configuration)"));
  19. mainGroup.SetProperty("BaseIntermediateOutputPath", Path.Combine(".mono", "temp", "obj"));
  20. mainGroup.SetProperty("IntermediateOutputPath", Path.Combine("$(BaseIntermediateOutputPath)", "$(Configuration)"));
  21. mainGroup.SetProperty("ApiConfiguration", "Debug").Condition = " '$(Configuration)' != 'ExportRelease' ";
  22. mainGroup.SetProperty("ApiConfiguration", "Release").Condition = " '$(Configuration)' == 'ExportRelease' ";
  23. var debugGroup = root.AddPropertyGroup();
  24. debugGroup.Condition = " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ";
  25. debugGroup.AddProperty("DebugSymbols", "true");
  26. debugGroup.AddProperty("DebugType", "portable");
  27. debugGroup.AddProperty("Optimize", "false");
  28. debugGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;DEBUG;TOOLS;");
  29. debugGroup.AddProperty("ErrorReport", "prompt");
  30. debugGroup.AddProperty("WarningLevel", "4");
  31. debugGroup.AddProperty("ConsolePause", "false");
  32. var coreApiRef = root.AddItem("Reference", CoreApiProjectName);
  33. coreApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", "$(ApiConfiguration)", CoreApiProjectName + ".dll"));
  34. coreApiRef.AddMetadata("Private", "False");
  35. var editorApiRef = root.AddItem("Reference", EditorApiProjectName);
  36. editorApiRef.Condition = " '$(Configuration)' == 'Debug' ";
  37. editorApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", "$(ApiConfiguration)", EditorApiProjectName + ".dll"));
  38. editorApiRef.AddMetadata("Private", "False");
  39. GenAssemblyInfoFile(root, dir, name);
  40. foreach (var item in compileItems)
  41. {
  42. root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\"));
  43. }
  44. root.Save(path);
  45. return root.GetGuid().ToString().ToUpper();
  46. }
  47. private static void GenAssemblyInfoFile(ProjectRootElement root, string dir, string name, string[] assemblyLines = null, string[] usingDirectives = null)
  48. {
  49. string propertiesDir = Path.Combine(dir, "Properties");
  50. if (!Directory.Exists(propertiesDir))
  51. Directory.CreateDirectory(propertiesDir);
  52. string usingDirectivesText = string.Empty;
  53. if (usingDirectives != null)
  54. {
  55. foreach (var usingDirective in usingDirectives)
  56. usingDirectivesText += "\nusing " + usingDirective + ";";
  57. }
  58. string assemblyLinesText = string.Empty;
  59. if (assemblyLines != null)
  60. assemblyLinesText += string.Join("\n", assemblyLines) + "\n";
  61. string content = string.Format(AssemblyInfoTemplate, usingDirectivesText, name, assemblyLinesText);
  62. string assemblyInfoFile = Path.Combine(propertiesDir, "AssemblyInfo.cs");
  63. File.WriteAllText(assemblyInfoFile, content);
  64. root.AddItem("Compile", assemblyInfoFile.RelativeToPath(dir).Replace("/", "\\"));
  65. }
  66. public static ProjectRootElement CreateLibraryProject(string name, string defaultConfig, out ProjectPropertyGroupElement mainGroup)
  67. {
  68. if (string.IsNullOrEmpty(name))
  69. throw new ArgumentException($"{nameof(name)} cannot be empty", nameof(name));
  70. var root = ProjectRootElement.Create();
  71. root.DefaultTargets = "Build";
  72. mainGroup = root.AddPropertyGroup();
  73. mainGroup.AddProperty("Configuration", defaultConfig).Condition = " '$(Configuration)' == '' ";
  74. mainGroup.AddProperty("Platform", "AnyCPU").Condition = " '$(Platform)' == '' ";
  75. mainGroup.AddProperty("ProjectGuid", "{" + Guid.NewGuid().ToString().ToUpper() + "}");
  76. mainGroup.AddProperty("OutputType", "Library");
  77. mainGroup.AddProperty("OutputPath", Path.Combine("bin", "$(Configuration)"));
  78. mainGroup.AddProperty("RootNamespace", IdentifierUtils.SanitizeQualifiedIdentifier(name, allowEmptyIdentifiers: true));
  79. mainGroup.AddProperty("AssemblyName", name);
  80. mainGroup.AddProperty("TargetFrameworkVersion", "v4.7");
  81. mainGroup.AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString());
  82. var exportDebugGroup = root.AddPropertyGroup();
  83. exportDebugGroup.Condition = " '$(Configuration)|$(Platform)' == 'ExportDebug|AnyCPU' ";
  84. exportDebugGroup.AddProperty("DebugSymbols", "true");
  85. exportDebugGroup.AddProperty("DebugType", "portable");
  86. exportDebugGroup.AddProperty("Optimize", "false");
  87. exportDebugGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;DEBUG;");
  88. exportDebugGroup.AddProperty("ErrorReport", "prompt");
  89. exportDebugGroup.AddProperty("WarningLevel", "4");
  90. exportDebugGroup.AddProperty("ConsolePause", "false");
  91. var exportReleaseGroup = root.AddPropertyGroup();
  92. exportReleaseGroup.Condition = " '$(Configuration)|$(Platform)' == 'ExportRelease|AnyCPU' ";
  93. exportReleaseGroup.AddProperty("DebugType", "portable");
  94. exportReleaseGroup.AddProperty("Optimize", "true");
  95. exportReleaseGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;");
  96. exportReleaseGroup.AddProperty("ErrorReport", "prompt");
  97. exportReleaseGroup.AddProperty("WarningLevel", "4");
  98. exportReleaseGroup.AddProperty("ConsolePause", "false");
  99. // References
  100. var referenceGroup = root.AddItemGroup();
  101. referenceGroup.AddItem("Reference", "System");
  102. var frameworkRefAssembliesItem = referenceGroup.AddItem("PackageReference", "Microsoft.NETFramework.ReferenceAssemblies");
  103. // Use metadata (child nodes) instead of attributes for the PackageReference.
  104. // This is for compatibility with 3.2, where GodotTools uses an old Microsoft.Build.
  105. frameworkRefAssembliesItem.AddMetadata("Version", "1.0.0");
  106. frameworkRefAssembliesItem.AddMetadata("PrivateAssets", "All");
  107. root.AddImport(Path.Combine("$(MSBuildBinPath)", "Microsoft.CSharp.targets").Replace("/", "\\"));
  108. return root;
  109. }
  110. private const string AssemblyInfoTemplate =
  111. @"using System.Reflection;{0}
  112. // Information about this assembly is defined by the following attributes.
  113. // Change them to the values specific to your project.
  114. [assembly: AssemblyTitle(""{1}"")]
  115. [assembly: AssemblyDescription("""")]
  116. [assembly: AssemblyConfiguration("""")]
  117. [assembly: AssemblyCompany("""")]
  118. [assembly: AssemblyProduct("""")]
  119. [assembly: AssemblyCopyright("""")]
  120. [assembly: AssemblyTrademark("""")]
  121. [assembly: AssemblyCulture("""")]
  122. // The assembly version has the format ""{{Major}}.{{Minor}}.{{Build}}.{{Revision}}"".
  123. // The form ""{{Major}}.{{Minor}}.*"" will automatically update the build and revision,
  124. // and ""{{Major}}.{{Minor}}.{{Build}}.*"" will update just the revision.
  125. [assembly: AssemblyVersion(""1.0.*"")]
  126. // The following attributes are used to specify the signing key for the assembly,
  127. // if desired. See the Mono documentation for more information about signing.
  128. //[assembly: AssemblyDelaySign(false)]
  129. //[assembly: AssemblyKeyFile("""")]
  130. {2}";
  131. }
  132. }