From 233637ee926b4aa44e2a8f16e14e989e34eb5c99 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 7 Jun 2022 15:03:29 +0800 Subject: [PATCH] Add `CliCommand`. Resolve #12745 --- docs/en/CLI.md | 20 ++ .../Volo/Abp/Cli/AbpCliCoreModule.cs | 1 + .../Volo/Abp/Cli/Commands/CliCommand.cs | 181 ++++++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs diff --git a/docs/en/CLI.md b/docs/en/CLI.md index f2da02d889..9c06cd2a84 100644 --- a/docs/en/CLI.md +++ b/docs/en/CLI.md @@ -27,6 +27,7 @@ While each command may have a set of options, there are some global options that Here, is the list of all available commands before explaining their details: * **`help`**: Shows help on the usage of the ABP CLI. +* **`cli`**: Update or remove ABP CLI. * **`new`**: Generates a new solution based on the ABP [startup templates](Startup-Templates/Index.md). * **`update`**: Automatically updates all ABP related NuGet and NPM packages in a solution. * **`clean`**: Deletes all `BIN` and `OBJ` folders in the current folder. @@ -63,6 +64,25 @@ abp help # Shows a general help. abp help new # Shows help about the "new" command. ```` +### cli + +Update or remove ABP CLI. + +Usage: + +````bash +abp cli [command-name] +```` + +Examples: + +````bash +abp cli update +abp cli update --preview +abp cli update --version 5.0.0 +abp cli remove +```` + ### new Generates a new solution based on the ABP [startup templates](Startup-Templates/Index.md). 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 3e4649b6e3..4fda075521 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 @@ -64,6 +64,7 @@ public class AbpCliCoreModule : AbpModule options.Commands[CreateMigrationAndRunMigratorCommand.Name] = typeof(CreateMigrationAndRunMigratorCommand); options.Commands[InstallLibsCommand.Name] = typeof(InstallLibsCommand); options.Commands[CleanCommand.Name] = typeof(CleanCommand); + options.Commands[CliCommand.Name] = typeof(CliCommand); }); Configure(options => diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs new file mode 100644 index 0000000000..c0f832908e --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs @@ -0,0 +1,181 @@ +using System; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.Cli.Args; +using Volo.Abp.Cli.Commands.Services; +using Volo.Abp.Cli.NuGet; +using Volo.Abp.Cli.Utils; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.Commands; + +public class CliCommand : IConsoleCommand, ITransientDependency +{ + public const string Name = "cli"; + + private const string CliPackageName = "Volo.Abp.Cli"; + + private readonly ICmdHelper _cmdHelper; + private readonly NuGetService _nuGetService; + private readonly AbpNuGetIndexUrlService _nuGetIndexUrlService; + public ILogger Logger { get; set; } + + public CliCommand(ICmdHelper cmdHelper, NuGetService nuGetService, AbpNuGetIndexUrlService nuGetIndexUrlService) + { + _cmdHelper = cmdHelper; + _nuGetService = nuGetService; + _nuGetIndexUrlService = nuGetIndexUrlService; + + Logger = NullLogger.Instance; + } + + public async Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + var operationType = NamespaceHelper.NormalizeNamespace(commandLineArgs.Target); + + var preview = commandLineArgs.Options.ContainsKey(Options.Preview.Short) || + commandLineArgs.Options.ContainsKey(Options.Preview.Long); + + var version = commandLineArgs.Options.GetOrNull(Options.Version.Short, Options.Version.Long); + + switch (operationType) + { + case "": + case null: + _cmdHelper.RunCmd("abp"); + break; + + case "update": + await UpdateCliAsync(version, preview); + break; + + case "remove": + RemoveCli(); + break; + } + } + + private async Task UpdateCliAsync(string version = null, bool preview = false) + { + RemoveCli(); + var infoText = "Updating ABP CLI "; + if (version != null) + { + infoText += "to the " + version + "... "; + } + else if (preview) + { + infoText += "to the latest preview version..."; + } + else + { + infoText += "..."; + } + + Logger.LogInformation(infoText); + + try + { + var versionOption = string.Empty; + + if (preview) + { + var latestPreviewVersion = await GetLatestPreviewVersion(); + if (latestPreviewVersion != null) + { + versionOption = $" --version {latestPreviewVersion}"; + Logger.LogInformation("Latest preview version is " + latestPreviewVersion); + } + } + else if (version != null) + { + versionOption = $" --version {version}"; + } + + _cmdHelper.RunCmd( + $"dotnet tool update {CliPackageName}{versionOption} -g", + out var exitCode + ); + + if (exitCode != 0) + { + ShowCliManualUpdateCommand(); + } + } + catch (Exception ex) + { + Logger.LogError("Couldn't update ABP CLI." + ex.Message); + ShowCliManualUpdateCommand(); + } + } + + private async Task GetLatestPreviewVersion() + { + var latestPreviewVersion = await _nuGetService + .GetLatestVersionOrNullAsync( + packageId: CliPackageName, + includeReleaseCandidates: true + ); + + return latestPreviewVersion.IsPrerelease ? latestPreviewVersion.ToString() : null; + } + + private void ShowCliManualUpdateCommand() + { + Logger.LogError("You can also run the following command to update ABP CLI."); + Logger.LogError("dotnet tool update -g Volo.Abp.Cli"); + } + + private void RemoveCli() + { + Logger.LogInformation("Removing CLI..."); + _cmdHelper.RunCmd("dotnet tool uninstall " + CliPackageName + " -g"); + } + + public string GetUsageInfo() + { + var sb = new StringBuilder(); + + sb.AppendLine(""); + sb.AppendLine("Usage:"); + sb.AppendLine(""); + sb.AppendLine(" abp cli [options]"); + sb.AppendLine(""); + sb.AppendLine("Options:"); + sb.AppendLine(""); + sb.AppendLine("update (update ABP CLI to the latest)"); + sb.AppendLine("remove (uninstall ABP CLI)"); + sb.AppendLine(""); + sb.AppendLine("Examples:"); + sb.AppendLine(""); + sb.AppendLine(" abp cli update"); + sb.AppendLine(" abp cli update --preview"); + sb.AppendLine(" abp cli update --version 4.2.2"); + sb.AppendLine(" abp cli remove"); + sb.AppendLine(""); + + return sb.ToString(); + } + + public string GetShortDescription() + { + return "Update or remove ABP CLI. See https://docs.abp.io/en/abp/latest/CLI"; + } + + public static class Options + { + public static class Preview + { + public const string Long = "preview"; + public const string Short = "p"; + } + + public static class Version + { + public const string Long = "version"; + public const string Short = "v"; + } + } +}