Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install latest plugin when the modules are not released for the latest neo-cli #3229

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 63 additions & 34 deletions src/Neo.CLI/CLI/MainService.Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,58 @@ private static async Task<Stream> 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<JsonArray>(Settings.Default.Plugins.DownloadUrl) ?? throw new HttpRequestException($"Failed: {Settings.Default.Plugins.DownloadUrl}");
var json = await httpClient.GetFromJsonAsync<JsonArray>(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<string>() == $"v{pluginVersion.ToString(3)}" &&
s["prerelease"]!.GetValue<bool>() == prerelease) ?? throw new Exception($"Could not find Release {pluginVersion}");
.FirstOrDefault(s =>
s?["tag_name"]?.GetValue<string>() == pluginVersionString &&
s["prerelease"]?.GetValue<bool>() == prerelease);
shargon marked this conversation as resolved.
Show resolved Hide resolved

if (jsonRelease == null)
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
{
jsonRelease = json.AsArray()
.Where(s => s?["prerelease"]?.GetValue<bool>() == prerelease)
.Select(s =>
{
var tagName = s?["tag_name"]?.GetValue<string>();
return Version.TryParse(tagName?[1..], out var version)
? new { JsonObject = s, Version = version }
: null;
})
.OfType<dynamic>()
.OrderByDescending(s => s.Version)
.Select(s => s.JsonObject)
.FirstOrDefault();

if (jsonRelease != null)
{

shargon marked this conversation as resolved.
Show resolved Hide resolved
var latestVersion = Version.Parse(jsonRelease["tag_name"]!.GetValue<string>()[1..]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this 1? Start with v?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Version.TryParse instead of Version.Parse?

Copy link
Member

@cschuchardt88 cschuchardt88 Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version.TryParse is good

if (latestVersion < pluginVersion)
{
var latestDownloadUrl = $"https://github.com/neo-project/neo/releases/download/v{latestVersion}/{pluginName}.zip";
ConsoleHelper.Info($"Could not find the corresponding version, installing the latest: v{latestVersion}");
return await httpClient.GetStreamAsync(latestDownloadUrl);
}
Comment on lines +117 to +122
Copy link
Member

@superboyiii superboyiii May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need take care of (latestVersion > pluginVersion), why not always pick the latestVersion?

}

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<string>()).Equals(pluginName, StringComparison.InvariantCultureIgnoreCase))
.FirstOrDefault(s =>
Path.GetFileNameWithoutExtension(s?["name"]?.GetValue<string>() ?? string.Empty)
.Equals(pluginName, StringComparison.InvariantCultureIgnoreCase))
?? throw new Exception($"Could not find {pluginName}");

var downloadUrl = jsonPlugin["browser_download_url"]!.GetValue<string>();
var downloadUrl = jsonPlugin["browser_download_url"]?.GetValue<string>()
?? throw new Exception("Could not find download URL");

return await httpClient.GetStreamAsync(downloadUrl);
}
Expand All @@ -112,7 +145,7 @@ private static async Task<Stream> DownloadPluginAsync(string pluginName, Version
/// <param name="pluginName">Name of the plugin</param>
/// <param name="installed">Dependency set</param>
/// <param name="overWrite">Install by force for `update`</param>
private async Task<bool> InstallPluginAsync(
public async Task<bool> InstallPluginAsync(
string pluginName,
HashSet<string>? installed = null,
bool overWrite = false)
Expand Down Expand Up @@ -228,28 +261,24 @@ private void OnPluginsCommand()
{
try
{
var plugins = GetPluginListAsync().GetAwaiter().GetResult()?.ToArray() ?? [];
var installedPlugins = Plugin.Plugins.ToList();

var maxLength = installedPlugins.Count == 0 ? 0 : installedPlugins.Max(s => s.Name.Length);
if (plugins.Length > 0)
var plugins = GetPluginListAsync().GetAwaiter().GetResult();
if (plugins == null) return;
plugins
.Order()
.ForEach(f =>
{
maxLength = Math.Max(maxLength, plugins.Max(s => s.Length));
}

plugins.Select(s => (name: s, installedPlugin: Plugin.Plugins.SingleOrDefault(pp => string.Equals(pp.Name, s, StringComparison.InvariantCultureIgnoreCase))))
.Concat(installedPlugins.Select(u => (name: u.Name, installedPlugin: (Plugin?)u)).Where(u => !plugins.Contains(u.name, StringComparer.InvariantCultureIgnoreCase)))
.OrderBy(u => u.name)
.ForEach((f) =>
var installedPlugin = Plugin.Plugins.SingleOrDefault(pp => string.Equals(pp.Name, f, StringComparison.CurrentCultureIgnoreCase));
if (installedPlugin != null)
{
if (f.installedPlugin != null)
{
var tabs = f.name.Length < maxLength ? "\t" : string.Empty;
ConsoleHelper.Info("", $"[Installed]\t {f.name,6}{tabs}", " @", $"{f.installedPlugin.Version.ToString(3)} {f.installedPlugin.Description}");
}
else
ConsoleHelper.Info($"[Not Installed]\t {f.name}");
});
var maxLength = plugins.Select(s => s.Length).OrderDescending().First();
string tabs = string.Empty;
if (f.Length < maxLength)
tabs = "\t";
ConsoleHelper.Info("", $"[Installed]\t {f,6}{tabs}", " @", $"{installedPlugin.Version.ToString(3)} {installedPlugin.Description}");
}
else
ConsoleHelper.Info($"[Not Installed]\t {f}");
});
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.fs.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"NeoNameService": "0x7061fbd31562664b58f422c3dee4acfd70dba8af"
},
"Plugins": {
"DownloadUrl": "https://api.github.com/repos/neo-project/neo-modules/releases"
"DownloadUrl": "https://api.github.com/repos/neo-project/neo/releases"
}
},
"ProtocolConfiguration": {
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.fs.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"NeoNameService": "0xfb08ccf30ab534a871b7b092a49fe70c154ed678"
},
"Plugins": {
"DownloadUrl": "https://api.github.com/repos/neo-project/neo-modules/releases"
"DownloadUrl": "https://api.github.com/repos/neo-project/neo/releases"
}
},
"ProtocolConfiguration": {
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"NeoNameService": "0x50ac1c37690cc2cfc594472833cf57505d5f46de"
},
"Plugins": {
"DownloadUrl": "https://api.github.com/repos/neo-project/neo-modules/releases"
"DownloadUrl": "https://api.github.com/repos/neo-project/neo/releases"
}
},
"ProtocolConfiguration": {
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"NeoNameService": "0x50ac1c37690cc2cfc594472833cf57505d5f46de"
},
"Plugins": {
"DownloadUrl": "https://api.github.com/repos/neo-project/neo-modules/releases"
"DownloadUrl": "https://api.github.com/repos/neo-project/neo/releases"
}
},
"ProtocolConfiguration": {
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"NeoNameService": "0x50ac1c37690cc2cfc594472833cf57505d5f46de"
},
"Plugins": {
"DownloadUrl": "https://api.github.com/repos/neo-project/neo-modules/releases"
"DownloadUrl": "https://api.github.com/repos/neo-project/neo/releases"
}
},
"ProtocolConfiguration": {
Expand Down