From d58bb7ba31dff069cc4c118502c808a2f225ce1d Mon Sep 17 00:00:00 2001 From: RealityStop Date: Tue, 10 Nov 2020 00:04:40 -0500 Subject: [PATCH 1/2] Initial content. Package+buildscript & asmdef --- Editor.meta | 8 ++ Editor/BuildHelper.cs | 81 +++++++++++++++++++ Editor/BuildHelper.cs.meta | 11 +++ Editor/Unity.CustomPackage.LinkMerge.asmdef | 15 ++++ .../Unity.CustomPackage.LinkMerge.asmdef.meta | 7 ++ README.md | 1 - package.json | 17 ++++ package.json.meta | 7 ++ 8 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 Editor.meta create mode 100644 Editor/BuildHelper.cs create mode 100644 Editor/BuildHelper.cs.meta create mode 100644 Editor/Unity.CustomPackage.LinkMerge.asmdef create mode 100644 Editor/Unity.CustomPackage.LinkMerge.asmdef.meta delete mode 100644 README.md create mode 100644 package.json create mode 100644 package.json.meta diff --git a/Editor.meta b/Editor.meta new file mode 100644 index 0000000..ac325e8 --- /dev/null +++ b/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9f4e1c2642830f34d8b704386ba6e3af +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/BuildHelper.cs b/Editor/BuildHelper.cs new file mode 100644 index 0000000..d071700 --- /dev/null +++ b/Editor/BuildHelper.cs @@ -0,0 +1,81 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using UnityEditor.Build; +using UnityEditor.Build.Reporting; +using UnityEditor.PackageManager; +using UnityEngine; + +public class PackagesLinkXmlExtractor : IPreprocessBuildWithReport, IPostprocessBuildWithReport +{ + public string TemporaryFolder + { + get { return $"{Application.dataPath}/Temporary-Build/";} + } + + public string TemporaryFolderMeta + { + get { return $"{Application.dataPath}/Temporary-Build.meta";} + } + + public string LinkFilePath + { + get { return $"{TemporaryFolder}link.xml"; } + } + + public int callbackOrder { get { return 0; } } + + public void OnPreprocessBuild(BuildReport report) + { + CreateMergedLinkFromPackages(); + } + + public void OnPostprocessBuild(BuildReport report) + { + if(File.Exists(LinkFilePath)) + File.Delete(LinkFilePath); + if (Directory.Exists(TemporaryFolder)) + { + if (!Directory.EnumerateFiles(TemporaryFolder, "*").Any()) + Directory.Delete(TemporaryFolder); + Directory.Delete(TemporaryFolder, true); + } + if (File.Exists(TemporaryFolderMeta)) + File.Delete(TemporaryFolderMeta); + } + + private void CreateMergedLinkFromPackages() + { + var request = Client.List(); + do { } while (!request.IsCompleted); + if (request.Status == StatusCode.Success) + { + List xmlPathList = new List(); + foreach (var package in request.Result) + { + var path = package.resolvedPath; + xmlPathList.AddRange(Directory.EnumerateFiles(path, "linkmerge.xml", SearchOption.AllDirectories).ToList()); + } + + if (xmlPathList.Count <= 0) + return; + + var xmlList = xmlPathList.Select(XDocument.Load).ToArray(); + + var combinedXml = xmlList.First(); + foreach (var xDocument in xmlList.Where(xml => xml != combinedXml)) + { + combinedXml.Root.Add(xDocument.Root.Elements()); + } + + if (!Directory.Exists(TemporaryFolder)) + Directory.CreateDirectory(TemporaryFolder); + combinedXml.Save(LinkFilePath); + } + else if (request.Status >= StatusCode.Failure) + { + Debug.LogError(request.Error.message); + } + } +} \ No newline at end of file diff --git a/Editor/BuildHelper.cs.meta b/Editor/BuildHelper.cs.meta new file mode 100644 index 0000000..63c95d3 --- /dev/null +++ b/Editor/BuildHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c12770bea2254cc41b88f94667a1ae57 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Unity.CustomPackage.LinkMerge.asmdef b/Editor/Unity.CustomPackage.LinkMerge.asmdef new file mode 100644 index 0000000..a0ee6c2 --- /dev/null +++ b/Editor/Unity.CustomPackage.LinkMerge.asmdef @@ -0,0 +1,15 @@ +{ + "name": "Unity.CustomPackage.LinkMerge", + "references": [], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Editor/Unity.CustomPackage.LinkMerge.asmdef.meta b/Editor/Unity.CustomPackage.LinkMerge.asmdef.meta new file mode 100644 index 0000000..dd237d6 --- /dev/null +++ b/Editor/Unity.CustomPackage.LinkMerge.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 1e7f100b027e4184dae29d96f09e1440 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md b/README.md deleted file mode 100644 index b86a0aa..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -Unity.CustomPackage.LinkMerge diff --git a/package.json b/package.json new file mode 100644 index 0000000..4df3595 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "Unity.CustomPackage.LinkMerge", + "version": "1.0.0", + "displayName": "Custom Package Link Merger", + "description": "A Utility to pull in flagged link.xml files from custom packages.", + "unity": "2018.4", + "dependencies": {}, + "keywords": [ + "Packages", + "Link" + "Merge" + "LinkMerge" + ], + "author": { + "name": "Reality.Stop()" + } +} \ No newline at end of file diff --git a/package.json.meta b/package.json.meta new file mode 100644 index 0000000..eba0a6c --- /dev/null +++ b/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 411775e880ef3704d833ee1d2fc9d4d5 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: From 397378dc3825b5c125b984f7e1c04c9708ab9072 Mon Sep 17 00:00:00 2001 From: RealityStop Date: Tue, 10 Nov 2020 00:26:27 -0500 Subject: [PATCH 2/2] Sigh. Unity doesn't support package dependencies, so for now, the "first" to execute wins --- Editor/BuildHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Editor/BuildHelper.cs b/Editor/BuildHelper.cs index d071700..82af466 100644 --- a/Editor/BuildHelper.cs +++ b/Editor/BuildHelper.cs @@ -28,7 +28,8 @@ public string LinkFilePath public void OnPreprocessBuild(BuildReport report) { - CreateMergedLinkFromPackages(); + if (!File.Exists(LinkFilePath)) + CreateMergedLinkFromPackages(); } public void OnPostprocessBuild(BuildReport report)