public static void GetRefs(string repoUrl, Action <IEnumerable <string> > callback) { // If it is cached. string cacheKey = "<GetRefs>" + repoUrl; IEnumerable <string> result; if (EditorKvs.TryGet(cacheKey, out result, x => x.Split(','))) { callback(result); return; } string args = string.Format("ls-remote --refs -q {0}", repoUrl); ExecuteGitCommand(args, (success, output) => { if (success) { result = s_regRefs.Matches(output) .OfType <Match> () .Select(x => x.Groups [2].Value.Trim()) .ToArray(); if (result.Any()) { EditorKvs.Set(cacheKey, result, x => string.Join(",", x)); } } else { result = new string [0]; } callback(result); }); }
public static void GetPackageJson(string repoUrl, string branch, Action <string> callback) { // package.json is cached. string cacheKey = "<GetPackageJson>" + repoUrl + "/" + branch; string result; if (EditorKvs.TryGet(cacheKey, out result)) { callback(PackageJsonHelper.GetPackageNameFromJson(result)); return; } // Download raw package.json from host. using (var wc = new System.Net.WebClient()) { string userAndRepoName = PackageUtils.GetUserAndRepo(repoUrl); var host = Settings.GetHostData(repoUrl); Uri uri = new Uri(string.Format(host.Raw, userAndRepoName, branch, "package.json")); wc.DownloadStringCompleted += (s, e) => { IsGitRunning = false; // Download is completed successfully. if (e.Error == null) { try { // Check meta file. wc.DownloadData(new Uri(string.Format(host.Raw, userAndRepoName, branch, "package.json.meta"))); if (!string.IsNullOrEmpty(e.Result)) { EditorKvs.Set(cacheKey, e.Result); } callback(PackageJsonHelper.GetPackageNameFromJson(e.Result)); return; } // Maybe, package.json.meta is not found. catch (Exception ex) { Debug.LogException(ex); callback(""); } } // Download is failed: Clone the repo to get package.json. string clonePath = Path.GetTempFileName(); FileUtil.DeleteFileOrDirectory(clonePath); string args = string.Format("clone --depth=1 --branch {0} --single-branch {1} {2}", branch, repoUrl, clonePath); ExecuteGitCommand(args, (_, __) => { // Get package.json from cloned repo. string filePath = Path.Combine(clonePath, "package.json"); string json = File.Exists(filePath) && File.Exists(filePath + ".meta") ? File.ReadAllText(filePath) : ""; if (!string.IsNullOrEmpty(json)) { EditorKvs.Set(cacheKey, json); } callback(PackageJsonHelper.GetPackageNameFromJson(json)); }); }; IsGitRunning = true; wc.DownloadStringAsync(uri); } }