Skip to content

Instantly share code, notes, and snippets.

@Refsa
Last active April 24, 2020 06:19
Show Gist options
  • Save Refsa/0e5c456c44a031f8e9bc4c0abfce4ff5 to your computer and use it in GitHub Desktop.
Save Refsa/0e5c456c44a031f8e9bc4c0abfce4ff5 to your computer and use it in GitHub Desktop.
Unity Package Manager updater for Unity UPM packages on GitHub using Git Tags for versions
// Object representation of the json result from the GitHub tags API
[System.Serializable]
class GitHubTagsResponseCommit
{
public string sha;
public string url;
}
[System.Serializable]
class GitHubTagsResponse
{
public string name;
public string zipball_url;
public string tarball_url;
public GitHubTagsResponseCommit commit;
public string node_id;
}
[System.Serializable]
class GitHubTagsResponseRoot
{
public GitHubTagsResponse[] responses;
}
// This class contains all you need to retreive tags, get current package version
// and update your package via PackageManager.Client
// Replace <username> with the relevant GitHub username
// Replace <repo> with the repo you want the tags for
// Replace <packagename> with your package name, f.ex. "com.user.name"
using System.Collections.Generic;
using System.Linq;
using System.Net;
using UnityEditor.PackageManager;
using UnityEngine;
public static class PackageVersionCache
{
static System.Uri getUrl = new System.Uri(@"https://api.github.com/repos/<username>/<repo>/tags");
static string gitUrl = "https://github.com/<username>/<repo>.git";
static string packageName = "<packageName>";
public static List<string> latestVersionTags;
public static string currentVersion;
// Gets the currently installed version from PackageManager.Client
public static bool FetchCurrentVersion()
{
var pmRequest = UnityEditor.PackageManager.Client.List();
while (!pmRequest.IsCompleted) { }
if (pmRequest.Status == StatusCode.Success)
{
try
{
var packageInfo = pmRequest.Result.First(p => p.name == packageName);
if (packageInfo != null)
{
currentVersion = packageInfo.version;
return true;
}
}
catch {}
}
return false;
}
// Fetches the tags on the GitHub repo
public static bool FetchLatestTags()
{
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Add("User-Agent: request");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
try
{
byte[] data = client.DownloadData(getUrl);
string responseJson = System.Text.Encoding.UTF8.GetString(data);
GitHubTagsResponseRoot responses = null;
if (!string.IsNullOrEmpty(responseJson))
{
responses = JsonUtility.FromJson<GitHubTagsResponseRoot>("{\"responses\":" + responseJson + "}");
}
latestVersionTags = responses.responses.Select(e => e.name).ToList();
client.Dispose();
return true;
}
catch (System.Exception e)
{
UnityEngine.Debug.LogError($"Error when retreiving Tags from GitHub API \n\t{e.Message}");
client.Dispose();
return false;
}
}
// Updates the version through PackageManager.Client
public static bool UpdateVersion(string newVersion)
{
if (string.IsNullOrEmpty(newVersion) || newVersion == currentVersion || newVersion == currentVersion + "-preview") return false;
var addRequest = UnityEditor.PackageManager.Client.Add(gitUrl + "#" + newVersion);
while (!addRequest.IsCompleted) {}
return addRequest.Status == StatusCode.Success;
}
}
@Refsa
Copy link
Author

Refsa commented Apr 24, 2020

Fair warning before using this:

You are subject to the GitHub API TOS
I am not responsible for your misuse of the terms of service.

Currently this Gist does not contain proper exception handling and user feedback. You should probably come back later for my updated revision or make the necessary changes yourself.

Hope this could be useful, it certainly helped me with managing Unity UPM Packages without a proper UPM registry server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment