Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Improve plugins installation #805

Merged
merged 4 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
74 changes: 38 additions & 36 deletions neo-cli/CLI/MainService.Plugins.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neo.ConsoleService;
using Neo.IO.Json;
using Neo.Plugins;
using System;
using System.IO;
Expand All @@ -17,51 +18,52 @@ partial class MainService
[ConsoleCommand("install", Category = "Plugin Commands")]
private void OnInstallCommand(string pluginName)
{
bool isTemp;
string fileName;

if (!File.Exists(pluginName))
{
if (string.IsNullOrEmpty(Settings.Default.PluginURL))
{
Console.WriteLine("You must define `PluginURL` in your `config.json`");
return;
}

var address = string.Format(Settings.Default.PluginURL, pluginName, typeof(Plugin).Assembly.GetVersion());
fileName = Path.Combine(Path.GetTempPath(), $"{pluginName}.zip");
isTemp = true;

Console.WriteLine($"Downloading from {address}");
using (WebClient wc = new WebClient())
{
wc.DownloadFile(address, fileName);
}
}
else
{
fileName = pluginName;
isTemp = false;
}

HttpWebRequest request = WebRequest.CreateHttp($"https://github.com/neo-project/neo-modules/releases/download/v{typeof(Plugin).Assembly.GetVersion()}/{pluginName}.zip");
HttpWebResponse response;
try
{
ZipFile.ExtractToDirectory(fileName, ".");
response = (HttpWebResponse)request.GetResponse();
}
catch (IOException)
catch (WebException ex) when (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine($"Plugin already exist.");
return;
Version version_core = typeof(Plugin).Assembly.GetName().Version;
request = WebRequest.CreateHttp($"https://api.github.com/repos/neo-project/neo-modules/releases");
request.UserAgent = $"{GetType().Assembly.GetName().Name}/{GetType().Assembly.GetVersion()}";
using HttpWebResponse response_api = (HttpWebResponse)request.GetResponse();
using Stream stream = response_api.GetResponseStream();
using StreamReader reader = new(stream);
JObject releases = JObject.Parse(reader.ReadToEnd());
try
{
JObject asset = releases.GetArray()
.Where(p => !p["tag_name"].GetString().Contains('-'))
.Select(p => new
{
Version = Version.Parse(p["tag_name"].GetString().TrimStart('v')),
Assets = p["assets"].GetArray()
})
.OrderByDescending(p => p.Version)
.First(p => p.Version <= version_core).Assets
.First(p => p["name"].GetString() == $"{pluginName}.zip");
request = WebRequest.CreateHttp(asset["browser_download_url"].GetString());
response = (HttpWebResponse)request.GetResponse();
}
catch (Exception) { throw new Exception("Plugin doesn't exist."); }
}
finally
using (response)
{
if (isTemp)
using Stream stream = response.GetResponseStream();
using ZipArchive zip = new(stream, ZipArchiveMode.Read);
try
{
File.Delete(fileName);
zip.ExtractToDirectory(".");
shargon marked this conversation as resolved.
Show resolved Hide resolved
Console.WriteLine($"Install successful, please restart neo-cli.");
}
catch (IOException)
{
Console.WriteLine($"Plugin already exist.");
}
}

Console.WriteLine($"Install successful, please restart neo-cli.");
}

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions neo-cli/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class Settings
public StorageSettings Storage { get; }
public P2PSettings P2P { get; }
public UnlockWalletSettings UnlockWallet { get; }
public string PluginURL { get; }

static Settings _default;

Expand Down Expand Up @@ -45,7 +44,6 @@ public Settings(IConfigurationSection section)
this.Storage = new StorageSettings(section.GetSection("Storage"));
this.P2P = new P2PSettings(section.GetSection("P2P"));
this.UnlockWallet = new UnlockWalletSettings(section.GetSection("UnlockWallet"));
this.PluginURL = section.GetValue("PluginURL", "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip");
}
}

Expand Down
3 changes: 1 addition & 2 deletions neo-cli/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"Path": "",
"Password": "",
"IsActive": false
},
"PluginURL": "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip"
}
},
"ProtocolConfiguration": {
"Network": 860833102,
Expand Down
3 changes: 1 addition & 2 deletions neo-cli/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"Path": "",
"Password": "",
"IsActive": false
},
"PluginURL": "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip"
}
},
"ProtocolConfiguration": {
"Network": 860833102,
Expand Down
3 changes: 1 addition & 2 deletions neo-cli/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"Path": "",
"Password": "",
"IsActive": false
},
"PluginURL": "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip"
}
},
"ProtocolConfiguration": {
"Network": 877933390,
Expand Down