From f940f9142e214a6b07dae912b4b6be5e196e4778 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Thu, 12 Dec 2019 10:52:17 +0300 Subject: [PATCH] add Suite cli command to Abp Cli. --- .../Volo/Abp/Cli/AbpCliCoreModule.cs | 1 + .../Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs | 11 ++ .../Volo/Abp/Cli/Commands/SuiteCommand.cs | 136 ++++++++++++++++++ .../Volo/Abp/Cli/Utils/GlobalToolHelper.cs | 21 +++ .../Volo/Abp/Cli/Utils/PlatformHelper.cs | 51 +++++++ .../Properties/launchSettings.json | 8 ++ 6 files changed, 228 insertions(+) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/GlobalToolHelper.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/PlatformHelper.cs create mode 100644 framework/src/Volo.Abp.Cli/Properties/launchSettings.json diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index a616d29409..ac190cf154 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -33,6 +33,7 @@ namespace Volo.Abp.Cli options.Commands["add-module"] = typeof(AddModuleCommand); options.Commands["login"] = typeof(LoginCommand); options.Commands["logout"] = typeof(LogoutCommand); + options.Commands["suite"] = typeof(SuiteCommand); }); } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs index 3c034d5702..3081c77cb5 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs @@ -4,10 +4,21 @@ { #if DEBUG public const string WwwAbpIo = "https://localhost:44328/"; + public const string AccountAbpIo = "https://localhost:44333/"; + + public const string NuGetRootPath = "https://localhost:44373/"; #else public const string WwwAbpIo = "https://abp.io/"; + public const string AccountAbpIo = "https://account.abp.io/"; + + public const string NuGetRootPath = "https://nuget.abp.io/"; #endif + + public static string GetNuGetServiceIndexUrl(string apiKey) + { + return NuGetRootPath + apiKey + "/v3/index.json"; + } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs new file mode 100644 index 0000000000..a7c767ffc6 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs @@ -0,0 +1,136 @@ +using System; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Zip; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.Cli.Args; +using Volo.Abp.Cli.Licensing; +using Volo.Abp.Cli.ProjectBuilding; +using Volo.Abp.Cli.ProjectBuilding.Building; +using Volo.Abp.Cli.Utils; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.Commands +{ + public class SuiteCommand : IConsoleCommand, ITransientDependency + { + private const string SuitePackageName = "Volo.Abp.Suite"; + public ILogger Logger { get; set; } + private readonly IApiKeyService _apiKeyService; + + public SuiteCommand(IApiKeyService apiKeyService) + { + _apiKeyService = apiKeyService; + Logger = NullLogger.Instance; + } + + public async Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + var operationType = NamespaceHelper.NormalizeNamespace(commandLineArgs.Target); + + switch (operationType) + { + case "": + Logger.LogInformation("Running Suite..."); + RunSuite(); + break; + + case "install": + case "i": + Logger.LogInformation("Installing Suite..."); + await InstallSuiteAsync(); + break; + + case "update": + case "u": + Logger.LogInformation("Updating Suite..."); + UpdateSuite(); + break; + + case "remove": + case "r": + Logger.LogInformation("Removing Suite..."); + RemoveSuite(); + break; + } + } + + private async Task InstallSuiteAsync() + { + var apiKeyResult = await _apiKeyService.GetApiKeyOrNullAsync(); + if (apiKeyResult == null || string.IsNullOrEmpty(apiKeyResult.ApiKey)) + { + Logger.LogInformation("Couldn't retrieve the API Key for Nuget!"); + await Task.CompletedTask; + return; + } + + var nugetIndexUrl = CliUrls.GetNuGetServiceIndexUrl(apiKeyResult.ApiKey); + CmdHelper.RunCmd("dotnet tool install " + SuitePackageName + " --add-source " + nugetIndexUrl + " -g"); + } + + private static void UpdateSuite() + { + CmdHelper.RunCmd("dotnet tool update " + SuitePackageName + " -g"); + } + + private static void RemoveSuite() + { + CmdHelper.RunCmd("dotnet tool uninstall " + SuitePackageName + " -g"); + } + + private void RunSuite() + { + try + { + if (!GlobalToolHelper.IsGlobalToolInstalled("abp-suite")) + { + Logger.LogWarning("Suite is not installed! To install it you can run the command: \"abp suite install\""); + return; + } + } + catch (Exception ex) + { + Logger.LogWarning("Couldn't check Suite installed status: " + ex.Message); + } + + CmdHelper.RunCmd("abp-suite"); + } + + public string GetUsageInfo() + { + var sb = new StringBuilder(); + + sb.AppendLine(""); + sb.AppendLine("Usage:"); + sb.AppendLine(""); + sb.AppendLine(" abp suite [options]"); + sb.AppendLine(""); + sb.AppendLine("Options:"); + sb.AppendLine(""); + sb.AppendLine(" (runs Suite)"); + sb.AppendLine("-i|--install (installs Suite as a dotnet global tool)"); + sb.AppendLine("-u|--update (updates Suite to the latest)"); + sb.AppendLine("-r|--remove (uninstalls Suite)"); + sb.AppendLine(""); + sb.AppendLine("Examples:"); + sb.AppendLine(""); + sb.AppendLine(" abp suite"); + sb.AppendLine(" abp suite install"); + sb.AppendLine(" abp suite update"); + sb.AppendLine(""); + + return sb.ToString(); + } + + public string GetShortDescription() + { + return "Shortcut commands to use Abp Suite tool."; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/GlobalToolHelper.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/GlobalToolHelper.cs new file mode 100644 index 0000000000..325d73a99b --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/GlobalToolHelper.cs @@ -0,0 +1,21 @@ +using System.IO; + +namespace Volo.Abp.Cli.Utils +{ + public class GlobalToolHelper + { + /// + /// Checks whether the tool is installed or not. + /// + /// Eg: For AbpSuite tool it's "abp-suite", for ABP CLI tool it's "abp" + public static bool IsGlobalToolInstalled(string toolCommandName) + { + if (PlatformHelper.GetPlatform() == RuntimePlatform.LinuxOrMacOs) + { + return File.Exists("%HOME%/.dotnet/tools/" + toolCommandName); + } + + return File.Exists(@"%USERPROFILE%\.dotnet\tools\" + toolCommandName + ".exe"); + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/PlatformHelper.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/PlatformHelper.cs new file mode 100644 index 0000000000..09d2481782 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/PlatformHelper.cs @@ -0,0 +1,51 @@ +using System; +using System.Runtime.InteropServices; + +namespace Volo.Abp.Cli.Utils +{ + public class PlatformHelper + { + public static OSPlatform GetOperatingSystem() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + //MAC + return OSPlatform.OSX; + } + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return OSPlatform.Linux; + } + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return OSPlatform.Windows; + } + + throw new Exception("Cannot determine operating system!"); + } + + public static RuntimePlatform GetPlatform() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || + RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return RuntimePlatform.LinuxOrMacOs; + } + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return RuntimePlatform.Windows; + } + + throw new Exception("Cannot determine runtime platform!"); + } + } + + public enum RuntimePlatform + { + Windows = 1, + LinuxOrMacOs = 2 + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli/Properties/launchSettings.json b/framework/src/Volo.Abp.Cli/Properties/launchSettings.json new file mode 100644 index 0000000000..23603f1358 --- /dev/null +++ b/framework/src/Volo.Abp.Cli/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "Volo.Abp.Cli": { + "commandName": "Project", + "commandLineArgs": "suite i" + } + } +} \ No newline at end of file