ProjectGenerator.cs 9.2 KB

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