diff --git a/Assets/Tests/ReflectionTests.cs b/Assets/Tests/ReflectionTests.cs index 116d539..80c7966 100644 --- a/Assets/Tests/ReflectionTests.cs +++ b/Assets/Tests/ReflectionTests.cs @@ -1,14 +1,20 @@ #if UNITY_2020_1_OR_NEWER +using System; +using System.Collections; using System.Linq; using NUnit.Framework; using UnityEngine; using UnityEngine.UIElements; +using UnityEngine.TestTools; #if UNITY_2021_1_OR_NEWER using UnityEditor.PackageManager.UI.Internal; #else using UnityEditor.PackageManager.UI; #endif +#if UNITY_2023_1_OR_NEWER +using UpmPackage = UnityEditor.PackageManager.UI.Internal.Package; +#endif namespace Coffee.UpmGitExtension { @@ -34,6 +40,39 @@ public void UpmPackageVersion_Tag() ); } + [UnityTest] + public IEnumerator UpmPackage_UpdateVersions() + { + if (Application.isBatchMode) yield break; // Skip in batch mode. +#if UNITY_2020_1 + var upmClient = UpmClient.instance; + var database = PackageDatabase.instance; +#else + var upmClient = UnityEditor.ScriptableSingleton.instance.Resolve(); + var database = UnityEditor.ScriptableSingleton.instance.Resolve(); +#endif + + IOperation current = null; + void onListOperation(IOperation op) + { + current = op; + upmClient.onListOperation -= onListOperation; + } + + upmClient.onListOperation += onListOperation; + upmClient.List(true); + + while (current == null || current.isInProgress) + { + yield return null; + } + + var package = database.GetPackage("com.coffee.softmask-for-ugui") as UpmPackage; + Console.WriteLine(JsonUtility.ToJson(package, true)); + Debug.Log(JsonUtility.ToJson(package, true)); + package.UpdateVersionsSafety(new[] { package.GetInstalledVersion() }); + } + #if UNITY_2023_1_OR_NEWER #elif UNITY_2021_2_OR_NEWER [Test] diff --git a/Packages/src/Editor/Coffee.UpmGitExtension/Extensions/PackageExtensions.cs b/Packages/src/Editor/Coffee.UpmGitExtension/Extensions/PackageExtensions.cs index ec1ecc8..a7779f0 100644 --- a/Packages/src/Editor/Coffee.UpmGitExtension/Extensions/PackageExtensions.cs +++ b/Packages/src/Editor/Coffee.UpmGitExtension/Extensions/PackageExtensions.cs @@ -1,4 +1,14 @@ +// UpmPackage.UpdateVersions is removed from Unity 2021.3.26 + +#if UNITY_2021_3_0 || UNITY_2021_3_1 || UNITY_2021_3_2 || UNITY_2021_3_3 || UNITY_2021_3_4 || UNITY_2021_3_5 || UNITY_2021_3_6 || UNITY_2021_3_7 || UNITY_2021_3_8 || UNITY_2021_3_9 +#elif UNITY_2021_3_10 || UNITY_2021_3_11 || UNITY_2021_3_12 || UNITY_2021_3_13 || UNITY_2021_3_14 || UNITY_2021_3_15 || UNITY_2021_3_16 || UNITY_2021_3_17 || UNITY_2021_3_18 || UNITY_2021_3_19 +#elif UNITY_2021_3_20 || UNITY_2021_3_21 || UNITY_2021_3_22 || UNITY_2021_3_23 || UNITY_2021_3_24 || UNITY_2021_3_25 +#elif UNITY_2021_3 +#define UNITY_2021_3_26_OR_NEWER +#endif + using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; using UnityEditor.PackageManager; #if UNITY_2021_1_OR_NEWER @@ -24,16 +34,30 @@ public static PackageInfo GetPackageInfo(this IPackageVersion self) internal static class UpmPackageExtensions { - public static void UpdateVersionsSafety(this UpmPackage self, IEnumerable updatedVersions) + public static UpmPackage UpdateVersionsSafety(this UpmPackage self, IEnumerable versions) { - if (self.Has("UpdateVersions", updatedVersions, 0)) +#if UNITY_2023_1_OR_NEWER + var factory = UnityEditor.ScriptableSingleton.instance.Resolve(); + self = factory.CreatePackage(self.name, new UpmVersionList(versions.OrderBy(v => v.version))); +#elif UNITY_2022_2_OR_NEWER || UNITY_2021_3_26_OR_NEWER + self = new UpmPackage(self.uniqueId, true, new UpmVersionList(versions.OrderBy(v => v.version))); +#else + if (self.Has("UpdateVersions", versions, 0)) { - self.Call("UpdateVersions", updatedVersions, 0); + self.Call("UpdateVersions", versions, 0); + } + else if (self.Has("UpdateVersions", versions)) + { + self.Call("UpdateVersions", versions); } else { - self.Call("UpdateVersions", updatedVersions); + throw new System.NotImplementedException( + "void UpmPackage.UpdateVersions(IEnumerable, int) or void UpmPackage.UpdateVersions(IEnumerable) is not found"); } +#endif + + return self; } } diff --git a/Packages/src/Editor/Coffee.UpmGitExtension/GitPackageDataBase.cs b/Packages/src/Editor/Coffee.UpmGitExtension/GitPackageDataBase.cs index 6c3b81f..138be0d 100644 --- a/Packages/src/Editor/Coffee.UpmGitExtension/GitPackageDataBase.cs +++ b/Packages/src/Editor/Coffee.UpmGitExtension/GitPackageDataBase.cs @@ -243,17 +243,7 @@ private static void UpdateGitPackageVersions() .OrderBy(v => v.semVersion) .ThenBy(v => v.isInstalled) .ToArray(); - -#if UNITY_2023_1_OR_NEWER - var factory = ScriptableSingleton.instance.Resolve(); - var versionList = new UpmVersionList(newVersions.OrderBy(v => v.version)); - upmPackage = factory.CreatePackage(upmPackage.name, versionList); -#elif UNITY_2022_2_OR_NEWER - var versionList = new UpmVersionList(newVersions.OrderBy(v => v.version)); - upmPackage = new UpmPackage(upmPackage.uniqueId, true, versionList); -#else - upmPackage.UpdateVersionsSafety(newVersions); -#endif + upmPackage = upmPackage.UpdateVersionsSafety(newVersions); return upmPackage; }