ProjectGenerator.cs 9.2 KB

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