2019-07-03 07:44:53 +00:00
|
|
|
using GodotTools.Core;
|
2017-10-02 21:24:00 +00:00
|
|
|
using System;
|
2019-07-03 07:44:53 +00:00
|
|
|
using System.Collections.Generic;
|
2017-10-02 21:24:00 +00:00
|
|
|
using System.IO;
|
2019-12-30 15:16:47 +00:00
|
|
|
using System.Reflection;
|
2017-10-02 21:24:00 +00:00
|
|
|
using Microsoft.Build.Construction;
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
namespace GodotTools.ProjectEditor
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
public static class ProjectGenerator
|
|
|
|
{
|
2019-07-03 07:44:53 +00:00
|
|
|
private const string CoreApiProjectName = "GodotSharp";
|
|
|
|
private const string EditorApiProjectName = "GodotSharpEditor";
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-06-23 19:01:54 +00:00
|
|
|
public const string CSharpProjectTypeGuid = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
|
|
|
|
public const string GodotProjectTypeGuid = "{8F3E2DF0-C35C-4265-82FC-BEA011F4A7ED}";
|
|
|
|
|
|
|
|
public static readonly string GodotDefaultProjectTypeGuids = $"{GodotProjectTypeGuid};{CSharpProjectTypeGuid}";
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
public static string GenGameProject(string dir, string name, IEnumerable<string> compileItems)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
string path = Path.Combine(dir, name + ".csproj");
|
|
|
|
|
|
|
|
ProjectPropertyGroupElement mainGroup;
|
2020-03-14 18:01:29 +00:00
|
|
|
var root = CreateLibraryProject(name, "Debug", out mainGroup);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-06-23 19:01:54 +00:00
|
|
|
mainGroup.SetProperty("ProjectTypeGuids", GodotDefaultProjectTypeGuids);
|
2017-10-02 21:24:00 +00:00
|
|
|
mainGroup.SetProperty("OutputPath", Path.Combine(".mono", "temp", "bin", "$(Configuration)"));
|
|
|
|
mainGroup.SetProperty("BaseIntermediateOutputPath", Path.Combine(".mono", "temp", "obj"));
|
|
|
|
mainGroup.SetProperty("IntermediateOutputPath", Path.Combine("$(BaseIntermediateOutputPath)", "$(Configuration)"));
|
2020-03-14 18:01:29 +00:00
|
|
|
mainGroup.SetProperty("ApiConfiguration", "Debug").Condition = " '$(Configuration)' != 'ExportRelease' ";
|
|
|
|
mainGroup.SetProperty("ApiConfiguration", "Release").Condition = " '$(Configuration)' == 'ExportRelease' ";
|
|
|
|
|
|
|
|
var debugGroup = root.AddPropertyGroup();
|
|
|
|
debugGroup.Condition = " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ";
|
|
|
|
debugGroup.AddProperty("DebugSymbols", "true");
|
|
|
|
debugGroup.AddProperty("DebugType", "portable");
|
|
|
|
debugGroup.AddProperty("Optimize", "false");
|
|
|
|
debugGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;DEBUG;TOOLS;");
|
|
|
|
debugGroup.AddProperty("ErrorReport", "prompt");
|
|
|
|
debugGroup.AddProperty("WarningLevel", "4");
|
|
|
|
debugGroup.AddProperty("ConsolePause", "false");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-11-08 00:05:19 +00:00
|
|
|
var coreApiRef = root.AddItem("Reference", CoreApiProjectName);
|
2019-07-03 07:44:53 +00:00
|
|
|
coreApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", "$(ApiConfiguration)", CoreApiProjectName + ".dll"));
|
2017-10-02 21:24:00 +00:00
|
|
|
coreApiRef.AddMetadata("Private", "False");
|
|
|
|
|
2018-11-08 00:05:19 +00:00
|
|
|
var editorApiRef = root.AddItem("Reference", EditorApiProjectName);
|
2020-03-14 18:01:29 +00:00
|
|
|
editorApiRef.Condition = " '$(Configuration)' == 'Debug' ";
|
2019-07-03 07:44:53 +00:00
|
|
|
editorApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", "$(ApiConfiguration)", EditorApiProjectName + ".dll"));
|
2017-10-02 21:24:00 +00:00
|
|
|
editorApiRef.AddMetadata("Private", "False");
|
|
|
|
|
|
|
|
GenAssemblyInfoFile(root, dir, name);
|
|
|
|
|
|
|
|
foreach (var item in compileItems)
|
|
|
|
{
|
|
|
|
root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\"));
|
|
|
|
}
|
|
|
|
|
|
|
|
root.Save(path);
|
|
|
|
|
|
|
|
return root.GetGuid().ToString().ToUpper();
|
|
|
|
}
|
|
|
|
|
2019-12-28 18:12:32 +00:00
|
|
|
private static void GenAssemblyInfoFile(ProjectRootElement root, string dir, string name, string[] assemblyLines = null, string[] usingDirectives = null)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
string propertiesDir = Path.Combine(dir, "Properties");
|
|
|
|
if (!Directory.Exists(propertiesDir))
|
|
|
|
Directory.CreateDirectory(propertiesDir);
|
|
|
|
|
|
|
|
string usingDirectivesText = string.Empty;
|
|
|
|
|
|
|
|
if (usingDirectives != null)
|
|
|
|
{
|
|
|
|
foreach (var usingDirective in usingDirectives)
|
|
|
|
usingDirectivesText += "\nusing " + usingDirective + ";";
|
|
|
|
}
|
|
|
|
|
|
|
|
string assemblyLinesText = string.Empty;
|
|
|
|
|
|
|
|
if (assemblyLines != null)
|
2019-07-03 07:44:53 +00:00
|
|
|
assemblyLinesText += string.Join("\n", assemblyLines) + "\n";
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
string content = string.Format(AssemblyInfoTemplate, usingDirectivesText, name, assemblyLinesText);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
string assemblyInfoFile = Path.Combine(propertiesDir, "AssemblyInfo.cs");
|
|
|
|
|
|
|
|
File.WriteAllText(assemblyInfoFile, content);
|
|
|
|
|
|
|
|
root.AddItem("Compile", assemblyInfoFile.RelativeToPath(dir).Replace("/", "\\"));
|
|
|
|
}
|
|
|
|
|
2019-12-28 18:12:32 +00:00
|
|
|
public static ProjectRootElement CreateLibraryProject(string name, string defaultConfig, out ProjectPropertyGroupElement mainGroup)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2019-02-28 21:24:31 +00:00
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
throw new ArgumentException($"{nameof(name)} cannot be empty", nameof(name));
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
var root = ProjectRootElement.Create();
|
|
|
|
root.DefaultTargets = "Build";
|
|
|
|
|
|
|
|
mainGroup = root.AddPropertyGroup();
|
2019-12-28 18:12:32 +00:00
|
|
|
mainGroup.AddProperty("Configuration", defaultConfig).Condition = " '$(Configuration)' == '' ";
|
2017-10-02 21:24:00 +00:00
|
|
|
mainGroup.AddProperty("Platform", "AnyCPU").Condition = " '$(Platform)' == '' ";
|
|
|
|
mainGroup.AddProperty("ProjectGuid", "{" + Guid.NewGuid().ToString().ToUpper() + "}");
|
|
|
|
mainGroup.AddProperty("OutputType", "Library");
|
|
|
|
mainGroup.AddProperty("OutputPath", Path.Combine("bin", "$(Configuration)"));
|
2019-02-28 21:24:31 +00:00
|
|
|
mainGroup.AddProperty("RootNamespace", IdentifierUtils.SanitizeQualifiedIdentifier(name, allowEmptyIdentifiers: true));
|
2017-10-02 21:24:00 +00:00
|
|
|
mainGroup.AddProperty("AssemblyName", name);
|
2020-01-23 14:57:47 +00:00
|
|
|
mainGroup.AddProperty("TargetFrameworkVersion", "v4.7");
|
2019-12-30 15:16:47 +00:00
|
|
|
mainGroup.AddProperty("GodotProjectGeneratorVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-03-14 18:01:29 +00:00
|
|
|
var exportDebugGroup = root.AddPropertyGroup();
|
|
|
|
exportDebugGroup.Condition = " '$(Configuration)|$(Platform)' == 'ExportDebug|AnyCPU' ";
|
|
|
|
exportDebugGroup.AddProperty("DebugSymbols", "true");
|
|
|
|
exportDebugGroup.AddProperty("DebugType", "portable");
|
|
|
|
exportDebugGroup.AddProperty("Optimize", "false");
|
|
|
|
exportDebugGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;DEBUG;");
|
|
|
|
exportDebugGroup.AddProperty("ErrorReport", "prompt");
|
|
|
|
exportDebugGroup.AddProperty("WarningLevel", "4");
|
|
|
|
exportDebugGroup.AddProperty("ConsolePause", "false");
|
|
|
|
|
|
|
|
var exportReleaseGroup = root.AddPropertyGroup();
|
|
|
|
exportReleaseGroup.Condition = " '$(Configuration)|$(Platform)' == 'ExportRelease|AnyCPU' ";
|
|
|
|
exportReleaseGroup.AddProperty("DebugType", "portable");
|
|
|
|
exportReleaseGroup.AddProperty("Optimize", "true");
|
|
|
|
exportReleaseGroup.AddProperty("DefineConstants", "$(GodotDefineConstants);GODOT;");
|
|
|
|
exportReleaseGroup.AddProperty("ErrorReport", "prompt");
|
|
|
|
exportReleaseGroup.AddProperty("WarningLevel", "4");
|
|
|
|
exportReleaseGroup.AddProperty("ConsolePause", "false");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
// References
|
|
|
|
var referenceGroup = root.AddItemGroup();
|
|
|
|
referenceGroup.AddItem("Reference", "System");
|
2020-05-10 20:56:35 +00:00
|
|
|
var frameworkRefAssembliesItem = referenceGroup.AddItem("PackageReference", "Microsoft.NETFramework.ReferenceAssemblies");
|
|
|
|
|
|
|
|
// Use metadata (child nodes) instead of attributes for the PackageReference.
|
|
|
|
// This is for compatibility with 3.2, where GodotTools uses an old Microsoft.Build.
|
|
|
|
frameworkRefAssembliesItem.AddMetadata("Version", "1.0.0");
|
|
|
|
frameworkRefAssembliesItem.AddMetadata("PrivateAssets", "All");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
root.AddImport(Path.Combine("$(MSBuildBinPath)", "Microsoft.CSharp.targets").Replace("/", "\\"));
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
private const string AssemblyInfoTemplate =
|
|
|
|
@"using System.Reflection;{0}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
// Information about this assembly is defined by the following attributes.
|
|
|
|
// Change them to the values specific to your project.
|
|
|
|
|
|
|
|
[assembly: AssemblyTitle(""{1}"")]
|
|
|
|
[assembly: AssemblyDescription("""")]
|
|
|
|
[assembly: AssemblyConfiguration("""")]
|
|
|
|
[assembly: AssemblyCompany("""")]
|
|
|
|
[assembly: AssemblyProduct("""")]
|
|
|
|
[assembly: AssemblyCopyright("""")]
|
|
|
|
[assembly: AssemblyTrademark("""")]
|
|
|
|
[assembly: AssemblyCulture("""")]
|
|
|
|
|
|
|
|
// The assembly version has the format ""{{Major}}.{{Minor}}.{{Build}}.{{Revision}}"".
|
|
|
|
// The form ""{{Major}}.{{Minor}}.*"" will automatically update the build and revision,
|
|
|
|
// and ""{{Major}}.{{Minor}}.{{Build}}.*"" will update just the revision.
|
|
|
|
|
|
|
|
[assembly: AssemblyVersion(""1.0.*"")]
|
|
|
|
|
|
|
|
// The following attributes are used to specify the signing key for the assembly,
|
|
|
|
// if desired. See the Mono documentation for more information about signing.
|
|
|
|
|
|
|
|
//[assembly: AssemblyDelaySign(false)]
|
|
|
|
//[assembly: AssemblyKeyFile("""")]
|
|
|
|
{2}";
|
|
|
|
}
|
|
|
|
}
|