From 8008f7329f07c9b10707e295f630dbd2843ce1d6 Mon Sep 17 00:00:00 2001 From: Viktor Kharchenko Date: Thu, 11 Mar 2021 08:26:00 +0200 Subject: [PATCH 1/2] feat: get the name and the tag of a latest release in repository from the repo URL --- .../Editor/Utility/GitHubRelease.cs | 27 ++++++++++++++++ .../Editor/Utility/GitHubRelease.cs.meta | 3 ++ .../Editor/Utility/GitHubUtility.cs | 31 +++++++++++++++++++ .../Editor/Utility/GitHubUtility.cs.meta | 3 ++ 4 files changed, 64 insertions(+) create mode 100644 com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs create mode 100644 com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs.meta create mode 100644 com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs create mode 100644 com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs.meta diff --git a/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs new file mode 100644 index 0000000..0cac536 --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs @@ -0,0 +1,27 @@ +using System.Linq; + +namespace StansAssets.Plugins.Editor +{ + public class GitHubRelease + { + public string Name { get; private set; } + public string Tag { get; private set; } + + public void ReadJson (string json) + { + Name = FindValueByKey ("name", json); + Tag = FindValueByKey ("tag_name", json); + } + + string FindValueByKey (string key, string json) + { + // number of characters from the 1st char of the given key to the 1st char of corresponding value + int offsetBeforeValue = key.Length + 5; + int position = json.IndexOf ("\"" + key + "\"") + offsetBeforeValue; + var value = (position > offsetBeforeValue) + ? new string(json.Skip (position).TakeWhile (c => c != '"').ToArray ()) + : string.Empty; + return value; + } + } +} diff --git a/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs.meta b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs.meta new file mode 100644 index 0000000..d90acd7 --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ef20256522cd4339913eee76e849c638 +timeCreated: 1615401859 \ No newline at end of file diff --git a/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs new file mode 100644 index 0000000..9d361a0 --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs @@ -0,0 +1,31 @@ +using System; +using UnityEngine.Networking; + +namespace StansAssets.Plugins.Editor +{ + public static class GitHubUtility + { + public static void GetLatestRelease (string url, Action callback) + { + var release = new GitHubRelease (); + var rq = UnityWebRequest.Get (GetReleaseInfoURL (url)); + rq.SendWebRequest ().completed += obj => { + release.ReadJson (rq.downloadHandler.text); + callback (release); + }; + } + + public static string GetReleaseInfoURL (string repositoryURL) + { + if (repositoryURL.Contains ("github.com")) { + return repositoryURL.Replace (@".git", @"/releases/latest") + .Replace (@"ssh://git@github.com:", @"https://api.github.com/repos/"); + } + else if (repositoryURL.Contains ("npmjs.org")) { + throw new NotImplementedException (); + } + + throw new NotImplementedException (); + } + } +} diff --git a/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs.meta b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs.meta new file mode 100644 index 0000000..268a0fe --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a75ca5d93e3640d28b484907e79f0077 +timeCreated: 1615384062 \ No newline at end of file From b6bc65cf649ef1120973a237dcd128538e5f17cf Mon Sep 17 00:00:00 2001 From: Viktor Kharchenko Date: Thu, 11 Mar 2021 12:55:54 +0200 Subject: [PATCH 2/2] refactor: minor cleaning up --- .../Editor/IMGUI/Headers/IMGUIBlockWithIndent.cs | 1 - .../Editor/UIToolkit/SettingsWindow/BaseTab.cs | 1 - .../Editor/Utility/GitHubRelease.cs | 5 +++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/com.stansassets.plugins-dev-kit/Editor/IMGUI/Headers/IMGUIBlockWithIndent.cs b/com.stansassets.plugins-dev-kit/Editor/IMGUI/Headers/IMGUIBlockWithIndent.cs index 4ea0b1d..e682931 100644 --- a/com.stansassets.plugins-dev-kit/Editor/IMGUI/Headers/IMGUIBlockWithIndent.cs +++ b/com.stansassets.plugins-dev-kit/Editor/IMGUI/Headers/IMGUIBlockWithIndent.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using UnityEngine; using UnityEditor; diff --git a/com.stansassets.plugins-dev-kit/Editor/UIToolkit/SettingsWindow/BaseTab.cs b/com.stansassets.plugins-dev-kit/Editor/UIToolkit/SettingsWindow/BaseTab.cs index e51d860..5d8cc09 100644 --- a/com.stansassets.plugins-dev-kit/Editor/UIToolkit/SettingsWindow/BaseTab.cs +++ b/com.stansassets.plugins-dev-kit/Editor/UIToolkit/SettingsWindow/BaseTab.cs @@ -1,7 +1,6 @@ #if UNITY_2019_4_OR_NEWER using StansAssets.Foundation.Editor; -using UnityEditor; using UnityEngine.UIElements; namespace StansAssets.Plugins.Editor diff --git a/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs index 0cac536..339f3ac 100644 --- a/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs +++ b/com.stansassets.plugins-dev-kit/Editor/Utility/GitHubRelease.cs @@ -16,8 +16,9 @@ public void ReadJson (string json) string FindValueByKey (string key, string json) { // number of characters from the 1st char of the given key to the 1st char of corresponding value - int offsetBeforeValue = key.Length + 5; - int position = json.IndexOf ("\"" + key + "\"") + offsetBeforeValue; + int offsetBeforeValue = key.Length + 5; + string lookForString = $"\"{key}\""; + int position = json.IndexOf (lookForString) + offsetBeforeValue; var value = (position > offsetBeforeValue) ? new string(json.Skip (position).TakeWhile (c => c != '"').ToArray ()) : string.Empty;