forked from dotnet/templating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutableCommand.cs
50 lines (41 loc) · 2.04 KB
/
ExecutableCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.CommandLine;
using System.CommandLine.Invocation;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
namespace Microsoft.TemplateEngine.Authoring.CLI.Commands
{
/// <summary>
/// Represents a <see cref="Command"/> together with its action.
/// </summary>
internal abstract class ExecutableCommand<TModel> : Command where TModel : class
{
internal ExecutableCommand(string name, string? description = null)
: base(name, description)
{
Action = new CommandAction(this);
}
/// <summary>
/// Parses the context from <see cref="ParseResult"/>.
/// </summary>
protected internal abstract TModel ParseContext(ParseResult parseResult);
/// <summary>
/// Executes the command on the parsed context.
/// </summary>
protected abstract Task<int> ExecuteAsync(TModel args, ILoggerFactory loggerFactory, CancellationToken cancellationToken);
private sealed class CommandAction : AsynchronousCommandLineAction
{
private readonly ExecutableCommand<TModel> _command;
public CommandAction(ExecutableCommand<TModel> command) => _command = command;
public int Invoke(ParseResult parseResult) => InvokeAsync(parseResult).GetAwaiter().GetResult();
public override async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken = default)
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole(c => c.ColorBehavior = LoggerColorBehavior.Disabled));
TModel arguments = _command.ParseContext(parseResult);
//exceptions are handled by parser itself
return await _command.ExecuteAsync(arguments, loggerFactory, cancellationToken).ConfigureAwait(false);
}
}
}
}