ProjectGenerator.cs 7.4 KB

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