Skip to content

Commit

Permalink
Merge branch 'feature/PreviewOptionalUpgrade' into 'main'
Browse files Browse the repository at this point in the history
Add functionality to upgrade to optionally upgrade to preview packages and...

See merge request halodi/controls/unity/halodi-unity-package-registry-manager!27
  • Loading branch information
jamestkpoon committed May 26, 2021
2 parents 0aa98b5 + f7d1294 commit 9c70654
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 107 deletions.
240 changes: 168 additions & 72 deletions Editor/Halodi/PackageRegistry/Core/UpgradePackagesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,177 @@
using System.Threading;
using System.Collections.Generic;
using Artees.UnitySemVer;
using System;

namespace Halodi.PackageRegistry.Core
{
public class UpgradePackagesManager
{

public List<UnityEditor.PackageManager.PackageInfo> UpgradeablePackages = new List<UnityEditor.PackageManager.PackageInfo>();
public class PackageUpgradeState
{
public PackageUpgradeState(UnityEditor.PackageManager.PackageInfo info)
{
this.info = info;
previewAvailable = false;
stableAvailable = false;
verifiedAvailable = false;
hasVerified = false;
stableVersion = SemVer.Parse(info.version);
previewVersion = SemVer.Parse(info.version);



try
{
current = SemVer.Parse(info.version);
}
catch
{
Debug.LogError("Cannot parse version for package " + info.displayName + ": " + info.version);
}

if (info.source == PackageSource.Git)
{

previewAvailable = true;
preview = info.packageId;

stableAvailable = true;
stable = info.packageId;
}
else if (info.source == PackageSource.Registry)
{

string[] compatible = info.versions.compatible;

foreach (string ver in compatible)
{
try
{
SemVer version = SemVer.Parse(ver);

if (string.IsNullOrWhiteSpace(version.preRelease))
{
if (version > stableVersion)
{
stableVersion = version;
stableAvailable = true;
stable = info.name + "@" + ver;
}
}
else
{
// This is a pre-release
if (version > previewVersion)
{
previewVersion = version;
previewAvailable = true;
preview = info.name + "@" + ver;
}

}
}
catch
{
Debug.LogError("Invalid version for package " + info.displayName + ": " + ver);
}
}

hasVerified = !String.IsNullOrWhiteSpace(info.versions.verified);
if(hasVerified)
{
try
{
verifiedVersion = SemVer.Parse(info.versions.verified);
if(verifiedVersion > current)
{
verifiedAvailable = verifiedVersion > current;
verified = info.name + "@" + info.versions.verified;
}

}
catch
{
Debug.LogError("Cannot parse version for package " + info.displayName + ": " + info.versions.verified);
}
}
}
}

internal string GetCurrentVersion()
{
return info.packageId;
}

public UnityEditor.PackageManager.PackageInfo info;

private SemVer current;

private bool previewAvailable;
private SemVer previewVersion;

private string preview;

private bool stableAvailable;
private SemVer stableVersion;

private string stable;

private bool hasVerified;
private bool verifiedAvailable;
private SemVer verifiedVersion;
private string verified;

public bool HasNewVersion(bool showPreviewVersion, bool useVerified)
{
if(useVerified && hasVerified)
{
return verifiedAvailable;
}
else if (showPreviewVersion)
{
return previewAvailable || stableAvailable;

}
else
{
return stableAvailable;
}
}

public string GetNewestVersion(bool showPreviewVersion, bool useVerified)
{
if(useVerified && hasVerified)
{
if(verifiedAvailable)
{
return verified;
}
}
else if (showPreviewVersion)
{
if (previewAvailable)
{
if (!stableAvailable || previewVersion > stableVersion)
{
return preview;
}
}
}
if (stableAvailable)
{
if (stableAvailable)
{
return stable;
}
}

return null;
}
}

public List<PackageUpgradeState> UpgradeablePackages = new List<PackageUpgradeState>();

private ListRequest request;

Expand All @@ -35,14 +199,7 @@ public void Update()
PackageCollection collection = request.Result;
foreach (UnityEditor.PackageManager.PackageInfo info in collection)
{
if (info.source == PackageSource.Git)
{
UpgradeablePackages.Add(info);
}
else if (info.source == PackageSource.Registry)
{
AddRegistryPackage(info);
}
UpgradeablePackages.Add(new PackageUpgradeState(info));
}
}
else
Expand All @@ -54,71 +211,10 @@ public void Update()
}
}

private void AddRegistryPackage(UnityEditor.PackageManager.PackageInfo info)
{
try
{

SemVer latestVersion = SemVer.Parse(GetLatestVersion(info));
SemVer currentVersion = SemVer.Parse(info.version);

if (currentVersion < latestVersion)
{
UpgradeablePackages.Add(info);
}
}
catch(System.Exception)
{
Debug.LogError("Invalid version for package " + info.displayName + ". Current: " + info.version + ", Latest: " + GetLatestVersion(info));
}
}

public string GetLatestVersion(UnityEditor.PackageManager.PackageInfo info)
public bool UpgradePackage(String packageWithVersion, ref string error)
{
if (info.source == PackageSource.Git)
{
return info.packageId;
}
else
{
string latest = "";

#if UNITY_2019_1_OR_NEWER
if (string.IsNullOrEmpty(info.versions.verified))
{
latest = info.versions.latestCompatible;
}
else
{
latest = info.versions.verified;
}
#else
latest = info.versions.latestCompatible;
#endif

return latest;
}
}

public bool UpgradePackage(UnityEditor.PackageManager.PackageInfo info, ref string error)
{
string latestVersion;
if(info.source == PackageSource.Git)
{
latestVersion = GetLatestVersion(info);
}
else if (info.source == PackageSource.Registry)
{
latestVersion = info.name + "@" + GetLatestVersion(info);
}
else
{
error = "Invalid source";
return false;
}


AddRequest request = UnityEditor.PackageManager.Client.Add(latestVersion);
AddRequest request = UnityEditor.PackageManager.Client.Add(packageWithVersion);

while (!request.IsCompleted)
{
Expand Down
2 changes: 1 addition & 1 deletion Editor/Halodi/PackageRegistry/UI/InstallPackageCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Halodi.PackageRegistry.UI
{
internal class InstallPackageCreatorView : EditorWindow
{
[MenuItem("Packages/Install Halodi Package Creator", false, 34)]
[MenuItem("Packages/Install Halodi Package Creator", false, 41)]
internal static void ManageRegistries()
{
UnityEditor.PackageManager.Client.Add("com.halodi.halodi-unity-package-creator");
Expand Down
Loading

0 comments on commit 9c70654

Please sign in to comment.