diff --git a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/AppliedOptionsExtensions.cs b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/AppliedOptionsExtensions.cs new file mode 100644 index 000000000..a5e32d1ad --- /dev/null +++ b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/AppliedOptionsExtensions.cs @@ -0,0 +1,30 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Linq; +using Microsoft.DotNet.Cli.CommandLine; + +namespace Microsoft.DotNet.Tools.DependencyInvoker +{ + public static class AppliedOptionExtensions + { + public static T ValueOrDefault(this AppliedOption parseResult, string alias) + { + return parseResult + .AppliedOptions + .Where(o => o.HasAlias(alias)) + .Select(o => o.Value()) + .SingleOrDefault(); + } + + public static string SingleArgumentOrDefault(this AppliedOption parseResult, string alias) + { + return parseResult + .AppliedOptions + .Where(o => o.HasAlias(alias)) + .Select(o => o.Arguments.Single()) + .SingleOrDefault(); + } + } +} diff --git a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/DotnetBaseParams.cs b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/DotnetBaseParams.cs deleted file mode 100644 index 0d9e07748..000000000 --- a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/DotnetBaseParams.cs +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Collections.Generic; -using System.IO; -using Microsoft.DotNet.Cli.CommandLine; -using Microsoft.DotNet.Cli.Utils; -using NuGet.Frameworks; - -namespace Microsoft.DotNet.Tools.DependencyInvoker -{ - public class DotnetBaseParams - { - private readonly CommandLineApplication _app; - - private CommandOption _outputOption; - private CommandOption _buildBasePath; - private CommandOption _frameworkOption; - private CommandOption _runtimeOption; - private CommandOption _configurationOption; - private CommandOption _projectPath; - private CommandArgument _command; - - public string Runtime { get; set; } - - public string Config { get; set; } - - public string BuildBasePath { get; set; } - - public string Output { get; set; } - - public string ProjectPath { get; set; } - - public NuGetFramework Framework { get; set; } - - public string Command {get; set; } - - public List RemainingArguments { get; set; } - - public DotnetBaseParams(string name, string fullName, string description) - { - _app = new CommandLineApplication(false) - { - Name = name, - FullName = fullName, - Description = description - }; - - AddDotnetBaseParameters(); - } - - public void Parse(string[] args) - { - _app.OnExecute(() => - { - // Locate the project and get the name and full path - ProjectPath = _projectPath.Value(); - Output = _outputOption.Value(); - BuildBasePath = _buildBasePath.Value(); - Config = _configurationOption.Value() ?? Constants.DefaultConfiguration; - Runtime = _runtimeOption.Value(); - if (_frameworkOption.HasValue()) - { - Framework = NuGetFramework.Parse(_frameworkOption.Value()); - } - Command = _command.Value; - RemainingArguments = _app.RemainingArguments; - - if (string.IsNullOrEmpty(ProjectPath)) - { - ProjectPath = Directory.GetCurrentDirectory(); - } - - return 0; - }); - - _app.Execute(args); - } - - private void AddDotnetBaseParameters() - { - _app.HelpOption("-?|-h|--help"); - - _configurationOption = _app.Option( - "-c|--configuration ", - "Configuration under which to build", - CommandOptionType.SingleValue); - _outputOption = _app.Option( - "-o|--output ", - "Directory in which to find the binaries to be run", - CommandOptionType.SingleValue); - _buildBasePath = _app.Option( - "-b|--build-base-path ", - "Directory in which to find temporary outputs", - CommandOptionType.SingleValue); - _frameworkOption = _app.Option( - "-f|--framework ", - "Looks for test binaries for a specific framework", - CommandOptionType.SingleValue); - _runtimeOption = _app.Option( - "-r|--runtime ", - "Look for test binaries for a for the specified runtime", - CommandOptionType.SingleValue); - _projectPath = _app.Option( - "-p|--project-path ", - "Path to Project.json that contains the tool dependency", - CommandOptionType.SingleValue); - _command = _app.Argument( - "", - "The command to execute."); - } - } -} diff --git a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/DotnetDependencyToolInvokerParser.cs b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/DotnetDependencyToolInvokerParser.cs new file mode 100644 index 000000000..31f29c70f --- /dev/null +++ b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/DotnetDependencyToolInvokerParser.cs @@ -0,0 +1,58 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.IO; +using System.Linq; +using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Tools.Common; +using Microsoft.DotNet.Cli.Utils; +using NuGet.Frameworks; + +namespace Microsoft.DotNet.Tools.DependencyInvoker +{ + internal static class DotnetDependencyToolInvokerParser + { + public static Microsoft.DotNet.Cli.CommandLine.Command DotnetDependencyToolInvoker() => + Create.Command( + "dotnet-dependency-tool-invoker", + "DotNet Dependency Tool Invoker", + Accept.ExactlyOneArgument() + .With(name: "COMMAND", + description: "The command to execute."), + false, + Create.Option( + "-h|--help", + "Show help information", + Accept.NoArguments(), + materialize: o => o.Option.Command().HelpView()), + Create.Option( + "-p|--project-path", + "Path to Project.json that contains the tool dependency", + Accept.ExactlyOneArgument() + .With(name: "PROJECT_JSON_PATH", + defaultValue: () => + PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()))), + Create.Option( + "-c|--configuration", + "Configuration under which to build", + Accept.ExactlyOneArgument() + .With(name: "CONFIGURATION", + defaultValue: () => Constants.DefaultConfiguration)), + Create.Option( + "-o|--output", + "Directory in which to find the binaries to be run", + Accept.ExactlyOneArgument() + .With(name: "OUTPUT_DIR")), + Create.Option( + "-f|--framework", + "Looks for test binaries for a specific framework", + Accept.ExactlyOneArgument() + .With(name: "FRAMEWORK") + .MaterializeAs(p => NuGetFramework.Parse(p.Arguments.Single()))), + Create.Option( + "-r|--runtime", + "Look for test binaries for a for the specified runtime", + Accept.ExactlyOneArgument() + .With(name: "RUNTIME_IDENTIFIER"))); + } +} diff --git a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/Program.cs b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/Program.cs index 9c481c55d..ff312149e 100644 --- a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/Program.cs +++ b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/Program.cs @@ -5,8 +5,10 @@ using System; using System.IO; using System.Collections.Generic; using System.Linq; +using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Utils; using NuGet.Frameworks; +using Microsoft.DotNet.Tools.Common; namespace Microsoft.DotNet.Tools.DependencyInvoker { @@ -16,51 +18,79 @@ namespace Microsoft.DotNet.Tools.DependencyInvoker { DebugHelper.HandleDebugSwitch(ref args); - var dotnetParams = new DotnetBaseParams("dotnet-dependency-tool-invoker", "DotNet Dependency Tool Invoker", "Invokes tools declared as NuGet dependencies of a project"); + args = new [] { "dotnet-dependency-tool-invoker" }.Concat(args).ToArray(); - dotnetParams.Parse(args); + var parser = new Parser( + options: DotnetDependencyToolInvokerParser.DotnetDependencyToolInvoker()); - if (string.IsNullOrEmpty(dotnetParams.Command)) + var parseResult = parser.Parse(args); + var appliedOptions = parseResult["dotnet-dependency-tool-invoker"]; + + Console.WriteLine(parseResult.Diagram()); + + if (appliedOptions.HasOption("help")) { - Console.WriteLine("A command name must be provided"); - - return 1; + Console.WriteLine(parseResult.Command().HelpView()); + return 0; } + var command = appliedOptions.Arguments.First(); + var framework = appliedOptions.ValueOrDefault("framework"); + var configuration = appliedOptions.ValueOrDefault("configuration"); + if (string.IsNullOrEmpty(configuration)) + { + configuration = Constants.DefaultConfiguration; + } + + var output = appliedOptions.SingleArgumentOrDefault("output"); + var projectPath = appliedOptions.ValueOrDefault("project-path"); + if (string.IsNullOrEmpty(projectPath)) + { + projectPath = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()); + } + + var appArguments = parseResult.UnmatchedTokens; + var commandFactory = new ProjectDependenciesCommandFactory( - dotnetParams.Framework, - dotnetParams.Config, - dotnetParams.Output, - dotnetParams.BuildBasePath, - dotnetParams.ProjectPath); + framework, + configuration, + output, + string.Empty, + projectPath); - var result = InvokeDependencyToolForMSBuild(commandFactory, dotnetParams); + var result = + InvokeDependencyToolForMSBuild(commandFactory, command, framework, configuration, appArguments); return result; } private static int InvokeDependencyToolForMSBuild( ProjectDependenciesCommandFactory commandFactory, - DotnetBaseParams dotnetParams) + string command, + NuGetFramework framework, + string configuration, + IEnumerable appArguments) { - Console.WriteLine($"Invoking '{dotnetParams.Command}' for '{dotnetParams.Framework.GetShortFolderName()}'."); + Console.WriteLine($"Invoking '{command}' for '{framework.GetShortFolderName()}'."); - return InvokeDependencyTool(commandFactory, dotnetParams, dotnetParams.Framework); + return InvokeDependencyTool(commandFactory, command, framework, configuration, appArguments); } private static int InvokeDependencyTool( ProjectDependenciesCommandFactory commandFactory, - DotnetBaseParams dotnetParams, - NuGetFramework framework) + string command, + NuGetFramework framework, + string configuration, + IEnumerable appArguments) { try { var exitCode = commandFactory.Create( - $"dotnet-{dotnetParams.Command}", - dotnetParams.RemainingArguments, + $"dotnet-{command}", + appArguments, framework, - dotnetParams.Config) + configuration) .ForwardStdErr() .ForwardStdOut() .Execute() diff --git a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/dotnet-dependency-tool-invoker.csproj b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/dotnet-dependency-tool-invoker.csproj index 47539b5f0..a6c5b91c1 100644 --- a/TestAssets/TestPackages/dotnet-dependency-tool-invoker/dotnet-dependency-tool-invoker.csproj +++ b/TestAssets/TestPackages/dotnet-dependency-tool-invoker/dotnet-dependency-tool-invoker.csproj @@ -16,13 +16,10 @@ $(CliVersionPrefix)-* - - - - +