8000 auto update assembly version based on package.json info by StephenHodgson · Pull Request #840 · XRTK/com.xrtk.core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Aug 11, 2024. It is now read-only.

auto update assembly version based on package.json info #840

Merged
merged 3 commits into from
May 16, 2021
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 Submodules/Lumin
Submodule Lumin updated from 10c703 to b7d8ca
2 changes: 1 addition & 1 deletion Submodules/Oculus
2 changes: 1 addition & 1 deletion Submodules/SDK
Submodule SDK updated from dbfdca to 6f0be2
2 changes: 1 addition & 1 deletion Submodules/WindowsMixedReality
2 changes: 1 addition & 1 deletion Submodules/glTF
Submodule glTF updated from b0b4ff to c6e72c
3 changes: 2 additions & 1 deletion XRTK-Core/Packages/com.xrtk.core/Editor/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) XRTK. All rights reserved.
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#define XRTK_EDITOR

using System.Reflection;

[assembly: AssemblyVersion("0.2.15")]
[assembly: AssemblyTitle("com.xrtk.core.editor")]
[assembly: AssemblyCompany("XRTK")]
[assembly: AssemblyCopyright("Copyright (c) XRTK. All rights reserved.")]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
Expand Down Expand Up @@ -28,9 +28,10 @@ internal class AsmDefSourceFiles
}

[Serializable]
internal class CustomScriptAssemblyData
internal class AssemblyDefinition
{
public string name = null;
public string version = null;
public string[] references = null;
public string[] optionalUnityReferences = null;
public string[] includePlatforms = null;
Expand All @@ -39,9 +40,9 @@ internal class CustomScriptAssemblyData

public AsmDefSourceFiles Source { get; set; } = new AsmDefSourceFiles();

public static CustomScriptAssemblyData FromJson(string json)
public static AssemblyDefinition FromJson(string json)
{
var scriptAssemblyData = JsonUtility.FromJson<CustomScriptAssemblyData>(json);
var scriptAssemblyData = JsonUtility.FromJson<AssemblyDefinition>(json);
if (scriptAssemblyData == null) { throw new Exception("Json file does not contain an assembly definition"); }
if (string.IsNullOrEmpty(scriptAssemblyData.name)) { throw new Exception("Required property 'name' not set"); }
if (scriptAssemblyData.excludePlatforms != null && scriptAssemblyData.excludePlatforms.Length > 0 &&
Expand All @@ -53,9 +54,9 @@ public static CustomScriptAssemblyData FromJson(string json)
return scriptAssemblyData;
}

public static string ToJson(CustomScriptAssemblyData data)
public static string ToJson(AssemblyDefinition definition)
{
return JsonUtility.ToJson(data, true);
return JsonUtility.ToJson(definition, true);
}
}

Expand All @@ -78,7 +79,7 @@ public static void ReplaceWithAssembly()
var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
var directoryPath = new FileInfo(assetPath).Directory?.FullName;
var assemblyDefinitionText = AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(assetPath).text;
var scriptAssemblyData = CustomScriptAssemblyData.FromJson(assemblyDefinitionText);
var scriptAssemblyData = AssemblyDefinition.FromJson(assemblyDefinitionText);
var fromAssemblyName = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName(scriptAssemblyData.name);

Debug.Assert(!string.IsNullOrEmpty(scriptAssemblyData.name));
Expand All @@ -88,7 +89,7 @@ public static void ReplaceWithAssembly()
CompilationPipeline.GetAssemblies(AssembliesType.Player).ReplaceSourceWithAssembly(ref scriptAssemblyData, directoryPath))
{
EditorUtility.DisplayProgressBar("Replacing source with assembly", "Saving source meta data for later...", 0.95f);
File.WriteAllText(assetPath, CustomScriptAssemblyData.ToJson(scriptAssemblyData));
File.WriteAllText(assetPath, AssemblyDefinition.ToJson(scriptAssemblyData));
File.WriteAllText($"{Path.GetFullPath(assetPath).Hide()}{JSON}", JsonUtility.ToJson(scriptAssemblyData.Source, true));
}
else
Expand All @@ -104,7 +105,7 @@ public static void ReplaceWithAssembly()
EditorUtility.ClearProgressBar();
}

private static bool ReplaceSourceWithAssembly(this Assembly[] assemblies, ref CustomScriptAssemblyData assemblyData, string directoryPath)
private static bool ReplaceSourceWithAssembly(this Assembly[] assemblies, ref AssemblyDefinition assemblyDefinition, string directoryPath)
{
EditorUtility.DisplayProgressBar("Replacing source with assembly", "Gathering assembly information...", 0.1f);

Expand All @@ -113,13 +114,13 @@ private static bool ReplaceSourceWithAssembly(this Assembly[] assemblies, ref Cu
Assembly assembly = assemblies[i];
EditorUtility.DisplayProgressBar("Replacing source with assembly", $"Processing assembly {assembly.name}", i / (float)assemblies.Length);

if (assembly.name != assemblyData.name) { continue; }
if (assembly.name != assemblyDefinition.name) { continue; }

Debug.Assert(assembly.sourceFiles != null);
Debug.Assert(assembly.sourceFiles.Length > 0);
Debug.Assert(File.Exists(assembly.outputPath));

assemblyData.Source.Files = assembly.sourceFiles;
assemblyDefinition.Source.Files = assembly.sourceFiles;
AssetDatabase.ReleaseCachedFileHandles();

for (var j = 0; j < assembly.sourceFiles.Length; j++)
Expand Down Expand Up @@ -153,37 +154,37 @@ private static bool ReplaceSourceWithAssembly(this Assembly[] assemblies, ref Cu
return true;
}

if (assemblyData.excludePlatforms != null && assemblyData.excludePlatforms.Length > 0 &&
assemblyData.includePlatforms != null && assemblyData.includePlatforms.Length > 0)
if (assemblyDefinition.excludePlatforms != null && assemblyDefinition.excludePlatforms.Length > 0 &&
assemblyDefinition.includePlatforms != null && assemblyDefinition.includePlatforms.Length > 0)
{
Selection.activeObject = importedAssembly;
Debug.LogError("Unable to update plugin import settings, as both exclude and include platforms have been enabled.");
return true;
}

BuildTarget buildTarget;
importedAssembly.SetCompatibleWithAnyPlatform(assemblyData.includePlatforms == null || assemblyData.includePlatforms.Length == 0);
importedAssembly.SetCompatibleWithAnyPlatform(assemblyDefinition.includePlatforms == null || assemblyDefinition.includePlatforms.Length == 0);

if (assemblyData.includePlatforms != null && assemblyData.includePlatforms.Length > 0)
if (assemblyDefinition.includePlatforms != null && assemblyDefinition.includePlatforms.Length > 0)
{
importedAssembly.SetCompatibleWithEditor(assemblyData.includePlatforms.Contains("Editor"));
importedAssembly.SetCompatibleWithEditor(assemblyDefinition.includePlatforms.Contains("Editor"));

for (int j = 0; j < assemblyData.includePlatforms?.Length; j++)
for (int j = 0; j < assemblyDefinition.includePlatforms?.Length; j++)
{
if (assemblyData.includePlatforms[j].TryGetBuildTarget(out buildTarget))
if (assemblyDefinition.includePlatforms[j].TryGetBuildTarget(out buildTarget))
{
importedAssembly.SetCompatibleWithPlatform(buildTarget, true);
}
}
}

if (assemblyData.excludePlatforms != null && assemblyData.excludePlatforms.Length > 0)
if (assemblyDefinition.excludePlatforms != null && assemblyDefinition.excludePlatforms.Length > 0)
{
importedAssembly.SetCompatibleWithEditor(!assemblyData.excludePlatforms.Contains("Editor"));
importedAssembly.SetCompatibleWithEditor(!assemblyDefinition.excludePlatforms.Contains("Editor"));

for (int j = 0; j < assemblyData.excludePlatforms?.Length; j++)
for (int j = 0; j < assemblyDefinition.excludePlatforms?.Length; j++)
{
if (assemblyData.excludePlatforms[j].TryGetBuildTarget(out buildTarget))
if (assemblyDefinition.excludePlatforms[j].TryGetBuildTarget(out buildTarget))
{
importedAssembly.SetExcludeFromAnyPlatform(buildTarget, true);
}
Expand Down Expand Up @@ -241,7 +242,7 @@ public static void ReplaceWithSource()
var assemblyDefinitionAsset = AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(assemblyDefinitionPath);
Debug.Assert(assemblyDefinitionAsset != null, $"Failed to load assembly def asset at {assemblyDefinitionPath}");
var assemblyDefinitionText = assemblyDefinitionAsset.text;
var scriptAssemblyData = CustomScriptAssemblyData.FromJson(assemblyDefinitionText);
var scriptAssemblyData = AssemblyDefinition.FromJson(assemblyDefinitionText);
var assemblySourcePath = $"{Path.GetFullPath(assemblyDefinitionPath).Hide()}{JSON}";
Debug.Assert(File.Exists(assemblySourcePath), "Fatal Error: Missing meta data to re-import source files. You'll need to manually do it by removing the '.' in front of each file.");
string sourceFilesText = File.ReadAllText(assemblySourcePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace XRTK.Editor.Utilities
{
internal class AssemblyDefinitionPreProcessor : AssetPostprocessor
{
private const string VersionRegexPattern = "\\[assembly: AssemblyVersion\\(\"(.*)\"\\)\\]";

private void OnPreprocessAsset()
{
if (assetPath.Contains("package.json") && !Application.isBatchMode)
{
var text = File.ReadAllText(assetPath);
var asmdef = AssemblyDefinitionEditorExtension.AssemblyDefinition.FromJson(text);

if (!asmdef.name.Contains("com.xrtk"))
{
return;
}

var newVersion = $"[assembly: AssemblyVersion(\"{asmdef.version}\")]";
var assemblyPath = assetPath.Replace("package.json", "Runtime/AssemblyInfo.cs");
var editorAssemblyPath = assetPath.Replace("package.json", "Editor/AssemblyInfo.cs");

Debug.Assert(File.Exists(assemblyPath));
Debug.Assert(File.Exists(editorAssemblyPath));

File.WriteAllText(assemblyPath, Regex.Replace(File.ReadAllText(assemblyPath), VersionRegexPattern, newVersion));
File.WriteAllText(editorAssemblyPath, Regex.Replace(File.ReadAllText(editorAssemblyPath), VersionRegexPattern, newVersion));
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion XRTK-Core/Packages/com.xrtk.core/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) XRTK. All rights reserved.
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#define XRTK

using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyVersion("0.2.15")]
[assembly: AssemblyTitle("com.xrtk.core")]
[assembly: AssemblyCompany("XRTK")]
[assembly: AssemblyCopyright("Copyright (c) XRTK. All rights reserved.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
Expand Down Expand Up @@ -2005,5 +2006,25 @@ private void OnDispose(bool finalizing)
}

#endregion IDisposable Implementation

private static List<Tuple<string, Version>> modules = null;

/// <summary>
/// The list of active xrtk modules/packages currently loaded into runtime.
/// </summary>
public static List<Tuple<string, Version>> Modules
{
get
{
return modules ?? (modules = AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly =>
{
var titleAttribute = assembly.GetCustomAttribute<AssemblyTitleAttribute>();
return titleAttribute != null && titleAttribute.Title.Contains("xrtk");
})
.Select(assembly => new Tuple<string, Version>(assembly.GetCustomAttribute<AssemblyTitleAttribute>().Title, assembly.GetName().Version))
.ToList());
}
} 3DD3
}
}
0