Browse Source

Resolved #8885: ABP CLI batch command

pull/8893/head
Halil İbrahim Kalkan 5 years ago
parent
commit
0862dc148a
  1. 71
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs

71
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs

@ -41,33 +41,76 @@ namespace Volo.Abp.Cli
{ {
Logger.LogInformation("ABP CLI (https://abp.io)"); Logger.LogInformation("ABP CLI (https://abp.io)");
var commandLineArgs = CommandLineArgumentParser.Parse(args);
#if !DEBUG #if !DEBUG
if (!commandLineArgs.Options.ContainsKey("skip-cli-version-check")) if (!commandLineArgs.Options.ContainsKey("skip-cli-version-check"))
{ {
await CheckCliVersionAsync(); await CheckCliVersionAsync();
} }
#endif #endif
var commandLineArgs = CommandLineArgumentParser.Parse(args);
var commandType = CommandSelector.Select(commandLineArgs);
try
using (var scope = ServiceScopeFactory.CreateScope())
{ {
var command = (IConsoleCommand)scope.ServiceProvider.GetRequiredService(commandType); if (commandLineArgs.IsCommand("batch"))
try
{ {
await command.ExecuteAsync(commandLineArgs); await RunBatchAsync(commandLineArgs);
} }
catch (CliUsageException usageException) else
{ {
Logger.LogWarning(usageException.Message); await RunInternalAsync(commandLineArgs);
} }
catch (Exception ex) }
catch (CliUsageException usageException)
{
Logger.LogWarning(usageException.Message);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
private async Task RunBatchAsync(CommandLineArgs commandLineArgs)
{
var targetFile = commandLineArgs.Target;
if (targetFile.IsNullOrWhiteSpace())
{
throw new CliUsageException(
"Must provide a file name/path that contains a list of commands" +
Environment.NewLine + Environment.NewLine +
"Example: " +
" abp batch commands.txt"
);
}
var filePath = Path.Combine(Directory.GetCurrentDirectory(), targetFile);
var fileLines = File.ReadAllLines(filePath);
foreach (var line in fileLines)
{
var lineText = line;
if (lineText.IsNullOrWhiteSpace() || lineText.StartsWith("#"))
{ {
Logger.LogException(ex); continue;
} }
if (lineText.Contains('#'))
{
lineText = lineText.Substring(0, lineText.IndexOf('#'));
}
var args = CommandLineArgumentParser.Parse(lineText.Split(' '));
await RunInternalAsync(args);
}
}
private async Task RunInternalAsync(CommandLineArgs commandLineArgs)
{
var commandType = CommandSelector.Select(commandLineArgs);
using (var scope = ServiceScopeFactory.CreateScope())
{
var command = (IConsoleCommand) scope.ServiceProvider.GetRequiredService(commandType);
await command.ExecuteAsync(commandLineArgs);
} }
} }

Loading…
Cancel
Save