Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>37c0371ab3f7651d64a6dfe4c2e8677206fa34ee</Sha>
</Dependency>
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.23165.3">
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.23307.1">
<Uri>https://github.com/dotnet/command-line-api</Uri>
<Sha>42c58533cdf478b15120542be76f7cbaacaab024</Sha>
<Sha>02fe27cd6a9b001c8feb7938e6ef4b3799745759</Sha>
</Dependency>
</ProductDependencies>
<ToolsetDependencies>
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PropertyGroup>
<!-- Maestro-managed Package Versions - Ordered by repo name -->
<!-- Dependencies from https://github.com/dotnet/command-line-api -->
<SystemCommandLinePackageVersion>2.0.0-beta4.23165.3</SystemCommandLinePackageVersion>
<SystemCommandLinePackageVersion>2.0.0-beta4.23307.1</SystemCommandLinePackageVersion>
<!-- Dependencies from https://github.com/dotnet/runtime -->
<MicrosoftNETCoreAppRefPackageVersion>8.0.0-preview.6.23309.7</MicrosoftNETCoreAppRefPackageVersion>
<MicrosoftNETCoreAppRuntimewinx64PackageVersion>8.0.0-preview.6.23309.7</MicrosoftNETCoreAppRuntimewinx64PackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ValidateCommandTests : TestBase
[Fact]
public async Task ValidateCommand_BasicTest_InvalidTemplate()
{
RootCommand root = new()
CliRootCommand root = new()
{
new ValidateCommand()
};
Expand All @@ -26,7 +26,7 @@ public async Task ValidateCommand_BasicTest_InvalidTemplate()
[Fact]
public async Task ValidateCommand_BasicTest_ValidTemplate()
{
RootCommand root = new()
CliRootCommand root = new()
{
new ValidateCommand()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,22 @@
// 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 handler.
/// Represents a <see cref="CliCommand"/> together with its action.
/// </summary>
internal abstract class ExecutableCommand<TModel> : Command, ICommandHandler where TModel : class
internal abstract class ExecutableCommand<TModel> : CliCommand where TModel : class
{
internal ExecutableCommand(string name, string? description = null)
: base(name, description)
{
Handler = this;
Action = new CommandAction(this);
}

/// <inheritdoc/>
public async Task<int> InvokeAsync(InvocationContext context, CancellationToken cancellationToken = default)
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole(c => c.ColorBehavior = LoggerColorBehavior.Disabled));
TModel arguments = ParseContext(context.ParseResult);

//exceptions are handled by parser itself
return await ExecuteAsync(arguments, loggerFactory, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc/>
public int Invoke(InvocationContext context) => InvokeAsync(context).GetAwaiter().GetResult();

/// <summary>
/// Parses the context from <see cref="ParseResult"/>.
/// </summary>
Expand All @@ -42,5 +28,22 @@ public async Task<int> InvokeAsync(InvocationContext context, CancellationToken
/// </summary>
protected abstract Task<int> ExecuteAsync(TModel args, ILoggerFactory loggerFactory, CancellationToken cancellationToken);

private sealed class CommandAction : CliAction
{
private readonly ExecutableCommand<TModel> _command;

public CommandAction(ExecutableCommand<TModel> command) => _command = command;

public override 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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,72 @@ internal class VerifyCommand : ExecutableCommand<VerifyCommandArgs>
{
private const string CommandName = "verify";

private readonly Argument<string> _templateNameArgument = new("template-short-name")
private readonly CliArgument<string> _templateNameArgument = new("template-short-name")
{
Description = LocalizableStrings.command_verify_help_templateName_description,
// 0 for case where only path is specified
Arity = new ArgumentArity(1, 1)
};

private readonly Option<string> _remainingArguments = new Option<string>("template-args", new[] { "--template-args" })
private readonly CliOption<string> _remainingArguments = new("--template-args")
{
Description = "Template specific arguments - all joined into single enquoted string. Any needed quotations of actual arguments has to be escaped.",
Arity = new ArgumentArity(0, 1)
};

private readonly Option<string> _templatePathOption = new("template-path", new[] { "-p", "--template-path" })
private readonly CliOption<string> _templatePathOption = new("--template-path", "-p")
{
Description = LocalizableStrings.command_verify_help_templatePath_description,
};

private readonly Option<string> _templateOutputPathOption = new("output", new[] { "-o", "--output" })
private readonly CliOption<string> _templateOutputPathOption = new("--output", "-o")
{
Description = LocalizableStrings.command_verify_help_outputPath_description,
};

private readonly Option<string> _snapshotsDirectoryOption = new("snapshots-directory", new[] { "-d", "--snapshots-directory" })
private readonly CliOption<string> _snapshotsDirectoryOption = new("--snapshots-directory", "-d")
{
Description = LocalizableStrings.command_verify_help_snapshotsDirPath_description,
};

private readonly Option<string> _scenarioNameOption = new("scenario-name", new[] { "--scenario-name" })
private readonly CliOption<string> _scenarioNameOption = new("--scenario-name")
{
Description = LocalizableStrings.command_verify_help_scenarioName_description,
};

private readonly Option<bool> _disableDiffToolOption = new("disable-diff-tool", new[] { "--disable-diff-tool" })
private readonly CliOption<bool> _disableDiffToolOption = new("--disable-diff-tool")
{
Description = LocalizableStrings.command_verify_help_disableDiffTool_description,
};

private readonly Option<bool> _disableDefaultExcludePatternsOption = new("disable-default-exclude-patterns", new[] { "--disable-default-exclude-patterns" })
private readonly CliOption<bool> _disableDefaultExcludePatternsOption = new("--disable-default-exclude-patterns")
{
Description = LocalizableStrings.command_verify_help_disableDefaultExcludes_description,
};

private readonly Option<IEnumerable<string>> _excludePatternOption = new("exclude-pattern", new[] { "--exclude-pattern" })
private readonly CliOption<IEnumerable<string>> _excludePatternOption = new("--exclude-pattern")
{
Description = LocalizableStrings.command_verify_help_customExcludes_description,
Arity = new ArgumentArity(0, 999)
};

private readonly Option<IEnumerable<string>> _includePatternOption = new("include-pattern", new[] { "--include-pattern" })
private readonly CliOption<IEnumerable<string>> _includePatternOption = new("--include-pattern")
{
Description = LocalizableStrings.command_verify_help_customIncludes_description,
Arity = new ArgumentArity(0, 999)
};

private readonly Option<bool> _verifyCommandOutputOption = new("verify-std", new[] { "--verify-std" })
private readonly CliOption<bool> _verifyCommandOutputOption = new("--verify-std")
{
Description = LocalizableStrings.command_verify_help_verifyOutputs_description,
};

private readonly Option<bool> _isCommandExpectedToFailOption = new("fail-expected", new[] { "--fail-expected" })
private readonly CliOption<bool> _isCommandExpectedToFailOption = new("--fail-expected")
{
Description = LocalizableStrings.command_verify_help_expectFailure_description,
};

private readonly Option<IEnumerable<UniqueForOption>> _uniqueForOption = new("unique-for", new[] { "--unique-for" })
private readonly CliOption<IEnumerable<UniqueForOption>> _uniqueForOption = new("--unique-for")
{
Description = LocalizableStrings.command_verify_help_uniqueFor_description,
Arity = new ArgumentArity(0, 999),
Expand Down Expand Up @@ -171,9 +171,9 @@ await engine.Execute(
}

/// <summary>
/// Case insensitive version for <see cref="Option{T}.AcceptOnlyFromAmong(string[])"/>.
/// Case insensitive version for <see cref="CliOption{T}.AcceptOnlyFromAmong(string[])"/>.
/// </summary>
private static void FromAmongCaseInsensitive(Option<IEnumerable<UniqueForOption>> option, string[]? allowedValues = null, string? allowedHiddenValue = null)
private static void FromAmongCaseInsensitive(CliOption<IEnumerable<UniqueForOption>> option, string[]? allowedValues = null, string? allowedHiddenValue = null)
{
allowedValues ??= Array.Empty<string>();
option.Validators.Add(optionResult => ValidateAllowedValues(optionResult, allowedValues, allowedHiddenValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace Microsoft.TemplateEngine.Authoring.CLI.Commands
{
internal class LocalizeCommand : Command
internal class LocalizeCommand : CliCommand
{
internal LocalizeCommand()
: base("localize")
{
this.Subcommands.Add(new ExportCommand());
Subcommands.Add(new ExportCommand());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ internal sealed class ExportCommand : ExecutableCommand<ExportCommandArgs>
{
private const string CommandName = "export";

private readonly Argument<IEnumerable<string>> _templatePathArgument = new("template-path")
private readonly CliArgument<IEnumerable<string>> _templatePathArgument = new("template-path")
{
Arity = ArgumentArity.OneOrMore,
Description = LocalizableStrings.command_export_help_templatePath_description,
};

private readonly Option<IEnumerable<string>> _languageOption = new("language", new[] { "--language", "-l" })
private readonly CliOption<IEnumerable<string>> _languageOption = new("--language", "-l")
{
Description = LocalizableStrings.command_export_help_language_description,
Arity = ArgumentArity.OneOrMore,
AllowMultipleArgumentsPerToken = true,
};

private readonly Option<bool> _recursiveOption = new("recursive", new[] { "--recursive", "-r" })
private readonly CliOption<bool> _recursiveOption = new("recursive", new[] { "--recursive", "-r" })
{
Description = LocalizableStrings.command_export_help_recursive_description,
};

private readonly Option<bool> _dryRunOption = new("dry-run", new[] { "--dry-run", "-d" })
private readonly CliOption<bool> _dryRunOption = new("--dry-run", "-d")
{
Description = LocalizableStrings.command_export_help_dryrun_description,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class ValidateCommand : ExecutableCommand<ValidateCommandArgs>
{
private const string CommandName = "validate";

private readonly Argument<string> _templateLocationArg = new("template-location")
private readonly CliArgument<string> _templateLocationArg = new("template-location")
{
Description = LocalizableStrings.command_validate_help_description,
Arity = new ArgumentArity(1, 1)
Expand Down
12 changes: 6 additions & 6 deletions tools/Microsoft.TemplateEngine.Authoring.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ internal sealed class Program
{
internal static Task<int> Main(string[] args)
{
RootCommand rootCommand = new("dotnet-template-authoring");
CliRootCommand rootCommand = new("dotnet-template-authoring");
rootCommand.Subcommands.Add(new LocalizeCommand());
rootCommand.Subcommands.Add(new VerifyCommand());
rootCommand.Subcommands.Add(new ValidateCommand());

return GetCommandLineConfiguration(rootCommand).InvokeAsync(args);
}

internal static CommandLineConfiguration GetCommandLineConfiguration(Command command)
internal static CliConfiguration GetCommandLineConfiguration(CliCommand command)
{
CommandLineBuilder builder = new CommandLineBuilder(command)
.UseDefaults()
.EnablePosixBundling(false);
return builder.Build();
return new CliConfiguration(command)
{
EnablePosixBundling = false
};
}
}
}
4 changes: 2 additions & 2 deletions tools/Microsoft.TemplateSearch.TemplateDiscovery/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal class Program
{
private static async Task<int> Main(string[] args)
{
Command rootCommand = new TemplateDiscoveryCommand();
return await rootCommand.InvokeAsync(args).ConfigureAwait(false);
CliCommand rootCommand = new TemplateDiscoveryCommand();
return await rootCommand.Parse(args).InvokeAsync().ConfigureAwait(false);
}
}
}
Loading