#!/usr/bin/env bash set -euo pipefail workspace_root="${1:-$PWD}" workspace_root="$(cd "$workspace_root" && pwd)" unity_bin="${UNITY_EDITOR_PATH:-$(command -v unity-editor || command -v unity || command -v Unity || true)}" consumer_root="$workspace_root/Temp/PublishedUpmConsumerValidation" if [[ ! -f "$workspace_root/ProjectSettings/ProjectVersion.txt" ]]; then echo "Workspace ProjectVersion.txt was not found: $workspace_root" >&2 exit 1 fi if [[ -z "$unity_bin" ]]; then echo "Unity editor executable was not found. Set UNITY_EDITOR_PATH." >&2 exit 1 fi case "$consumer_root" in "$workspace_root"/Temp/*) ;; *) echo "Consumer project must stay under the workspace Temp directory." >&2; exit 1 ;; esac write_validator() { local project_root="$1" cat > "$project_root/Assets/Editor/PublishedUpmConsumerValidator.cs" <<'EOF' #if UNITY_EDITOR #nullable enable using System; using System.IO; using System.Linq; using System.Reflection; using Newtonsoft.Json.Linq; using UnityEditor; using UnityEditor.Compilation; using UnityEditor.PackageManager; using UnityEngine; using PackageInfo = UnityEditor.PackageManager.PackageInfo; public static class PublishedUpmConsumerValidator { private const string RegistryUrl = "https://git.crash.work/api/packages/ShrinkSDK/npm/"; private const string UniTaskUrl = "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask#7c0f199fe0d3fc528024488ccd671e6c7b27745b"; public static void VerifyRegistry() { Run(() => { VerifyPackage("com.cneicy.shrink-app-starter-basic", "0.2.0", "ShrinkApp.Starter.Basic.Runtime"); VerifyPackage("com.cneicy.shrink-context-app-adapter", "0.1.3", "ShrinkContext.AppAdapter.Runtime"); VerifyPackage("com.cneicy.shrink-tutorial", "0.1.2", "ShrinkTutorial.Runtime"); RequireManifestValue("com.cneicy.shrink-app-starter-basic", "0.2.0"); RequireManifestValue("com.cneicy.shrink-tutorial", "0.1.2"); RequireManifestValue("com.cysharp.unitask", UniTaskUrl); RequireRegistry("com.cneicy", RegistryUrl); }); } public static void VerifyGitTag() { Run(() => { VerifyPackage("com.cneicy.shrink-eventbus", "2.0.0", "ShrinkEventBus.Runtime"); RequireManifestValue( "com.cneicy.shrink-eventbus", "https://git.crash.work/ShrinkSDK/ShrinkEventBus.git#v2.0.0"); RequireManifestValue("com.cysharp.unitask", UniTaskUrl); }); } public static void ApplyInstaller() { try { VerifyPackage("com.cneicy.shrink-installer", "0.2.1", "ShrinkInstaller.Editor"); RequireManifestValue( "com.cneicy.shrink-installer", "https://git.crash.work/ShrinkSDK/Installer.git#v0.2.1"); var catalog = FindType("ShrinkSDK.Installer.ShrinkSdkPackageCatalog"); var starter = catalog.GetField("StarterBasic", BindingFlags.Static | BindingFlags.NonPublic) ?.GetValue(null) ?? throw new InvalidOperationException("Installer StarterBasic catalog entry was not found."); var manifestStore = FindType("ShrinkSDK.Installer.ShrinkSdkManifestStore"); var install = manifestStore.GetMethod( "EnsurePackageInstallation", BindingFlags.Static | BindingFlags.NonPublic) ?? throw new InvalidOperationException("Installer manifest installation method was not found."); var roots = Array.CreateInstance(starter.GetType(), 1); roots.SetValue(starter, 0); install.Invoke(null, new object[] { roots }); RequireManifestValue("com.cneicy.shrink-app-starter-basic", "0.2.0"); RequireManifestValue("com.cysharp.unitask", UniTaskUrl); RequireRegistry("com.cneicy", RegistryUrl); RequireRegistry("com.example", "https://registry.npmjs.org/"); Succeed("Installer wrote the fixed Starter Basic manifest entry."); } catch (Exception exception) { Fail(Unwrap(exception)); } } public static void VerifyInstallerResult() { Run(() => { VerifyPackage("com.cneicy.shrink-installer", "0.2.1", "ShrinkInstaller.Editor"); VerifyPackage("com.cneicy.shrink-app-starter-basic", "0.2.0", "ShrinkApp.Starter.Basic.Runtime"); VerifyPackage("com.cneicy.shrink-context-app-adapter", "0.1.3", "ShrinkContext.AppAdapter.Runtime"); RequireManifestValue("com.cneicy.shrink-app-starter-basic", "0.2.0"); RequireManifestValue("com.cysharp.unitask", UniTaskUrl); RequireRegistry("com.cneicy", RegistryUrl); RequireRegistry("com.example", "https://registry.npmjs.org/"); }); } private static void Run(Action action) { try { action(); Succeed("Published UPM consumer validation passed."); } catch (Exception exception) { Fail(Unwrap(exception)); } } private static void VerifyPackage(string packageName, string version, string assemblyName) { var package = PackageInfo.GetAllRegisteredPackages() .FirstOrDefault(candidate => string.Equals(candidate.name, packageName, StringComparison.Ordinal)); if (package == null) { throw new InvalidOperationException("Package was not resolved: " + packageName); } if (!string.Equals(package.version, version, StringComparison.Ordinal)) { throw new InvalidOperationException( "Package version mismatch for " + packageName + ": " + package.version + " != " + version); } if (!CompilationPipeline.GetAssemblies().Any(assembly => string.Equals(assembly.name, assemblyName, StringComparison.Ordinal))) { throw new InvalidOperationException("Expected assembly was not compiled: " + assemblyName); } } private static void RequireManifestValue(string packageName, string expectedValue) { var manifest = ReadManifest(); var actualValue = (string?)manifest["dependencies"]?[packageName]; if (!string.Equals(actualValue, expectedValue, StringComparison.Ordinal)) { throw new InvalidOperationException( "Manifest dependency mismatch for " + packageName + ": " + actualValue + " != " + expectedValue); } } private static void RequireRegistry(string scope, string url) { var registries = ReadManifest()["scopedRegistries"] as JArray; var found = registries?.OfType().Any(registry => string.Equals(NormalizeUrl((string?)registry["url"]), NormalizeUrl(url), StringComparison.OrdinalIgnoreCase) && (registry["scopes"] as JArray)?.Values().Any(value => string.Equals(value, scope, StringComparison.Ordinal)) == true) == true; if (!found) { throw new InvalidOperationException("Scoped registry was not preserved or added: " + scope + " -> " + url); } } private static JObject ReadManifest() { var manifestPath = Path.Combine(Path.GetFullPath(Path.Combine(Application.dataPath, "..")), "Packages", "manifest.json"); return JObject.Parse(File.ReadAllText(manifestPath)); } private static Type FindType(string fullName) { return AppDomain.CurrentDomain.GetAssemblies() .Select(assembly => assembly.GetType(fullName, false)) .FirstOrDefault(type => type != null) ?? throw new InvalidOperationException("Installer type was not loaded: " + fullName); } private static string NormalizeUrl(string? url) => (url ?? string.Empty).Trim().TrimEnd('/') + "/"; private static Exception Unwrap(Exception exception) => exception is TargetInvocationException { InnerException: not null } ? exception.InnerException : exception; private static void Succeed(string message) { Debug.Log(message); EditorApplication.Exit(0); } private static void Fail(Exception exception) { Debug.LogException(exception); EditorApplication.Exit(1); } } #endif EOF } prepare_consumer() { local name="$1" local project_root="$consumer_root/$name" mkdir -p "$project_root/Packages" "$project_root/Assets/Editor" "$project_root/ProjectSettings" cp "$workspace_root/ProjectSettings/ProjectVersion.txt" "$project_root/ProjectSettings/ProjectVersion.txt" write_validator "$project_root" printf '%s' "$project_root" } write_manifest() { local project_root="$1" local manifest="$2" printf '%s\n' "$manifest" > "$project_root/Packages/manifest.json" } show_failure() { local log_path="$1" echo "Unity consumer validation failed. Relevant log lines:" >&2 grep -in -E 'exception|buildfailed|build failed|error|failed|compilererror|invalidoperation|nullreference|package manager' "$log_path" | tail -n 180 >&2 || true } run_unity() { local project_root="$1" local method="$2" local log_path="$3" local wait_for_resolve="$4" local args=( -batchmode -nographics -projectPath "$project_root" -executeMethod "$method" -logFile "$log_path" ) if [[ "$wait_for_resolve" != "true" ]]; then args+=(-quit) fi if ! "$unity_bin" "${args[@]}"; then show_failure "$log_path" exit 1 fi } rm -rf "$consumer_root" mkdir -p "$consumer_root" registry_project="$(prepare_consumer registry)" write_manifest "$registry_project" '{ "scopedRegistries": [ { "name": "ShrinkSDK", "url": "https://git.crash.work/api/packages/ShrinkSDK/npm/", "scopes": ["com.cneicy"] } ], "dependencies": { "com.cneicy.shrink-app-starter-basic": "0.2.0", "com.cneicy.shrink-tutorial": "0.1.2", "com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask#7c0f199fe0d3fc528024488ccd671e6c7b27745b", "com.unity.nuget.newtonsoft-json": "3.2.2" } }' run_unity "$registry_project" PublishedUpmConsumerValidator.VerifyRegistry "$registry_project/registry.log" false git_project="$(prepare_consumer git-tag)" write_manifest "$git_project" '{ "dependencies": { "com.cneicy.shrink-eventbus": "https://git.crash.work/ShrinkSDK/ShrinkEventBus.git#v2.0.0", "com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask#7c0f199fe0d3fc528024488ccd671e6c7b27745b", "com.unity.nuget.newtonsoft-json": "3.2.2" } }' run_unity "$git_project" PublishedUpmConsumerValidator.VerifyGitTag "$git_project/git-tag.log" false installer_project="$(prepare_consumer installer)" write_manifest "$installer_project" '{ "scopedRegistries": [ { "name": "Existing Registry", "url": "https://registry.npmjs.org/", "scopes": ["com.example"] } ], "dependencies": { "com.cneicy.shrink-installer": "https://git.crash.work/ShrinkSDK/Installer.git#v0.2.1", "com.unity.nuget.newtonsoft-json": "3.2.2", "com.unity.test-framework": "1.1.33" } }' run_unity "$installer_project" PublishedUpmConsumerValidator.ApplyInstaller "$installer_project/installer-apply.log" false run_unity "$installer_project" PublishedUpmConsumerValidator.VerifyInstallerResult "$installer_project/installer-result.log" false rm -rf "$consumer_root" echo "Published UPM consumer validation passed: registry, exact Git tag, installer."