diff --git a/src/Neo.CLI/CLI/MainService.Plugins.cs b/src/Neo.CLI/CLI/MainService.Plugins.cs index 4468b63c9e..9038cacc47 100644 --- a/src/Neo.CLI/CLI/MainService.Plugins.cs +++ b/src/Neo.CLI/CLI/MainService.Plugins.cs @@ -83,45 +83,58 @@ private static async Task DownloadPluginAsync(string pluginName, Version var asmName = Assembly.GetExecutingAssembly().GetName(); httpClient.DefaultRequestHeaders.UserAgent.Add(new(asmName.Name!, asmName.Version!.ToString(3))); - var json = await httpClient.GetFromJsonAsync(Settings.Default.Plugins.DownloadUrl) ?? throw new HttpRequestException($"Failed: {Settings.Default.Plugins.DownloadUrl}"); + var json = await httpClient.GetFromJsonAsync(Settings.Default.Plugins.DownloadUrl) + ?? throw new HttpRequestException($"Failed: {Settings.Default.Plugins.DownloadUrl}"); + + var pluginVersionString = $"v{pluginVersion.ToString(3)}"; + var jsonRelease = json.AsArray() - .SingleOrDefault(s => - s != null && - s["tag_name"]!.GetValue() == $"v{pluginVersion.ToString(3)}" && - s["prerelease"]!.GetValue() == prerelease) ?? throw new Exception($"Could not find Release {pluginVersion}"); + .FirstOrDefault(s => + s?["tag_name"]?.GetValue() == pluginVersionString && + s["prerelease"]?.GetValue() == prerelease); if (jsonRelease == null) { - // If the corresponding version of the plugin is not found, get the latest version jsonRelease = json.AsArray() - .OrderByDescending(s => Version.Parse(s["tag_name"]!.GetValue().TrimStart('v'))) + .Where(s => s?["prerelease"]?.GetValue() == prerelease) + .Select(s => + { + var tagName = s["tag_name"]?.GetValue(); + return Version.TryParse(tagName?[1..], out var version) + ? new { JsonObject = s, Version = version } + : null; + }) + .OfType() + .OrderByDescending(s => s.Version) + .Select(s => s.JsonObject) .FirstOrDefault(); if (jsonRelease != null) { - var latestVersion = Version.Parse(jsonRelease["tag_name"]!.GetValue().TrimStart('v')); + + var latestVersion = Version.Parse(jsonRelease["tag_name"]!.GetValue()[1..]); if (latestVersion < pluginVersion) { - // If the latest version is lower than the locally passed version, use https://github.com/neo-project/neo-modules/releases/latest/download to get the latest version - var latestDownloadUrl = $"https://github.com/neo-project/neo-modules/releases/latest/download/{pluginName}.zip"; + var latestDownloadUrl = $"https://github.com/neo-project/neo-modules/releases/download/v{latestVersion}/{pluginName}.zip"; + ConsoleHelper.Info($"Could not find the corresponding version, installing the latest: v{latestVersion}"); return await httpClient.GetStreamAsync(latestDownloadUrl); } } throw new Exception($"Could not find Release {pluginVersion}"); } - var jsonAssets = jsonRelease - .AsObject() - .SingleOrDefault(s => s.Key == "assets").Value ?? throw new Exception("Could not find any Plugins"); + + var jsonAssets = jsonRelease["assets"]?.AsArray() + ?? throw new Exception("Could not find any Plugins"); var jsonPlugin = jsonAssets - .AsArray() - .SingleOrDefault(s => - Path.GetFileNameWithoutExtension( - s!["name"]!.GetValue()).Equals(pluginName, StringComparison.InvariantCultureIgnoreCase)) + .FirstOrDefault(s => + Path.GetFileNameWithoutExtension(s?["name"]?.GetValue() ?? string.Empty) + .Equals(pluginName, StringComparison.OrdinalIgnoreCase)) ?? throw new Exception($"Could not find {pluginName}"); - var downloadUrl = jsonPlugin["browser_download_url"]!.GetValue(); + var downloadUrl = jsonPlugin["browser_download_url"]?.GetValue() + ?? throw new Exception("Could not find download URL"); return await httpClient.GetStreamAsync(downloadUrl); } @@ -132,7 +145,7 @@ private static async Task DownloadPluginAsync(string pluginName, Version /// Name of the plugin /// Dependency set /// Install by force for `update` - private async Task InstallPluginAsync( + public async Task InstallPluginAsync( string pluginName, HashSet? installed = null, bool overWrite = false) @@ -144,7 +157,9 @@ private async Task InstallPluginAsync( try { - using var stream = await DownloadPluginAsync(pluginName, Settings.Default.Plugins.Version, Settings.Default.Plugins.Prerelease); + Version v = new Version(3, 7, 5); + + using var stream = await DownloadPluginAsync(pluginName, v, Settings.Default.Plugins.Prerelease); using var zip = new ZipArchive(stream, ZipArchiveMode.Read); var entry = zip.Entries.FirstOrDefault(p => p.Name == "config.json");