Skip to content

Commit

Permalink
close #17; Use system git instead of WebRequest to get branch/tag names
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Feb 11, 2019
1 parent fd51116 commit 8180a10
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 21 deletions.
55 changes: 34 additions & 21 deletions Assets/UpmGitExtension/Editor/Scripts/UpmGitExtensionUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using PackageInfo = UnityEditor.PackageManager.PackageInfo;
using Utils = Coffee.PackageManager.UpmGitExtensionUtils;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;

#if UNITY_2019_1_OR_NEWER
Expand Down Expand Up @@ -89,10 +90,16 @@ public void OnPackageSelectionChange (PackageInfo packageInfo)

if(isGit)
{
Utils.RequestTags (_packageInfo.packageId, _tags);
Utils.RequestBranches (_packageInfo.packageId, _branches);
_updateButton.text = "Update to";
_versionPopup.SetEnabled (false);
_updateButton.SetEnabled (false);
Utils.GetRefs (_packageInfo.packageId, _refs, () =>
{
_updateButton.SetEnabled (_currentRefName != _selectedRefName);
_versionPopup.SetEnabled (true);
});

SetVersion (_packageInfo.version);
SetVersion (_currentRefName);
EditorApplication.delayCall += ()=>
{
Utils.SetElementDisplay (_detailControls.Q ("updateCombo"), true);
Expand Down Expand Up @@ -128,6 +135,8 @@ public void OnPackageSelectionChange (PackageInfo packageInfo)
Button _viewDocumentation { get { return _gitDetailActoins.Q<Button> ("viewDocumentation"); } }
Button _viewChangelog { get { return _gitDetailActoins.Q<Button> ("viewChangelog"); } }
Button _viewLicense { get { return _gitDetailActoins.Q<Button> ("viewLicense"); } }
string _currentRefName { get { return Utils.GetRefName (_packageInfo.packageId); } }
string _selectedRefName { get { return _versionPopup.text != "(default)" ? _versionPopup.text : ""; } }
VisualElement _detailControls;
VisualElement _documentationContainer;
VisualElement _originalDetailActions;
Expand All @@ -136,6 +145,7 @@ public void OnPackageSelectionChange (PackageInfo packageInfo)
Button _updateButton;
List<string> _tags = new List<string> ();
List<string> _branches = new List<string> ();
List<string> _refs = new List<string> ();

/// <summary>
/// Initializes UI.
Expand Down Expand Up @@ -197,48 +207,51 @@ void InitializeUI ()
_initialized = true;
}

public static string GetVersionText(string version, string current = null)
{
return (current == null || current != version) ? version : version + " - current";
}

void PopupVersions()
{
var menu = new GenericMenu ();
var current = _packageInfo.version;
var currentRefName = _currentRefName;

menu.AddItem (new GUIContent (current + " - current"), _versionPopup.text == current, SetVersion, current);
menu.AddItem (new GUIContent (currentRefName + " - current"), _selectedRefName == currentRefName, SetVersion, currentRefName);

foreach (var t in _tags.OrderByDescending(x=>x))
// x.y(.z-sufix) only
foreach (var t in _refs.Where(x=>Regex.IsMatch(x, "^\\d+\\.\\d+.*$")).OrderByDescending (x => x))
{
string tag = t;
GUIContent text = new GUIContent ("All Tags/" + (current == tag ? tag + " - current" : tag));
menu.AddItem (text, _versionPopup.text == tag, SetVersion, tag);
string target = t;
bool isCurrent = currentRefName == target;
GUIContent text = new GUIContent ("All Versions/" + (isCurrent ? target + " - current" : target));
menu.AddItem (text, isCurrent, SetVersion, target);
}

menu.AddItem (new GUIContent ("All Branches/(default)"), false, SetVersion, "(default)");
foreach (var t in _branches.OrderBy (x => x))
// other
menu.AddItem (new GUIContent ("All Versions/Other/(default)"), _selectedRefName == "", SetVersion, "(default)");
foreach (var t in _refs.Where (x => !Regex.IsMatch (x, "^\\d+\\.\\d+.*$")).OrderByDescending (x => x))
{
string tag = t;
GUIContent text = new GUIContent ("All Branches/" + (current == tag ? tag + " - current" : tag));
menu.AddItem (text, _versionPopup.text == tag, SetVersion, tag);
string target = t;
bool isCurrent = currentRefName == target;
GUIContent text = new GUIContent ("All Versions/Other/" + (isCurrent ? target + " - current" : target));
menu.AddItem (text, isCurrent, SetVersion, target);
}

menu.DropDown (new Rect (_versionPopup.LocalToWorld (new Vector2 (0, 10)), Vector2.zero));
}

void SetVersion(object version)
{
string ver = version as string;
_versionPopup.text = ver;
_updateButton.SetEnabled (_packageInfo.version != ver);
_updateButton.SetEnabled(true);
_updateButton.SetEnabled (_currentRefName != _selectedRefName);
}

void AddOrUpdatePackage()
{
var target = _versionPopup.text != "(default)" ? _versionPopup.text : "";
var id = Utils.GetSpecificPackageId (_packageInfo.packageId, target);
Client.Add (id);

_versionPopup.SetEnabled (false);
_updateButton.SetEnabled (false);
_updateButton.text = "Updating to";
}
}
}
68 changes: 68 additions & 0 deletions Assets/UpmGitExtension/Editor/Scripts/UpmGitExtensionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
using PackageInfo = UnityEditor.PackageManager.PackageInfo;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Coffee.PackageManager
{
internal static class UpmGitExtensionUtils
{
static readonly StringBuilder s_sbError = new StringBuilder ();

const string kDisplayNone = "display-none";
public static void SetElementDisplay (VisualElement element, bool value)
{
Expand Down Expand Up @@ -72,6 +75,16 @@ public static string GetRepoURL (string packageId)
return "";
}

public static string GetRefName (string packageId)
{
Match m = Regex.Match (packageId, "^[^@]+@[^#]+#(.+)$");
if (m.Success)
{
return m.Groups [1].Value;
}
return "";
}

public static string GetRepoId (PackageInfo packageInfo)
{
return GetRepoId (packageInfo != null ? packageInfo.packageId : "");
Expand Down Expand Up @@ -211,5 +224,60 @@ public static AsyncOperation Request (string url, Action<string> onSuccess)
};
return op;
}

public static WaitUntil GetRefs (string packageId, List<string> result, Action onSuccess)
{
result.Clear ();
s_sbError.Length = 0;
string repoUrl = GetRepoURL (packageId);
var startInfo = new System.Diagnostics.ProcessStartInfo
{
Arguments = "ls-remote --refs -q " + repoUrl,
CreateNoWindow = true,
FileName = "git",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
};

bool exited = false;
var launchProcess = System.Diagnostics.Process.Start (startInfo);
if (launchProcess == null || launchProcess.HasExited == true || launchProcess.Id == 0)
{
Debug.LogError ("No 'git' executable was found. Please install Git on your system and restart Unity");
return null;
}
else
{
//Add process callback.
launchProcess.OutputDataReceived += (sender, e) =>
{
var m = Regex.Match (e.Data, "refs/(tags|heads)/(.*)$");
if(m.Success)
{
result.Add (m.Groups[2].Value);
}
};
launchProcess.ErrorDataReceived += (sender, e) => { if (!string.IsNullOrEmpty (e.Data)) s_sbError.AppendLine (e.Data); };
launchProcess.Exited += (sender, e) =>
{
exited = true;
bool success = 0 == s_sbError.Length;
if (!success)
{
Debug.LogErrorFormat ("Error: {0} => {1}\n\n{2}", packageId, repoUrl, s_sbError);
}
else
{
onSuccess ();
}
};

launchProcess.BeginOutputReadLine ();
launchProcess.BeginErrorReadLine ();
launchProcess.EnableRaisingEvents = true;
}
return new WaitUntil (() => exited);
}
}
}

0 comments on commit 8180a10

Please sign in to comment.