Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Samples/CrossUnityDependencies.Unity/MSBuild/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":3,"autoGenerateEnabled":true,"dependenciesProjectGuid":"f462fc4f-630c-4212-83ae-9208d85ad084","assemblyCSharpGuid":"35f18cf2-8f12-4108-b9f8-d34479756cd6","assemblyCSharpEditorGuid":"df8f1f7e-2119-4936-8901-2097aaf93cf4","assemblyCSharpFirstPassGuid":"cf1ec4f7-e2bb-4d55-949d-a039c5f1b4c2","assemblyCSharpFirstPassEditorGuid":"ac8eb4c9-8a89-4293-be50-54959b7aa5b7","builtInPackagesFolderGuid":"f7ce48f1-7bbc-4628-a728-6afec7c66b71","importedPackagesFolderGuid":"e6b8ba08-82d8-4266-93d4-0a9770d70a66","externalPackagesFolderGuid":"53c6b76f-fa9e-4bc0-9475-9a0944c6d7f5","solutionGuid":"79c75d6a-84d9-443a-98f8-4de7ac4365dc"}
{"version":3,"fullGenerationEnabled":true,"dependenciesProjectGuid":"f462fc4f-630c-4212-83ae-9208d85ad084","assemblyCSharpGuid":"35f18cf2-8f12-4108-b9f8-d34479756cd6","assemblyCSharpEditorGuid":"df8f1f7e-2119-4936-8901-2097aaf93cf4","assemblyCSharpFirstPassGuid":"cf1ec4f7-e2bb-4d55-949d-a039c5f1b4c2","assemblyCSharpFirstPassEditorGuid":"ac8eb4c9-8a89-4293-be50-54959b7aa5b7","builtInPackagesFolderGuid":"f7ce48f1-7bbc-4628-a728-6afec7c66b71","importedPackagesFolderGuid":"e6b8ba08-82d8-4266-93d4-0a9770d70a66","externalPackagesFolderGuid":"53c6b76f-fa9e-4bc0-9475-9a0944c6d7f5","solutionGuid":"79c75d6a-84d9-443a-98f8-4de7ac4365dc"}
2 changes: 1 addition & 1 deletion Samples/IntegratedDependencies.Unity/MSBuild/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":3,"autoGenerateEnabled":true,"dependenciesProjectGuid":"0d77ab82-d4eb-4034-ac54-4ca04d06906c","assemblyCSharpGuid":"4989d6c6-d8b3-4556-b4a9-06093ded68ad","assemblyCSharpEditorGuid":"0cd3f705-4fb4-4a60-8d09-89dc9f0b38a2","assemblyCSharpFirstPassGuid":"5c2fd87a-9877-4af1-ace8-1eed615617c0","assemblyCSharpFirstPassEditorGuid":"eea5290c-d0ca-4efa-b53c-69b3606c8bf2","builtInPackagesFolderGuid":"f78b56e6-cdfd-4bf5-9c51-6a8989cb7c6d","importedPackagesFolderGuid":"f0962e2e-3201-42a4-90a4-4bd07b8d0413","externalPackagesFolderGuid":"02cfe9b1-8003-483f-802b-f2acc2e2c82b","solutionGuid":"6e6cc69e-a194-4f01-9182-ce7b0f63f375"}
{"version":3,"fullGenerationEnabled":true,"dependenciesProjectGuid":"0d77ab82-d4eb-4034-ac54-4ca04d06906c","assemblyCSharpGuid":"4989d6c6-d8b3-4556-b4a9-06093ded68ad","assemblyCSharpEditorGuid":"0cd3f705-4fb4-4a60-8d09-89dc9f0b38a2","assemblyCSharpFirstPassGuid":"5c2fd87a-9877-4af1-ace8-1eed615617c0","assemblyCSharpFirstPassEditorGuid":"eea5290c-d0ca-4efa-b53c-69b3606c8bf2","builtInPackagesFolderGuid":"f78b56e6-cdfd-4bf5-9c51-6a8989cb7c6d","importedPackagesFolderGuid":"f0962e2e-3201-42a4-90a4-4bd07b8d0413","externalPackagesFolderGuid":"02cfe9b1-8003-483f-802b-f2acc2e2c82b","solutionGuid":"6e6cc69e-a194-4f01-9182-ce7b0f63f375"}
2 changes: 1 addition & 1 deletion Samples/SimpleNuGetDependency.Unity/MSBuild/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":3,"autoGenerateEnabled":false,"dependenciesProjectGuid":"e8876134-cce4-41b3-ab88-c3fbe5054847","assemblyCSharpGuid":"7bf3dda0-c67f-4423-8b23-f8ff7cbe5675","assemblyCSharpEditorGuid":"207db2b0-7820-489e-b53b-305058b18b5a","assemblyCSharpFirstPassGuid":"f64e1178-7b6f-4327-9507-ddadf821c230","assemblyCSharpFirstPassEditorGuid":"e5126e49-4984-43f0-90c5-978a9fd9ca84","builtInPackagesFolderGuid":"3c0655a2-0a72-4e5b-946f-00c3d7fad32a","importedPackagesFolderGuid":"7db99f50-7e71-432c-aae2-358a5bef69db","externalPackagesFolderGuid":"c1cc1a54-92d9-4d95-ac8f-1074bd46c449","solutionGuid":"3b4041bd-c118-4506-8256-1cb8ca716e78"}
{"version":3,"fullGenerationEnabled":false,"dependenciesProjectGuid":"e8876134-cce4-41b3-ab88-c3fbe5054847","assemblyCSharpGuid":"7bf3dda0-c67f-4423-8b23-f8ff7cbe5675","assemblyCSharpEditorGuid":"207db2b0-7820-489e-b53b-305058b18b5a","assemblyCSharpFirstPassGuid":"f64e1178-7b6f-4327-9507-ddadf821c230","assemblyCSharpFirstPassEditorGuid":"e5126e49-4984-43f0-90c5-978a9fd9ca84","builtInPackagesFolderGuid":"3c0655a2-0a72-4e5b-946f-00c3d7fad32a","importedPackagesFolderGuid":"7db99f50-7e71-432c-aae2-358a5bef69db","externalPackagesFolderGuid":"c1cc1a54-92d9-4d95-ac8f-1074bd46c449","solutionGuid":"3b4041bd-c118-4506-8256-1cb8ca716e78"}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSBuildForUnity" Version="[0.8.3-*, 0.8.3]">
<PackageReference Include="MSBuildForUnity" Version="[##MSB4U_VERSION_TOKEN##-*, ##MSB4U_VERSION_TOKEN##]">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<!-- We set these common properties for current MSBuild and Unity project, unless we are being invoked from a different MSBuild and Unity project -->
<PropertyGroup Condition="'$(MSBuildForUnityVersion)'==''">
<MSBuildForUnityVersion>0.8.3</MSBuildForUnityVersion>
<MSBuildForUnityVersion><!--MSB4U_VERSION_TOKEN--></MSBuildForUnityVersion>

<MSBuildForUnityPlatformPropsDirectory>$(_MSBuildForUnityGeneratedOutputDirectory)\Projects</MSBuildForUnityPlatformPropsDirectory>
<MSBuildForUnityBuildOutputDirectory>$(_MSBuildForUnityGeneratedOutputDirectory)\Output</MSBuildForUnityBuildOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

#if UNITY_EDITOR
using System;
using System.IO;

namespace Microsoft.Build.Unity.ProjectGeneration.Exporters
Expand All @@ -11,6 +12,11 @@ namespace Microsoft.Build.Unity.ProjectGeneration.Exporters
/// </summary>
public interface ICommonPropsExporter
{
/// <summary>
/// Gets or sets the current MSBuildForUnity version.
/// </summary>
Version MSBuildForUnityVersion { get; set; }

/// <summary>
/// Gets or sets the Unity major version.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public interface ITopLevelDependenciesProjectExporter
/// </summary>
Guid Guid { get; set; }

/// <summary>
/// Gets or sets the MSBuildForUnity version.
/// </summary>
Version MSBuildForUnityVersion { get; set; }

/// <summary>
/// Gets the set of references for this project.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

#if UNITY_EDITOR
using Microsoft.Build.Unity.ProjectGeneration.Templates;
using System;
using System.IO;

namespace Microsoft.Build.Unity.ProjectGeneration.Exporters.TemplatedExporter
{
internal class TemplatedCommonPropsExporter : ICommonPropsExporter
{
private const string MSBuildForUnityVersionToken = "MSB4U_VERSION";
private const string UnityMajorVersionToken = "UNITY_MAJOR_VERSION";
private const string UnityMinorVersionToken = "UNITY_MINOR_VERSION";
private const string UnityEditorInstallPathToken = "UNITY_EDITOR_INSTALL_FOLDER";
Expand All @@ -20,6 +22,8 @@ internal class TemplatedCommonPropsExporter : ICommonPropsExporter
private readonly FileTemplate fileTemplate;
private readonly FileInfo exportPath;

public Version MSBuildForUnityVersion { get; set; }

public string UnityMajorVersion { get; set; }

public string UnityMinorVersion { get; set; }
Expand All @@ -44,6 +48,7 @@ public void Write()
{
TemplatedWriter writer = new TemplatedWriter(fileTemplate);

writer.Write(MSBuildForUnityVersionToken, MSBuildForUnityVersion.ToString());
writer.Write(UnityMajorVersionToken, UnityMajorVersion);
writer.Write(UnityMinorVersionToken, UnityMinorVersion);
writer.Write(UnityEditorInstallPathToken, UnityEditorInstallPath.FullName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Microsoft.Build.Unity.ProjectGeneration.Exporters.TemplatedExporter
internal class TemplatedTopLevelDependenciesProjectExporter : ITopLevelDependenciesProjectExporter
{
private const string ProjectGuidToken = "PROJECT_GUID";
private const string MSBuildForUnityVersionToken = "MSB4U_VERSION";

private const string ProjectReferenceTemplate = "PROJECT_REFERENCE";
private const string ProjectReferenceTemplate_ReferenceToken = "REFERENCE";
Expand All @@ -32,6 +33,8 @@ internal class TemplatedTopLevelDependenciesProjectExporter : ITopLevelDependenc

public Guid Guid { get; set; }

public Version MSBuildForUnityVersion { get; set; }

public HashSet<ProjectReference> References { get; } = new HashSet<ProjectReference>();

public TemplatedTopLevelDependenciesProjectExporter(FileTemplate primaryTemplateFile, FileTemplate propsTemplateFile, FileTemplate targetsTemplateFile,
Expand All @@ -48,15 +51,15 @@ public TemplatedTopLevelDependenciesProjectExporter(FileTemplate primaryTemplate

public void Write()
{
TemplatedWriter propsWriter = new TemplatedWriter(propsTemplateFile);

propsWriter.Write(ProjectGuidToken, Guid);
TemplatedWriter propsWriter = new TemplatedWriter(propsTemplateFile)
.Write(ProjectGuidToken, Guid)
.Write(MSBuildForUnityVersionToken, MSBuildForUnityVersion.ToString());

foreach (ProjectReference projectReference in References)
{
TemplatedWriter referenceWriter = propsWriter.CreateWriterFor(ProjectReferenceTemplate);
referenceWriter.Write(ProjectReferenceTemplate_ReferenceToken, projectReference.ReferencePath.LocalPath);
referenceWriter.Write(ProjectReferenceTemplate_ConditionToken, projectReference.Condition ?? string.Empty);
TemplatedWriter referenceWriter = propsWriter.CreateWriterFor(ProjectReferenceTemplate)
.Write(ProjectReferenceTemplate_ReferenceToken, projectReference.ReferencePath.LocalPath)
.Write(ProjectReferenceTemplate_ConditionToken, projectReference.Condition ?? string.Empty);

if (projectReference.IsGenerated)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.Serialization;
using Debug = UnityEngine.Debug;

namespace Microsoft.Build.Unity.ProjectGeneration
Expand All @@ -25,8 +26,9 @@ public class MSBuildToolsConfig
[SerializeField]
private int version = 0;

[FormerlySerializedAs("autoGenerateEnabled")]
[SerializeField]
private bool autoGenerateEnabled = false;
private bool fullGenerationEnabled = false;

[SerializeField]
private string dependenciesProjectGuid = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -55,12 +57,12 @@ public class MSBuildToolsConfig
[SerializeField]
private string solutionGuid = Guid.NewGuid().ToString();

public bool AutoGenerateEnabled
public bool FullGenerationEnabled
{
get => autoGenerateEnabled;
get => fullGenerationEnabled;
set
{
autoGenerateEnabled = value;
fullGenerationEnabled = value;
Save();
}
}
Expand Down Expand Up @@ -177,9 +179,9 @@ public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget n
};

public const string CSharpVersion = "7.3";
public const string AutoGenerate = "MSBuild/Auto Generation Enabled";
public const string FullGeneration = "MSBuild/Full Generation Enabled";

private static readonly string TokenFilePath = Path.Combine(Utilities.ProjectPath, "Temp", "PropsGeneratedThisEditorInstance.token");
public static readonly Version MSBuildForUnityVersion = new Version(0, 8, 3);
public static readonly Version DefaultMinUWPSDK = new Version("10.0.14393.0");

private static UnityProjectInfo unityProjectInfo;
Expand All @@ -199,19 +201,19 @@ public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget n

public static MSBuildToolsConfig Config { get; } = MSBuildToolsConfig.Load();

[MenuItem(AutoGenerate, priority = 101)]
[MenuItem(FullGeneration, priority = 101)]
public static void ToggleAutoGenerate()
{
Config.AutoGenerateEnabled = !Config.AutoGenerateEnabled;
Menu.SetChecked(AutoGenerate, Config.AutoGenerateEnabled);
Config.FullGenerationEnabled = !Config.FullGenerationEnabled;
Menu.SetChecked(FullGeneration, Config.FullGenerationEnabled);
// If we just toggled on, regenerate everything
RefreshGeneratedOutput(forceGenerateEverything: true, forceCompleteGeneration: false);
}

[MenuItem(AutoGenerate, true, priority = 101)]
[MenuItem(FullGeneration, true, priority = 101)]
public static bool ToggleAutoGenerate_Validate()
{
Menu.SetChecked(AutoGenerate, Config.AutoGenerateEnabled);
Menu.SetChecked(FullGeneration, Config.FullGenerationEnabled);
return true;
}

Expand Down Expand Up @@ -275,58 +277,86 @@ private static void RefreshGeneratedOutput(bool forceGenerateEverything, bool fo
BuildTarget currentBuildTarget = EditorUserBuildSettings.activeBuildTarget;
ApiCompatibilityLevel targetFramework = PlayerSettings.GetApiCompatibilityLevel(EditorUserBuildSettings.selectedBuildTargetGroup);

bool shouldClean = EditorPrefs.GetInt($"{nameof(MSBuildTools)}.{nameof(currentBuildTarget)}") != (int)currentBuildTarget
bool buildTargetOrFrameworkChanged = EditorPrefs.GetInt($"{nameof(MSBuildTools)}.{nameof(currentBuildTarget)}") != (int)currentBuildTarget
|| EditorPrefs.GetInt($"{nameof(MSBuildTools)}.{nameof(targetFramework)}") != (int)targetFramework
|| forceGenerateEverything;

if (shouldClean)
if (buildTargetOrFrameworkChanged)
{
// We clean up previous build if the EditorPrefs currentBuildTarget or targetFramework is different from current ones.
MSBuildProjectBuilder.TryBuildAllProjects(MSBuildProjectBuilder.CleanProfileName);
}

bool doesTokenFileExist = File.Exists(TokenFilePath);
// Get the token file in the Unity Temp directory, if it exists.
Version tokenVerison = GetCurrentTokenVersion();

// We regenerate, if the token file exists, and it's current version.
bool doesCurrentVersionTokenFileExist = tokenVerison != null && tokenVerison == MSBuildForUnityVersion;

// We perform the regeneration of complete or partial pass in the following cases:
// - forceGenerateEverything is true (we are told to)
// - buildTargetOrFrameworkChanged is true (target framework changed)
// - doesCurrentVersionTokenFileExist is false (version changed, or editor just opened)

// We regenerate everything if:
// - We are forced to
// - AutoGenerateEnabled and token file doesn't exist or shouldClean is true
bool regenerateEverything = forceGenerateEverything || (Config.AutoGenerateEnabled && (!doesTokenFileExist || shouldClean));
bool performRegeneration = forceGenerateEverything || buildTargetOrFrameworkChanged || !doesCurrentVersionTokenFileExist;

if (regenerateEverything || unityProjectInfo == null)
if (performRegeneration || unityProjectInfo == null)
{
// Create the project info only if it's null or we need to regenerate
unityProjectInfo = new UnityProjectInfo(Debug.unityLogger, SupportedBuildTargets, Config, Config.AutoGenerateEnabled || forceCompleteGeneration);
unityProjectInfo = new UnityProjectInfo(Debug.unityLogger, SupportedBuildTargets, Config, Config.FullGenerationEnabled || forceCompleteGeneration);
}

// We regenerate the common "directory" props file under the following conditions:
// - Token file doesn't exist which means editor was just started
// - EditorPrefs currentBuildTarget or targetFramework is different from current ones (same as for executing a clean)
if (shouldClean || !doesTokenFileExist)
if (performRegeneration)
{
MSBuildUnityProjectExporter.ExportCommonPropsFile(Exporter, unityProjectInfo.CurrentPlayerPlatform);
// If we are forced complete, then we regenerate, otherwise perform the one that is selected
RegenerateEverything(unityProjectInfo, Config.FullGenerationEnabled || forceCompleteGeneration);
}

if (regenerateEverything)
if (!doesCurrentVersionTokenFileExist)
{
RegenerateEverything(unityProjectInfo, Config.AutoGenerateEnabled || forceCompleteGeneration);
}
foreach (string tokenFile in Directory.GetFiles(Path.Combine(Utilities.ProjectPath, "Temp"), "*_token.msb4u", SearchOption.TopDirectoryOnly))
{
File.Delete(tokenFile);
}

if (!doesTokenFileExist)
{
File.Create(TokenFilePath).Dispose();
File.Create(Path.Combine(Utilities.ProjectPath, "Temp", $"{MSBuildForUnityVersion.ToString(3)}_token.msb4u"))
.Dispose();
}

// Write the current targetframework and build target
EditorPrefs.SetInt($"{nameof(MSBuildTools)}.{nameof(currentBuildTarget)}", (int)currentBuildTarget);
EditorPrefs.SetInt($"{nameof(MSBuildTools)}.{nameof(targetFramework)}", (int)targetFramework);

// If we cleaned, now build
if (shouldClean)
if (buildTargetOrFrameworkChanged)
{
MSBuildProjectBuilder.TryBuildAllProjects(MSBuildProjectBuilder.BuildProfileName);
}
}

private static Version GetCurrentTokenVersion()
{
string[] file = Directory.GetFiles(Path.Combine(Utilities.ProjectPath, "Temp"), "*_token.msb4u", SearchOption.TopDirectoryOnly);

if (file.Length > 0)
{
string versionNumber = Path.GetFileNameWithoutExtension(file[0]).Split('_')[0];

string[] versionParts = versionNumber.Split('.');

if (versionParts.Length == 3
&& int.TryParse(versionParts[0], out int major)
&& int.TryParse(versionParts[1], out int minor)
&& int.TryParse(versionParts[2], out int patch))
{
return new Version(major, minor, patch);
}
}

return null;
}

private static void ExportCoreUnityPropFiles(UnityProjectInfo unityProjectInfo)
{
foreach (CompilationPlatformInfo platform in unityProjectInfo.AvailablePlatforms)
Expand Down Expand Up @@ -364,7 +394,7 @@ private static void RegenerateEverything(UnityProjectInfo unityProjectInfo, bool
postCleanupAndCopyStamp = stopwatch.ElapsedMilliseconds;

propsFileGenerationStart = stopwatch.ElapsedMilliseconds;
MSBuildUnityProjectExporter.ExportCommonPropsFile(Exporter, unityProjectInfo.CurrentPlayerPlatform);
MSBuildUnityProjectExporter.ExportCommonPropsFile(Exporter, MSBuildForUnityVersion, unityProjectInfo.CurrentPlayerPlatform);
if (completeGeneration)
{
ExportCoreUnityPropFiles(unityProjectInfo);
Expand All @@ -378,7 +408,7 @@ private static void RegenerateEverything(UnityProjectInfo unityProjectInfo, bool
unityProjectInfo.ExportSolution(Exporter, new FileInfo(Exporter.GetSolutionFilePath(unityProjectInfo)), directoryInfo);
unityProjectInfo.ExportProjects(Exporter, directoryInfo);
}
MSBuildUnityProjectExporter.ExportTopLevelDependenciesProject(Exporter, Config, new DirectoryInfo(Utilities.MSBuildProjectFolder), unityProjectInfo);
MSBuildUnityProjectExporter.ExportTopLevelDependenciesProject(Exporter, MSBuildForUnityVersion, Config, new DirectoryInfo(Utilities.MSBuildProjectFolder), unityProjectInfo);
solutionExportEnd = stopwatch.ElapsedMilliseconds;

foreach (string otherFile in TemplateFiles.Instance.OtherFiles)
Expand Down
Loading