ProjectGenerator.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. private const string CoreApiProjectGuid = "{AEBF0036-DA76-4341-B651-A3F2856AB2FA}";
  14. private const string EditorApiProjectGuid = "{8FBEC238-D944-4074-8548-B3B524305905}";
  15. public static string GenCoreApiProject(string dir, IEnumerable<string> compileItems)
  16. {
  17. string path = Path.Combine(dir, CoreApiProjectName + ".csproj");
  18. ProjectPropertyGroupElement mainGroup;
  19. var root = CreateLibraryProject(CoreApiProjectName, out mainGroup);
  20. mainGroup.AddProperty("DocumentationFile", Path.Combine("$(OutputPath)", "$(AssemblyName).xml"));
  21. mainGroup.SetProperty("RootNamespace", "Godot");
  22. mainGroup.SetProperty("ProjectGuid", CoreApiProjectGuid);
  23. mainGroup.SetProperty("BaseIntermediateOutputPath", "obj");
  24. GenAssemblyInfoFile(root, dir, CoreApiProjectName,
  25. new[] { "[assembly: InternalsVisibleTo(\"" + EditorApiProjectName + "\")]" },
  26. new[] { "System.Runtime.CompilerServices" });
  27. foreach (var item in compileItems)
  28. {
  29. root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\"));
  30. }
  31. root.Save(path);
  32. return CoreApiProjectGuid;
  33. }
  34. public static string GenEditorApiProject(string dir, string coreApiProjPath, IEnumerable<string> compileItems)
  35. {
  36. string path = Path.Combine(dir, EditorApiProjectName + ".csproj");
  37. ProjectPropertyGroupElement mainGroup;
  38. var root = CreateLibraryProject(EditorApiProjectName, out mainGroup);
  39. mainGroup.AddProperty("DocumentationFile", Path.Combine("$(OutputPath)", "$(AssemblyName).xml"));
  40. mainGroup.SetProperty("RootNamespace", "Godot");
  41. mainGroup.SetProperty("ProjectGuid", EditorApiProjectGuid);
  42. mainGroup.SetProperty("BaseIntermediateOutputPath", "obj");
  43. GenAssemblyInfoFile(root, dir, EditorApiProjectName);
  44. foreach (var item in compileItems)
  45. {
  46. root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\"));
  47. }
  48. var coreApiRef = root.AddItem("ProjectReference", coreApiProjPath.Replace("/", "\\"));
  49. coreApiRef.AddMetadata("Private", "False");
  50. root.Save(path);
  51. return EditorApiProjectGuid;
  52. }
  53. public static string GenGameProject(string dir, string name, IEnumerable<string> compileItems)
  54. {
  55. string path = Path.Combine(dir, name + ".csproj");
  56. ProjectPropertyGroupElement mainGroup;
  57. var root = CreateLibraryProject(name, out mainGroup);
  58. mainGroup.SetProperty("OutputPath", Path.Combine(".mono", "temp", "bin", "$(Configuration)"));
  59. mainGroup.SetProperty("BaseIntermediateOutputPath", Path.Combine(".mono", "temp", "obj"));
  60. mainGroup.SetProperty("IntermediateOutputPath", Path.Combine("$(BaseIntermediateOutputPath)", "$(Configuration)"));
  61. mainGroup.SetProperty("ApiConfiguration", "Debug").Condition = " '$(Configuration)' != 'Release' ";
  62. mainGroup.SetProperty("ApiConfiguration", "Release").Condition = " '$(Configuration)' == 'Release' ";
  63. var toolsGroup = root.AddPropertyGroup();
  64. toolsGroup.Condition = " '$(Configuration)|$(Platform)' == 'Tools|AnyCPU' ";
  65. toolsGroup.AddProperty("DebugSymbols", "true");
  66. toolsGroup.AddProperty("DebugType", "portable");
  67. toolsGroup.AddProperty("Optimize", "false");
  68. toolsGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;DEBUG;TOOLS;");
  69. toolsGroup.AddProperty("ErrorReport", "prompt");
  70. toolsGroup.AddProperty("WarningLevel", "4");
  71. toolsGroup.AddProperty("ConsolePause", "false");
  72. var coreApiRef = root.AddItem("Reference", CoreApiProjectName);
  73. coreApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", "$(ApiConfiguration)", CoreApiProjectName + ".dll"));
  74. coreApiRef.AddMetadata("Private", "False");
  75. var editorApiRef = root.AddItem("Reference", EditorApiProjectName);
  76. editorApiRef.Condition = " '$(Configuration)' == 'Tools' ";
  77. editorApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", "$(ApiConfiguration)", EditorApiProjectName + ".dll"));
  78. editorApiRef.AddMetadata("Private", "False");
  79. GenAssemblyInfoFile(root, dir, name);
  80. foreach (var item in compileItems)
  81. {
  82. root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\"));
  83. }
  84. root.Save(path);
  85. return root.GetGuid().ToString().ToUpper();
  86. }
  87. public static void GenAssemblyInfoFile(ProjectRootElement root, string dir, string name, string[] assemblyLines = null, string[] usingDirectives = null)
  88. {
  89. string propertiesDir = Path.Combine(dir, "Properties");
  90. if (!Directory.Exists(propertiesDir))
  91. Directory.CreateDirectory(propertiesDir);
  92. string usingDirectivesText = string.Empty;
  93. if (usingDirectives != null)
  94. {
  95. foreach (var usingDirective in usingDirectives)
  96. usingDirectivesText += "\nusing " + usingDirective + ";";
  97. }
  98. string assemblyLinesText = string.Empty;
  99. if (assemblyLines != null)
  100. assemblyLinesText += string.Join("\n", assemblyLines) + "\n";
  101. string content = string.Format(AssemblyInfoTemplate, usingDirectivesText, name, assemblyLinesText);
  102. string assemblyInfoFile = Path.Combine(propertiesDir, "AssemblyInfo.cs");
  103. File.WriteAllText(assemblyInfoFile, content);
  104. root.AddItem("Compile", assemblyInfoFile.RelativeToPath(dir).Replace("/", "\\"));
  105. }
  106. public static ProjectRootElement CreateLibraryProject(string name, out ProjectPropertyGroupElement mainGroup)
  107. {
  108. if (string.IsNullOrEmpty(name))
  109. throw new ArgumentException($"{nameof(name)} cannot be empty", nameof(name));
  110. var root = ProjectRootElement.Create();
  111. root.DefaultTargets = "Build";
  112. mainGroup = root.AddPropertyGroup();
  113. mainGroup.AddProperty("Configuration", "Debug").Condition = " '$(Configuration)' == '' ";
  114. mainGroup.AddProperty("Platform", "AnyCPU").Condition = " '$(Platform)' == '' ";
  115. mainGroup.AddProperty("ProjectGuid", "{" + Guid.NewGuid().ToString().ToUpper() + "}");
  116. mainGroup.AddProperty("OutputType", "Library");
  117. mainGroup.AddProperty("OutputPath", Path.Combine("bin", "$(Configuration)"));
  118. mainGroup.AddProperty("RootNamespace", IdentifierUtils.SanitizeQualifiedIdentifier(name, allowEmptyIdentifiers: true));
  119. mainGroup.AddProperty("AssemblyName", name);
  120. mainGroup.AddProperty("TargetFrameworkVersion", "v4.5");
  121. mainGroup.AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString());
  122. var debugGroup = root.AddPropertyGroup();
  123. debugGroup.Condition = " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ";
  124. debugGroup.AddProperty("DebugSymbols", "true");
  125. debugGroup.AddProperty("DebugType", "portable");
  126. debugGroup.AddProperty("Optimize", "false");
  127. debugGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;DEBUG;");
  128. debugGroup.AddProperty("ErrorReport", "prompt");
  129. debugGroup.AddProperty("WarningLevel", "4");
  130. debugGroup.AddProperty("ConsolePause", "false");
  131. var releaseGroup = root.AddPropertyGroup();
  132. releaseGroup.Condition = " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ";
  133. releaseGroup.AddProperty("DebugType", "portable");
  134. releaseGroup.AddProperty("Optimize", "true");
  135. releaseGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;");
  136. releaseGroup.AddProperty("ErrorReport", "prompt");
  137. releaseGroup.AddProperty("WarningLevel", "4");
  138. releaseGroup.AddProperty("ConsolePause", "false");
  139. // References
  140. var referenceGroup = root.AddItemGroup();
  141. referenceGroup.AddItem("Reference", "System");
  142. root.AddImport(Path.Combine("$(MSBuildBinPath)", "Microsoft.CSharp.targets").Replace("/", "\\"));
  143. return root;
  144. }
  145. private static void AddItems(ProjectRootElement elem, string groupName, params string[] items)
  146. {
  147. var group = elem.AddItemGroup();
  148. foreach (var item in items)
  149. {
  150. group.AddItem(groupName, item);
  151. }
  152. }
  153. private const string AssemblyInfoTemplate =
  154. @"using System.Reflection;{0}
  155. // Information about this assembly is defined by the following attributes.
  156. // Change them to the values specific to your project.
  157. [assembly: AssemblyTitle(""{1}"")]
  158. [assembly: AssemblyDescription("""")]
  159. [assembly: AssemblyConfiguration("""")]
  160. [assembly: AssemblyCompany("""")]
  161. [assembly: AssemblyProduct("""")]
  162. [assembly: AssemblyCopyright("""")]
  163. [assembly: AssemblyTrademark("""")]
  164. [assembly: AssemblyCulture("""")]
  165. // The assembly version has the format ""{{Major}}.{{Minor}}.{{Build}}.{{Revision}}"".
  166. // The form ""{{Major}}.{{Minor}}.*"" will automatically update the build and revision,
  167. // and ""{{Major}}.{{Minor}}.{{Build}}.*"" will update just the revision.
  168. [assembly: AssemblyVersion(""1.0.*"")]
  169. // The following attributes are used to specify the signing key for the assembly,
  170. // if desired. See the Mono documentation for more information about signing.
  171. //[assembly: AssemblyDelaySign(false)]
  172. //[assembly: AssemblyKeyFile("""")]
  173. {2}";
  174. }
  175. }