diff --git a/neo-cli/Helper.cs b/neo-cli/Helper.cs new file mode 100644 index 000000000..a45a4dd3d --- /dev/null +++ b/neo-cli/Helper.cs @@ -0,0 +1,15 @@ +using System.Linq; +using System.Reflection; + +namespace Neo +{ + internal static class Helper + { + internal static string GetVersion(this Assembly assembly) + { + CustomAttributeData attribute = assembly.CustomAttributes.FirstOrDefault(p => p.AttributeType == typeof(AssemblyInformationalVersionAttribute)); + if (attribute == null) return assembly.GetName().Version.ToString(3); + return (string)attribute.ConstructorArguments[0].Value; + } + } +} diff --git a/neo-cli/Settings.cs b/neo-cli/Settings.cs index b0eddbd60..0420b0d7c 100644 --- a/neo-cli/Settings.cs +++ b/neo-cli/Settings.cs @@ -1,6 +1,6 @@ -using System.Net; -using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration; using Neo.Network.P2P; +using System.Net; namespace Neo { @@ -9,7 +9,8 @@ internal class Settings public PathsSettings Paths { get; } public P2PSettings P2P { get; } public RPCSettings RPC { get; } - public UnlockWalletSettings UnlockWallet { get; set; } + public UnlockWalletSettings UnlockWallet { get; } + public string PluginURL { get; } public static Settings Default { get; } @@ -25,6 +26,7 @@ public Settings(IConfigurationSection section) this.P2P = new P2PSettings(section.GetSection("P2P")); this.RPC = new RPCSettings(section.GetSection("RPC")); this.UnlockWallet = new UnlockWalletSettings(section.GetSection("UnlockWallet")); + this.PluginURL = section.GetSection("PluginURL").Value; } } diff --git a/neo-cli/Shell/MainService.cs b/neo-cli/Shell/MainService.cs index 435283592..6a72358c6 100644 --- a/neo-cli/Shell/MainService.cs +++ b/neo-cli/Shell/MainService.cs @@ -15,6 +15,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.IO.Compression; using System.Linq; using System.Net; using System.Security.Cryptography; @@ -89,6 +90,10 @@ protected override bool OnCommand(string[] args) return OnStartCommand(args); case "upgrade": return OnUpgradeCommand(args); + case "install": + return OnInstallCommand(args); + case "uninstall": + return OnUnInstallCommand(args); default: return base.OnCommand(args); } @@ -392,7 +397,6 @@ private bool OnHelpCommand(string[] args) "Normal Commands:\n" + "\tversion\n" + "\thelp [plugin-name]\n" + - "\tplugins\n" + "\tclear\n" + "\texit\n" + "Wallet Commands:\n" + @@ -416,6 +420,10 @@ private bool OnHelpCommand(string[] args) "\tshow state\n" + "\tshow pool [verbose]\n" + "\trelay \n" + + "Plugin Commands:\n" + + "\tplugins\n" + + "\tinstall \n" + + "\tuninstall \n" + "Advanced Commands:\n" + "\tstart consensus\n"); @@ -941,6 +949,53 @@ private bool OnUpgradeCommand(string[] args) } } + private bool OnInstallCommand(string[] args) + { + if (args.Length < 2) + { + Console.WriteLine("error"); + return true; + } + var pluginName = args[1]; + var address = string.Format(Settings.Default.PluginURL, pluginName, typeof(Plugin).Assembly.GetVersion()); + var fileName = Path.Combine("Plugins", $"{pluginName}.zip"); + Directory.CreateDirectory("Plugins"); + Console.WriteLine($"Downloading from {address}"); + using (WebClient wc = new WebClient()) + { + wc.DownloadFile(address, fileName); + } + try + { + ZipFile.ExtractToDirectory(fileName, "."); + } + catch (IOException) + { + Console.WriteLine($"Plugin already exist."); + return true; + } + finally + { + File.Delete(fileName); + } + Console.WriteLine($"Install successful, please restart neo-cli."); + return true; + } + + private bool OnUnInstallCommand(string[] args) + { + if (args.Length < 2) + { + Console.WriteLine("error"); + return true; + } + var pluginName = args[1]; + Directory.Delete(Path.Combine("Plugins", pluginName), true); + File.Delete(Path.Combine("Plugins", $"{pluginName}.dll")); + Console.WriteLine($"Uninstall successful, please restart neo-cli."); + return true; + } + private bool OnUpgradeWalletCommand(string[] args) { if (args.Length < 3) diff --git a/neo-cli/config.json b/neo-cli/config.json index 0b6841950..2c3b065e8 100644 --- a/neo-cli/config.json +++ b/neo-cli/config.json @@ -19,6 +19,7 @@ "Password": "", "StartConsensus": false, "IsActive": false - } + }, + "PluginURL": "https://github.com/neo-project/neo-plugins/releases/download/v{1}/{0}.zip" } } diff --git a/neo-cli/config.mainnet.json b/neo-cli/config.mainnet.json index f14d6d54f..7433243d0 100644 --- a/neo-cli/config.mainnet.json +++ b/neo-cli/config.mainnet.json @@ -19,6 +19,7 @@ "Password": "", "StartConsensus": false, "IsActive": false - } + }, + "PluginURL": "https://github.com/neo-project/neo-plugins/releases/download/v{1}/{0}.zip" } } diff --git a/neo-cli/config.testnet.json b/neo-cli/config.testnet.json index cb56791fc..672ebfa68 100644 --- a/neo-cli/config.testnet.json +++ b/neo-cli/config.testnet.json @@ -19,6 +19,7 @@ "Password": "", "StartConsensus": false, "IsActive": false - } + }, + "PluginURL": "https://github.com/neo-project/neo-plugins/releases/download/v{1}/{0}.zip" } }