Browse Source

Add `CliCommand`.

Resolve #12745
pull/12920/head
maliming 4 years ago
parent
commit
233637ee92
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 20
      docs/en/CLI.md
  2. 1
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  3. 181
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs

20
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).

1
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<AbpCliServiceProxyOptions>(options =>

181
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<CliCommand> Logger { get; set; }
public CliCommand(ICmdHelper cmdHelper, NuGetService nuGetService, AbpNuGetIndexUrlService nuGetIndexUrlService)
{
_cmdHelper = cmdHelper;
_nuGetService = nuGetService;
_nuGetIndexUrlService = nuGetIndexUrlService;
Logger = NullLogger<CliCommand>.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<string> 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";
}
}
}
Loading…
Cancel
Save