From 0d7b50cc17cd8a9e6909e86c2a9758abdb65efa7 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 6 May 2021 17:01:16 +0800 Subject: [PATCH] ABP CLI prompt command Resolve #8886 --- .../Volo/Abp/Cli/AbpCliCoreModule.cs | 1 + .../Volo/Abp/Cli/CliService.cs | 47 ++++++++++++++++++- .../Volo/Abp/Cli/Commands/PromptCommand.cs | 33 +++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/PromptCommand.cs 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 ce6ab1e831..b3de9d71ff 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 @@ -29,6 +29,7 @@ namespace Volo.Abp.Cli { //TODO: Define constants like done for GenerateProxyCommand.Name. options.Commands["help"] = typeof(HelpCommand); + options.Commands["prompt"] = typeof(PromptCommand); options.Commands["new"] = typeof(NewCommand); options.Commands["get-source"] = typeof(GetSourceCommand); options.Commands["update"] = typeof(UpdateCommand); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs index a847881d60..b2588f4502 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs @@ -48,10 +48,14 @@ namespace Volo.Abp.Cli } #endif var commandLineArgs = CommandLineArgumentParser.Parse(args); - + try { - if (commandLineArgs.IsCommand("batch")) + if (commandLineArgs.IsCommand("prompt")) + { + await RunPromptAsync(); + } + else if (commandLineArgs.IsCommand("batch")) { await RunBatchAsync(commandLineArgs); } @@ -70,6 +74,45 @@ namespace Volo.Abp.Cli } } + private async Task RunPromptAsync() + { + string GetPromptInput() + { + Console.WriteLine("Enter the command to execute or `exit` to exit the prompt model"); + Console.Write("> "); + return Console.ReadLine(); + } + + var promptInput = GetPromptInput(); + do + { + try + { + var commandLineArgs = CommandLineArgumentParser.Parse(promptInput.Split(" ").Where(x => !x.IsNullOrWhiteSpace()).ToArray()); + + if (commandLineArgs.IsCommand("batch")) + { + await RunBatchAsync(commandLineArgs); + } + else + { + await RunInternalAsync(commandLineArgs); + } + } + catch (CliUsageException usageException) + { + Logger.LogWarning(usageException.Message); + } + catch (Exception ex) + { + Logger.LogException(ex); + } + + promptInput = GetPromptInput(); + + } while (promptInput?.ToLower() != "exit"); + } + private async Task RunBatchAsync(CommandLineArgs commandLineArgs) { var targetFile = commandLineArgs.Target; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/PromptCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/PromptCommand.cs new file mode 100644 index 0000000000..f6258c4b12 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/PromptCommand.cs @@ -0,0 +1,33 @@ +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.Cli.Args; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.Commands +{ + public class PromptCommand : IConsoleCommand, ITransientDependency + { + public Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + return Task.CompletedTask; + } + + public string GetUsageInfo() + { + var sb = new StringBuilder(); + + sb.AppendLine(""); + sb.AppendLine("Usage:"); + sb.AppendLine(" abp prompt"); + sb.AppendLine(""); + sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); + + return sb.ToString(); + } + + public string GetShortDescription() + { + return "Starts with prompt mode."; + } + } +}