From 20f6b18dddda3cfc64a876df47d8ceabdd094bc2 Mon Sep 17 00:00:00 2001 From: Andrei Borodin Date: Fri, 17 Jan 2020 17:40:59 -0800 Subject: [PATCH 1/2] Reworked the platform props generation following the similar approach as for common props file. --- .../Exporters/IPlatformPropsExporter.cs | 58 ++++++++++ .../Exporters/IPlatformPropsExporter.cs.meta | 11 ++ .../Exporters/IUnityProjectExporter.cs | 17 ++- .../TemplatedCommonPropsExporter.cs | 51 +++------ .../TemplatedExporterBase.cs | 13 ++- .../TemplatedExporterPart.cs | 53 --------- .../TemplatedPlatformPropsExporter.cs | 55 ++++++++++ .../TemplatedPlatformPropsExporter.cs.meta | 11 ++ .../TemplatedUnityProjectExporter.cs | 82 ++------------ ...TemplatedWSAPlayerPlatformPropsExporter.cs | 37 +++++++ ...atedWSAPlayerPlatformPropsExporter.cs.meta | 11 ++ .../TemplatedExporter/TemplatedWriter.cs | 75 +++++++++++++ ...erPart.cs.meta => TemplatedWriter.cs.meta} | 0 .../ProjectGenerator/Scripts/MSBuildTools.cs | 28 ++--- .../Scripts/MSBuildUnityProjectExporter.cs | 101 ++++++++++++++++++ .../MSBuildUnityProjectExporter.cs.meta | 11 ++ .../Scripts/Templates/ITemplatePart.cs | 2 +- .../ProjectGenerator/Scripts/Utilities.cs | 14 +++ 18 files changed, 438 insertions(+), 192 deletions(-) create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs.meta delete mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterPart.cs create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs.meta create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs.meta create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWriter.cs rename Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/{TemplatedExporterPart.cs.meta => TemplatedWriter.cs.meta} (100%) create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs create mode 100644 Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs.meta diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs new file mode 100644 index 0000000..7f5648f --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#if UNITY_EDITOR + +using System; +using System.Collections.Generic; + +namespace Microsoft.Build.Unity.ProjectGeneration.Exporters +{ + /// + /// Exporter for the platform props file. + /// + public interface IPlatformPropsExporter + { + /// + /// Gets or sets the TargetFramework of the platform. + /// + string TargetFramework { get; set; } + + /// + /// Gets a set of define constants for the platform. + /// + HashSet DefineConstants { get; } + + /// + /// Gets a set of assembly search paths for the platform. + /// + HashSet AssemblySearchPaths { get; } + + /// + /// Gets a set of references for the platform. + /// + Dictionary References { get; } + + /// + /// Writes out the data. + /// + void Write(); + } + + /// + /// Specialized exporter for the Player|WSA platform. + /// + public interface IWSAPlayerPlatformPropsExporter : IPlatformPropsExporter + { + /// + /// Gets or sets the target UWP version. + /// + string TargetUWPVersion { get; set; } + + /// + /// Gets or sets the minimum UWP version. + /// + string MinimumUWPVersion { get; set; } + } +} +#endif \ No newline at end of file diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs.meta b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs.meta new file mode 100644 index 0000000..6cb398c --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IPlatformPropsExporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 64017fe8796a9024dbfec4ca146f0e73 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IUnityProjectExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IUnityProjectExporter.cs index 2fb569c..5328ede 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IUnityProjectExporter.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/IUnityProjectExporter.cs @@ -46,11 +46,20 @@ public interface IUnityProjectExporter ICommonPropsExporter CreateCommonPropsExporter(FileInfo path); /// - /// Exports the Common props file based on the given compilation platform and whether to export it as an In-Editor flavor vs Player. + /// Creates the platform props file exporter. /// - /// The platform to export. - /// True if this is an In-Editor flavor, false otherwise. - void ExportPlatformPropsFile(CompilationPlatformInfo platform, bool inEditorConfiguration); + /// The representing where this props file will be written. + /// The configuration for the platform props. + /// The unity platform for the platform props. + /// The scripting backend for the platform props. + IPlatformPropsExporter CreatePlatformPropsExporter(FileInfo path, string unityConfiguration, string unityPlatform, ScriptingBackend scriptingBackend); + + /// + /// Creates the specialized platform props file exporter for Player|WSA combination. + /// + /// The representing where this props file will be written. + /// The scripting backend for the platform props. + IWSAPlayerPlatformPropsExporter CreateWSAPlayerPlatformPropsExporter(FileInfo path, ScriptingBackend scriptingBackend); } } #endif diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedCommonPropsExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedCommonPropsExporter.cs index 26773cc..2261f40 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedCommonPropsExporter.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedCommonPropsExporter.cs @@ -16,52 +16,31 @@ internal class TemplatedCommonPropsExporter : TemplatedExporterBase, ICommonProp private const string GeneratedOutputDirectoryToken = "GENERATED_OUTPUT_DIRECTORY"; private const string UnityProjectAssetsDirectoryToken = "UNITY_PROJECT_ASSETS_PATH"; - private string unityMajorVersion; - private string unityMinorVersion; - private string currentUnityPlatform; - private string currentTargetFramework; - private DirectoryInfo unityProjectAssetsDirectory; - private DirectoryInfo generatedProjectOutputPath; + public string UnityMajorVersion { get; set; } - public string UnityMajorVersion - { - get => unityMajorVersion; - set => UpdateToken(ref unityMajorVersion, value, UnityMajorVersionToken); - } + public string UnityMinorVersion { get; set; } - public string UnityMinorVersion - { - get => unityMinorVersion; - set => UpdateToken(ref unityMinorVersion, value, UnityMinorVersionToken); - } + public string CurrentUnityPlatform { get; set; } - public string CurrentUnityPlatform - { - get => currentUnityPlatform; - set => UpdateToken(ref currentUnityPlatform, value, CurrentUnityPlatformToken); - } + public string CurrentTargetFramework { get; set; } - public string CurrentTargetFramework - { - get => currentTargetFramework; - set => UpdateToken(ref currentTargetFramework, value, CurrentTargetFrameworkToken); - } + public DirectoryInfo UnityProjectAssetsDirectory { get; set; } - public DirectoryInfo UnityProjectAssetsDirectory - { - get => unityProjectAssetsDirectory; - set => UpdateToken(ref unityProjectAssetsDirectory, value, UnityProjectAssetsDirectoryToken, t => t.FullName); - } + public DirectoryInfo GeneratedProjectOutputPath { get; set; } - public DirectoryInfo GeneratedProjectOutputPath + internal TemplatedCommonPropsExporter(FileTemplate fileTemplate, FileInfo exportPath) + : base(fileTemplate, exportPath) { - get => generatedProjectOutputPath; - set => UpdateToken(ref generatedProjectOutputPath, value, GeneratedOutputDirectoryToken, t => t.FullName); } - internal TemplatedCommonPropsExporter(FileTemplate fileTemplate, FileInfo exportPath) - : base(fileTemplate, exportPath) + protected override void Export(TemplatedWriter writer) { + writer.Write(UnityMajorVersionToken, UnityMajorVersion); + writer.Write(UnityMinorVersionToken, UnityMinorVersion); + writer.Write(CurrentUnityPlatformToken, CurrentUnityPlatform); + writer.Write(CurrentTargetFrameworkToken, CurrentTargetFramework); + writer.Write(UnityProjectAssetsDirectoryToken, UnityProjectAssetsDirectory.FullName); + writer.Write(GeneratedOutputDirectoryToken, GeneratedProjectOutputPath.FullName); } } } diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterBase.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterBase.cs index bf19dc8..1e76792 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterBase.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterBase.cs @@ -10,13 +10,12 @@ namespace Microsoft.Build.Unity.ProjectGeneration.Exporters.TemplatedExporter /// /// Base class for file based exporters. /// - internal class TemplatedExporterBase : TemplatedExporterPart + internal abstract class TemplatedExporterBase { private readonly FileTemplate templateFile; private readonly FileInfo exportPath; protected TemplatedExporterBase(FileTemplate templateFile, FileInfo exportPath) - : base(templateFile.Root, templateFile.Root.CreateReplacementSet()) { this.templateFile = templateFile; this.exportPath = exportPath; @@ -30,8 +29,18 @@ public void Write() // Ensure the parent directories are created Directory.CreateDirectory(exportPath.Directory.FullName); + TemplateReplacementSet replacementSet = templateFile.Root.CreateReplacementSet(); + + Export(new TemplatedWriter(templateFile.Root, replacementSet)); + templateFile.Write(exportPath.FullName, replacementSet); } + + /// + /// Override this method in a derived class to perform the export. + /// + /// The writer to use to export. + protected abstract void Export(TemplatedWriter writer); } } #endif \ No newline at end of file diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterPart.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterPart.cs deleted file mode 100644 index 6c37120..0000000 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterPart.cs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. See LICENSE in the project root for license information. - -#if UNITY_EDITOR -using Microsoft.Build.Unity.ProjectGeneration.Templates; -using System; - -namespace Microsoft.Build.Unity.ProjectGeneration.Exporters.TemplatedExporter -{ - /// - /// Base class for template based exporters. - /// - internal class TemplatedExporterPart - { - private readonly ITemplatePart template; - protected readonly TemplateReplacementSet replacementSet; - - protected TemplatedExporterPart(ITemplatePart template, TemplateReplacementSet replacementSet) - { - this.template = template; - this.replacementSet = replacementSet; - } - - /// - /// Updates the value of the field and the token (if the field has changed). - /// - /// The ref field to update. - /// The value to update to. - /// The token key. - protected void UpdateToken(ref string field, string value, string token) - { - UpdateToken(ref field, value, token, t => t); - } - - /// - /// Updates the value of the field and the token (if the field has changed). - /// - /// The generic type of the field and value. - /// The ref field to update. - /// The value to update to. - /// The token key. - /// The conversion to string value function. - protected void UpdateToken(ref T field, T value, string token, Func toStringFunc) - { - if (!Equals(field, value)) - { - field = value; - template.Tokens[token].AssignValue(replacementSet, toStringFunc(value)); - } - } - } -} -#endif \ No newline at end of file diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs new file mode 100644 index 0000000..e7762ea --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#if UNITY_EDITOR +using Microsoft.Build.Unity.ProjectGeneration.Templates; +using System; +using System.Collections.Generic; +using System.IO; + +namespace Microsoft.Build.Unity.ProjectGeneration.Exporters.TemplatedExporter +{ + /// + /// A class for exporting platform props using templates. + /// + internal class TemplatedPlatformPropsExporter : TemplatedExporterBase, IPlatformPropsExporter + { + private const string TargetFrameworkToken = "TARGET_FRAMEWORK"; + private const string DefineConstantsToken = "PLATFORM_COMMON_DEFINE_CONSTANTS"; + private const string AssemblySearchPathsToken = "PLATFORM_COMMON_ASSEMBLY_SEARCH_PATHS"; + + private const string CommonReferenceSubTemplate = "PLATFORM_COMMON_REFERENCE"; + private const string CommonReferencesSubTemplateReferenceToken = "REFERENCE"; + private const string CommonReferencesSubTemplateHintPathToken = "HINT_PATH"; + + public string TargetFramework { get; set; } + + public HashSet DefineConstants { get; } = new HashSet(); // Guess at default size + + public HashSet AssemblySearchPaths { get; } = new HashSet(); // Guess at default size + + public Dictionary References { get; } = new Dictionary(250); // Guess at default size + + public TemplatedPlatformPropsExporter(FileTemplate fileTemplate, FileInfo exportPath) + : base(fileTemplate, exportPath) + { + + } + + protected override void Export(TemplatedWriter writer) + { + writer.Write(TargetFrameworkToken, TargetFramework, optional: true); + + writer.Write(DefineConstantsToken, DefineConstants); + writer.Write(AssemblySearchPathsToken, AssemblySearchPaths); + + foreach (KeyValuePair reference in References) + { + TemplatedWriter subTemplateWriter = writer.CreateWriterFor(CommonReferenceSubTemplate); + subTemplateWriter.Write(CommonReferencesSubTemplateReferenceToken, reference.Key); + subTemplateWriter.Write(CommonReferencesSubTemplateHintPathToken, reference.Value.LocalPath); + } + } + } +} +#endif \ No newline at end of file diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs.meta b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs.meta new file mode 100644 index 0000000..01afe86 --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedPlatformPropsExporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d33e7e73b7c53904e94fe2d06582a8cf +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedUnityProjectExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedUnityProjectExporter.cs index 30bcd48..6bfc500 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedUnityProjectExporter.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedUnityProjectExporter.cs @@ -100,7 +100,7 @@ public string GetSolutionFilePath(UnityProjectInfo unityProjectInfo) { return Path.Combine(Utilities.AssetPath, $"{unityProjectInfo.UnityProjectName}.{MSBuildFileSuffix}.sln"); } - + /// public void ExportProject(UnityProjectInfo unityProjectInfo, CSProjectInfo projectInfo) { @@ -832,90 +832,24 @@ private void ProcessExtraSection(ITemplatePart extraSectionTemplate, TemplateRep } } - public void ExportPlatformPropsFile(CompilationPlatformInfo platform, bool inEditorConfiguration) + public IPlatformPropsExporter CreatePlatformPropsExporter(FileInfo path, string unityConfiguration, string unityPlatform, ScriptingBackend scriptingBackend) { - string configuration = inEditorConfiguration ? "InEditor" : "Player"; - - if (!FileTemplate.TryParseTemplate(TemplateFiles.Instance.GetTemplateFilePathForPlatform(platform.Name, configuration, platform.ScriptingBackend), out FileTemplate fileTemplate)) + if (!FileTemplate.TryParseTemplate(TemplateFiles.Instance.GetTemplateFilePathForPlatform(unityPlatform, unityConfiguration, scriptingBackend), out FileTemplate fileTemplate)) { throw new InvalidOperationException("Failed to parse template file for common props."); } - ITemplatePart rootPart = fileTemplate.Root; - TemplateReplacementSet rootReplacementSet = rootPart.CreateReplacementSet(); - - if (inEditorConfiguration) - { - ProcessPlatformTemplate(rootPart, rootReplacementSet, platform.Name, configuration, platform.BuildTarget, platform.TargetFramework, - platform.CommonPlatformReferences.Concat(platform.AdditionalInEditorReferences), - platform.CommonPlatformDefines.Concat(platform.AdditionalInEditorDefines)); - } - else - { - ProcessPlatformTemplate(rootPart, rootReplacementSet, platform.Name, configuration, platform.BuildTarget, platform.TargetFramework, - platform.CommonPlatformReferences.Concat(platform.AdditionalPlayerReferences), - platform.CommonPlatformDefines.Concat(platform.AdditionalPlayerDefines)); - } - - fileTemplate.Write(Path.Combine(generatedOutputFolder.FullName, $"{platform.Name}.{configuration}.props"), rootReplacementSet); + return new TemplatedPlatformPropsExporter(fileTemplate, path); } - private void ProcessPlatformTemplate(ITemplatePart rootPart, TemplateReplacementSet rootReplacementSet, string platformName, string configuration, BuildTarget buildTarget, TargetFramework targetFramework, IEnumerable references, IEnumerable defines, params HashSet[] priorToCheck) + public IWSAPlayerPlatformPropsExporter CreateWSAPlayerPlatformPropsExporter(FileInfo path, ScriptingBackend scriptingBackend) { - ProcessReferences(buildTarget, references, out HashSet platformAssemblySearchPaths, out HashSet platformAssemblyReferencePaths, priorToCheck); - - string minUWPPlatform = EditorUserBuildSettings.wsaMinUWPSDK; - if (string.IsNullOrWhiteSpace(minUWPPlatform) || new Version(minUWPPlatform) < MSBuildTools.DefaultMinUWPSDK) - { - minUWPPlatform = MSBuildTools.DefaultMinUWPSDK.ToString(); - } - - // This is a try replace because some may hardcode this value - rootPart.TryReplaceToken("TARGET_FRAMEWORK", rootReplacementSet, targetFramework.AsMSBuildString()); - - rootPart.Tokens["PLATFORM_COMMON_DEFINE_CONSTANTS"].AssignValue(rootReplacementSet, new DelimitedStringSet(";", defines)); - rootPart.Tokens["PLATFORM_COMMON_ASSEMBLY_SEARCH_PATHS"].AssignValue(rootReplacementSet, new DelimitedStringSet(";", platformAssemblySearchPaths)); - - // These are UWP specific, but they will be no-op if not needed - if (buildTarget == BuildTarget.WSAPlayer && configuration == "Player") - { - string targetUWPPlatform = EditorUserBuildSettings.wsaUWPSDK; - if (string.IsNullOrWhiteSpace(targetUWPPlatform)) - { - targetUWPPlatform = Utilities.GetUWPSDKs().Max().ToString(4); - } - rootPart.TryReplaceToken("UWP_TARGET_PLATFORM_VERSION", rootReplacementSet, targetUWPPlatform); - rootPart.TryReplaceToken("UWP_MIN_PLATFORM_VERSION", rootReplacementSet, minUWPPlatform); - } - - ITemplatePart platformCommonReferencePart = rootPart.Templates["PLATFORM_COMMON_REFERENCE"]; - foreach (string reference in platformAssemblyReferencePaths) + if (!FileTemplate.TryParseTemplate(TemplateFiles.Instance.GetTemplateFilePathForPlatform("WSA", "Player", scriptingBackend), out FileTemplate fileTemplate)) { - TemplateReplacementSet replacementSet = platformCommonReferencePart.CreateReplacementSet(rootReplacementSet); - platformCommonReferencePart.Tokens["REFERENCE"].AssignValue(replacementSet, Path.GetFileNameWithoutExtension(reference)); - platformCommonReferencePart.Tokens["HINT_PATH"].AssignValue(replacementSet, reference); + throw new InvalidOperationException("Failed to parse template file for common props."); } - } - - private void ProcessReferences(BuildTarget buildTarget, IEnumerable references, out HashSet searchPaths, out HashSet referenceNames, params HashSet[] priorToCheck) - { - searchPaths = new HashSet(); - referenceNames = new HashSet(); - foreach (string reference in references) - { - string directory = Path.GetDirectoryName(reference); - string fileName = Path.GetFileName(reference); - if (!priorToCheck.Any(t => t.Contains(directory))) // Don't add duplicates - { - searchPaths.Add(directory); - } - - if (!referenceNames.Add(reference)) - { - Debug.LogError($"Duplicate assembly reference found for platform '{buildTarget}' - {reference} ignoring."); - } - } + return new TemplatedWSAPlayerPlatformPropsExporter(fileTemplate, path); } private class ProjectConfigurationMapping diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs new file mode 100644 index 0000000..b754d9b --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#if UNITY_EDITOR +using Microsoft.Build.Unity.ProjectGeneration.Templates; +using System.IO; + +namespace Microsoft.Build.Unity.ProjectGeneration.Exporters.TemplatedExporter +{ + /// + /// The specialized platform props exporter form Player|WSA pair. + /// + internal class TemplatedWSAPlayerPlatformPropsExporter : TemplatedPlatformPropsExporter, IWSAPlayerPlatformPropsExporter + { + private const string TargetUWPVersionToken = "UWP_TARGET_PLATFORM_VERSION"; + private const string MinimumUWPVersionToken = "UWP_MIN_PLATFORM_VERSION"; + + public string TargetUWPVersion { get; set; } + + public string MinimumUWPVersion { get; set; } + + public TemplatedWSAPlayerPlatformPropsExporter(FileTemplate fileTemplate, FileInfo exportPath) + : base(fileTemplate, exportPath) + { + + } + + protected override void Export(TemplatedWriter writer) + { + base.Export(writer); + + writer.Write(TargetUWPVersionToken, TargetUWPVersion); + writer.Write(MinimumUWPVersionToken, MinimumUWPVersion); + } + } +} +#endif \ No newline at end of file diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs.meta b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs.meta new file mode 100644 index 0000000..ecc0a5f --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWSAPlayerPlatformPropsExporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9b3df1ecf0315014da24364b2b57d060 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWriter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWriter.cs new file mode 100644 index 0000000..7286ddf --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWriter.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#if UNITY_EDITOR +using Microsoft.Build.Unity.ProjectGeneration.Templates; +using System.Collections.Generic; + +namespace Microsoft.Build.Unity.ProjectGeneration.Exporters.TemplatedExporter +{ + /// + /// A writer helper for templates and tokens. + /// + internal ref struct TemplatedWriter + { + private readonly ITemplatePart template; + private readonly TemplateReplacementSet replacementSet; + + /// + /// Creates a new instance of the writer. + /// + internal TemplatedWriter(ITemplatePart template, TemplateReplacementSet replacementSet) + { + this.template = template; + this.replacementSet = replacementSet; + } + + /// + /// Writes the a set of items for the token using a seperator. Use this to avoid the expensive string concat at this stage. + /// + /// The token key. + /// Items to write. + /// The seperator, defaulted to ';'. + /// Whether this is an optional setting. + internal void Write(string token, IEnumerable items, string seperator = ";", bool optional = false) + { + Write(token, new DelimitedStringSet(seperator, items), optional); + } + + /// + /// Updates the value of the field and the token (if the field has changed). + /// + /// The token key. + /// The value to update to. + /// Whether this is an optional setting. + internal void Write(string token, string value, bool optional = false) + { + Write(token, (object)value, optional); + } + + /// + /// Creates a writer for a sub-template. + /// + /// The name of the sub-template. + /// + internal TemplatedWriter CreateWriterFor(string subTemplateName) + { + ITemplatePart subTemplate = template.Templates[subTemplateName]; + return new TemplatedWriter(subTemplate, subTemplate.CreateReplacementSet(replacementSet)); + } + + private void Write(string token, object value, bool optional) + { + if (optional) + { + template.TryReplaceToken(token, replacementSet, value); + } + else + { + template.Tokens[token].AssignValue(replacementSet, value); + } + } + + } +} +#endif \ No newline at end of file diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterPart.cs.meta b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWriter.cs.meta similarity index 100% rename from Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedExporterPart.cs.meta rename to Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Exporters/TemplatedExporter/TemplatedWriter.cs.meta diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildTools.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildTools.cs index 1841e68..dec5030 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildTools.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildTools.cs @@ -296,7 +296,7 @@ private static void RefreshGeneratedOutput(bool forceGenerateEverything) // - EditorPrefs currentBuildTarget or targetFramework is different from current ones (same as for executing a clean) if (shouldClean || !doesTokenFileExist) { - ExportCommonPropsFile(Exporter, CurrentPlayerPlatform); + MSBuildUnityProjectExporter.ExportCommonPropsFile(Exporter, CurrentPlayerPlatform); } // We regenerate everything if: @@ -328,13 +328,14 @@ private static void ExportCoreUnityPropFiles() foreach (CompilationPlatformInfo platform in UnityProject.AvailablePlatforms) { // Check for specialized template, otherwise get the common one - Exporter.ExportPlatformPropsFile(platform, true); - Exporter.ExportPlatformPropsFile(platform, false); + MSBuildUnityProjectExporter.ExportCoreUnityPropFile(Exporter, platform, true); + MSBuildUnityProjectExporter.ExportCoreUnityPropFile(Exporter, platform, false); } - Exporter.ExportPlatformPropsFile(UnityProject.EditorPlatform, true); + MSBuildUnityProjectExporter.ExportCoreUnityPropFile(Exporter, UnityProject.EditorPlatform, true); } + private static void RegenerateEverything(bool reparseUnityData) { Stopwatch stopwatch = new Stopwatch(); @@ -365,7 +366,7 @@ private static void RegenerateEverything(bool reparseUnityData) postCleanupAndCopyStamp = stopwatch.ElapsedMilliseconds; propsFileGenerationStart = stopwatch.ElapsedMilliseconds; - ExportCommonPropsFile(Exporter, UnityProject.CurrentPlayerPlatform); + MSBuildUnityProjectExporter.ExportCommonPropsFile(Exporter, UnityProject.CurrentPlayerPlatform); ExportCoreUnityPropFiles(); propsFileGenerationEnd = stopwatch.ElapsedMilliseconds; @@ -391,23 +392,6 @@ private static void RegenerateEverything(bool reparseUnityData) } } - private static void ExportCommonPropsFile(IUnityProjectExporter exporter, CompilationPlatformInfo currentPlayerPlatform) - { - ICommonPropsExporter propsExporter = exporter.CreateCommonPropsExporter(new FileInfo(Path.Combine(Utilities.ProjectPath, "MSBuildForUnity.Common.props"))); - - string[] versionParts = Application.unityVersion.Split('.'); - propsExporter.UnityMajorVersion = versionParts[0]; - propsExporter.UnityMinorVersion = versionParts[1]; - - propsExporter.GeneratedProjectOutputPath = new DirectoryInfo(Utilities.MSBuildOutputFolder); - propsExporter.UnityProjectAssetsDirectory = new DirectoryInfo(Utilities.AssetPath); - - propsExporter.CurrentTargetFramework = currentPlayerPlatform.TargetFramework.AsMSBuildString(); - propsExporter.CurrentUnityPlatform = currentPlayerPlatform.Name; - - propsExporter.Write(); - } - private static void GenerateBuildProjectsFile(string fileName, string solutionPath, IEnumerable compilationPlatforms) { string template = File.ReadAllText(TemplateFiles.Instance.BuildProjectsTemplatePath); diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs new file mode 100644 index 0000000..a84994d --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs @@ -0,0 +1,101 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#if UNITY_EDITOR +using Microsoft.Build.Unity.ProjectGeneration.Exporters; +using System; +using System.IO; +using System.Linq; +using UnityEditor; +using UnityEngine; + +namespace Microsoft.Build.Unity.ProjectGeneration +{ + /// + /// This class contains the export logic given info about the current Unity project. + /// + public static class MSBuildUnityProjectExporter + { + /// + /// Exports the core Unity props file. + /// + /// The overal exporter to use for creating exporters. + /// The compilation platform to export. + /// Whether to export the inEditor version. + public static void ExportCoreUnityPropFile(IUnityProjectExporter exporter, CompilationPlatformInfo platform, bool inEditor) + { + string configuration = inEditor ? "InEditor" : "Player"; + FileInfo outputFile = new FileInfo(Path.Combine(Utilities.MSBuildProjectFolder, $"{platform.Name}.{configuration}.props")); + + IPlatformPropsExporter propsExporter = (!inEditor && platform.BuildTarget == BuildTarget.WSAPlayer) + ? CreateWSAPlayerExporter(exporter, outputFile, platform.ScriptingBackend) + : exporter.CreatePlatformPropsExporter(outputFile, configuration, platform.Name, platform.ScriptingBackend); + + propsExporter.TargetFramework = platform.TargetFramework.AsMSBuildString(); + + propsExporter.DefineConstants.AddRange(platform.CommonPlatformDefines); + propsExporter.DefineConstants.AddRange(inEditor ? platform.AdditionalInEditorDefines : platform.AdditionalPlayerDefines); + + // Common references + foreach (string reference in platform.CommonPlatformReferences) + { + propsExporter.References[Path.GetFileNameWithoutExtension(reference)] = new Uri(reference); + propsExporter.AssemblySearchPaths.Add(Path.GetDirectoryName(reference)); + } + + // Additional references + foreach (string reference in (inEditor ? platform.AdditionalInEditorReferences : platform.AdditionalPlayerReferences)) + { + propsExporter.References[Path.GetFileNameWithoutExtension(reference)] = new Uri(reference); + propsExporter.AssemblySearchPaths.Add(Path.GetDirectoryName(reference)); + } + + propsExporter.Write(); + } + + /// + /// Exports the common MSBuildForUnity.Common.props file. + /// + /// The overal exporter to use for creating exporters. + /// Current unity platform. + public static void ExportCommonPropsFile(IUnityProjectExporter exporter, CompilationPlatformInfo currentPlayerPlatform) + { + ICommonPropsExporter propsExporter = exporter.CreateCommonPropsExporter(new FileInfo(Path.Combine(Utilities.ProjectPath, "MSBuildForUnity.Common.props"))); + + string[] versionParts = Application.unityVersion.Split('.'); + propsExporter.UnityMajorVersion = versionParts[0]; + propsExporter.UnityMinorVersion = versionParts[1]; + + propsExporter.GeneratedProjectOutputPath = new DirectoryInfo(Utilities.MSBuildOutputFolder); + propsExporter.UnityProjectAssetsDirectory = new DirectoryInfo(Utilities.AssetPath); + + propsExporter.CurrentTargetFramework = currentPlayerPlatform.TargetFramework.AsMSBuildString(); + propsExporter.CurrentUnityPlatform = currentPlayerPlatform.Name; + + propsExporter.Write(); + } + + private static IPlatformPropsExporter CreateWSAPlayerExporter(IUnityProjectExporter exporter, FileInfo outputFile, ScriptingBackend scriptingBackend) + { + IWSAPlayerPlatformPropsExporter uwpExporter = exporter.CreateWSAPlayerPlatformPropsExporter(outputFile, scriptingBackend); + + string minUWPPlatform = EditorUserBuildSettings.wsaMinUWPSDK; + if (string.IsNullOrWhiteSpace(minUWPPlatform) || new Version(minUWPPlatform) < MSBuildTools.DefaultMinUWPSDK) + { + minUWPPlatform = MSBuildTools.DefaultMinUWPSDK.ToString(); + } + + string targetUWPPlatform = EditorUserBuildSettings.wsaUWPSDK; + if (string.IsNullOrWhiteSpace(targetUWPPlatform)) + { + targetUWPPlatform = Utilities.GetUWPSDKs().Max().ToString(4); + } + + uwpExporter.MinimumUWPVersion = minUWPPlatform; + uwpExporter.TargetUWPVersion = targetUWPPlatform; + + return uwpExporter; + } + } +} +#endif \ No newline at end of file diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs.meta b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs.meta new file mode 100644 index 0000000..d9afc65 --- /dev/null +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fc5c14db9c3581c40b8a16ba1f46115d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Templates/ITemplatePart.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Templates/ITemplatePart.cs index 97d7aff..4415b12 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Templates/ITemplatePart.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Templates/ITemplatePart.cs @@ -42,7 +42,7 @@ public static class Extensions /// The replacement set where to encode the value. /// The value to encode. /// True if was able to locate the token. - public static bool TryReplaceToken(this ITemplatePart templatePart, string tokenName, TemplateReplacementSet replacementSet, string value) + public static bool TryReplaceToken(this ITemplatePart templatePart, string tokenName, TemplateReplacementSet replacementSet, object value) { if (templatePart.Tokens.TryGetValue(tokenName, out ITemplateToken templateToken)) { diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Utilities.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Utilities.cs index 5453cdf..ed05053 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Utilities.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/Utilities.cs @@ -246,6 +246,20 @@ public static string ReadUntil(this StreamReader reader, params string[] content return ReadWhile(reader, line => !contents.Any(c => line.Contains(c))); } + /// + /// Helper extension method to add all items to a collection. + /// + /// Type of item. + /// Collection to add items to. + /// The items to add. + public static void AddRange(this ICollection collection, IEnumerable items) + { + foreach (T item in items) + { + collection.Add(item); + } + } + /// /// A helper to check whether a DLL is a managed assembly. /// From 3252586a49728740f2b8fae725c9acefdd0ba203 Mon Sep 17 00:00:00 2001 From: Andrei Borodin Date: Fri, 17 Jan 2020 17:43:20 -0800 Subject: [PATCH 2/2] Fixed copyright --- .../ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs index a84994d..93f2c6b 100644 --- a/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs +++ b/Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildUnityProjectExporter.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. See LICENSE in the project root for license information. #if UNITY_EDITOR