small change to arg forwarding methods, test fixes

This commit is contained in:
Jon Sequeira 2017-03-09 09:14:55 -08:00
parent 0b94c979db
commit 4284c4e363
10 changed files with 91 additions and 64 deletions

View file

@ -7,11 +7,14 @@ namespace Microsoft.DotNet.Cli
{
public static class ArgumentForwardingExtensions
{
public static ArgumentsRule Forward(
this ArgumentsRule rule) =>
rule.MaterializeAs(o => new ForwardedArgument(o.Arguments.SingleOrDefault()));
public static ArgumentsRule ForwardAs(
this ArgumentsRule rule,
string template) =>
rule.MaterializeAs(o =>
new ForwardedArgument(string.Format(template, o.Arguments.Single())));
rule.MaterializeAs(o => new ForwardedArgument(template));
public static ArgumentsRule ForwardAs(
this ArgumentsRule rule,

View file

@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools.Common;
@ -22,7 +23,7 @@ namespace Microsoft.DotNet.Cli
"m", "minimal",
"n", "normal",
"d", "detailed")
.ForwardAs("/verbosity:{0}"));
.ForwardAs(o => $"/verbosity:{o.Arguments.Single()}"));
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));

View file

@ -1,6 +1,7 @@
// 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.Linq;
using Microsoft.DotNet.Cli.CommandLine;
namespace Microsoft.DotNet.Cli
@ -12,37 +13,37 @@ namespace Microsoft.DotNet.Cli
"build",
".NET Builder",
Accept.ZeroOrOneArgument
.ForwardAs("{0}"),
.Forward(),
CommonOptions.HelpOption(),
Create.Option(
"-o|--output",
"Output directory in which to place built artifacts.",
Accept.ExactlyOneArgument
.With(name: "OUTPUT_DIR")
.ForwardAs("/p:OutputPath={0}")),
.ForwardAs(o => $"/p:OutputPath={o.Arguments.Single()}")),
Create.Option(
"-f|--framework",
"Target framework to build for. The target framework has to be specified in the project file.",
Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile)
.ForwardAs("/p:TargetFramework={0}")),
.ForwardAs(o => $"/p:TargetFramework={o.Arguments.Single()}")),
Create.Option(
"-r|--runtime",
"Target runtime to build for. The default is to build a portable application.",
Accept.AnyOneOf(Suggest.RunTimesFromProjectFile)
.ForwardAs("/p:RuntimeIdentifier={0}")),
.ForwardAs(o => $"/p:RuntimeIdentifier={o.Arguments.Single()}")),
Create.Option(
"-c|--configuration",
"Configuration to use for building the project. Default for most projects is \"Debug\".",
Accept.ExactlyOneArgument
.With(name: "CONFIGURATION")
.WithSuggestionsFrom("DEBUG", "RELEASE")
.ForwardAs("/p:Configuration={0}")),
.ForwardAs(o => $"/p:Configuration={o.Arguments.Single()}")),
Create.Option(
"--version-suffix",
"Defines the value for the $(VersionSuffix) property in the project",
Accept.ExactlyOneArgument
.With(name: "VERSION_SUFFIX")
.ForwardAs("/p:VersionSuffix={0}")),
.ForwardAs(o => $"/p:VersionSuffix={o.Arguments.Single()}")),
Create.Option(
"--no-incremental",
"Disables incremental build."),

View file

@ -24,7 +24,7 @@ namespace Microsoft.DotNet.Cli
// parse the arguments
var result = parser.ParseFrom("dotnet complete", args);
var complete = result["complete"];
var complete = result["dotnet"]["complete"];
var suggestions = Suggestions(complete);

View file

@ -15,7 +15,6 @@ namespace Microsoft.DotNet.Cli
.With(name: "path"),
Create.Option("--position", "",
Accept.ExactlyOneArgument
.With(name: "command")
.MaterializeAs(o => int.Parse(o.Arguments.Single()))));
.With(name: "command")));
}
}

View file

@ -1,6 +1,7 @@
// 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.Linq;
using Microsoft.DotNet.Cli.CommandLine;
namespace Microsoft.DotNet.Cli
@ -8,7 +9,8 @@ namespace Microsoft.DotNet.Cli
internal static class PublishCommandParser
{
public static Command Publish() =>
Create.Command("publish",
Create.Command(
"publish",
".NET Publisher",
Accept.ZeroOrMoreArguments,
CommonOptions.HelpOption(),
@ -16,30 +18,30 @@ namespace Microsoft.DotNet.Cli
"Target framework to publish for. The target framework has to be specified in the project file.",
Accept.WithSuggestionsFrom(_ => Suggest.TargetFrameworksFromProjectFile())
.With(name: "FRAMEWORK")
.ForwardAs("/p:TargetFramework={0}")),
.ForwardAs(o => $"/p:TargetFramework={o.Arguments.Single()}")),
Create.Option("-r|--runtime",
"Publish the project for a given runtime. This is used when creating self-contained deployment. Default is to publish a framework-dependent app.",
Accept.WithSuggestionsFrom(_ => Suggest.RunTimesFromProjectFile())
.With(name: "RUNTIME_IDENTIFIER")
.ForwardAs("/p:RuntimeIdentifier={0}")),
.ForwardAs(o => $"/p:RuntimeIdentifier={o.Arguments.Single()}")),
Create.Option("-o|--output",
"Output directory in which to place the published artifacts.",
Accept.ExactlyOneArgument
.With(name: "OUTPUT_DIR")
.ForwardAs("/p:PublishDir={0}")),
.ForwardAs(o => $"/p:PublishDir={o.Arguments.Single()}")),
Create.Option("-c|--configuration", "Configuration to use for building the project. Default for most projects is \"Debug\".",
Accept.ExactlyOneArgument
.With(name: "CONFIGURATION")
.WithSuggestionsFrom("DEBUG", "RELEASE")
.ForwardAs("/p:Configuration={0}")),
.ForwardAs(o => $"/p:Configuration={o.Arguments.Single()}")),
Create.Option("--version-suffix", "Defines the value for the $(VersionSuffix) property in the project.",
Accept.ExactlyOneArgument
.With(name: "VERSION_SUFFIX")
.ForwardAs("/p:VersionSuffix={0}")),
.ForwardAs(o => $"/p:VersionSuffix={o.Arguments.Single()}")),
Create.Option("--filter", "The XML file that contains the list of packages to be excluded from publish step.",
Accept.ExactlyOneArgument
.With(name: "PROFILE_XML")
.ForwardAs("/p:FilterProjectFiles={0}")),
.ForwardAs(o => $"/p:FilterProjectFiles={o.Arguments.Single()}")),
CommonOptions.VerbosityOption());
}
}

View file

@ -1,6 +1,7 @@
// 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.Linq;
using Microsoft.DotNet.Cli.CommandLine;
namespace Microsoft.DotNet.Cli
@ -31,7 +32,7 @@ namespace Microsoft.DotNet.Cli
"Directory to install packages in.",
Accept.ExactlyOneArgument
.With(name: "PACKAGES_DIRECTORY")
.ForwardAs("/p:RestorePackagesPath={0}")),
.ForwardAs(o => $"/p:RestorePackagesPath={o.Arguments.Single()}")),
Create.Option(
"--disable-parallel",
"Disables restoring multiple projects in parallel.",
@ -42,7 +43,7 @@ namespace Microsoft.DotNet.Cli
"The NuGet configuration file to use.",
Accept.ExactlyOneArgument
.With(name: "FILE")
.ForwardAs("/p:RestoreConfigFile={0}")),
.ForwardAs(o => $"/p:RestoreConfigFile={o.Arguments.Single()}")),
Create.Option(
"--no-cache",
"Do not cache packages and http requests.",

View file

@ -26,8 +26,8 @@ namespace Microsoft.DotNet.Cli.MSBuild.Tests
[InlineData(new string[] { "--no-cache" }, "/p:RestoreNoCache=true")]
[InlineData(new string[] { "--ignore-failed-sources" }, "/p:RestoreIgnoreFailedSources=true")]
[InlineData(new string[] { "--no-dependencies" }, "/p:RestoreRecursive=false")]
[InlineData(new string[] { "-v", "<verbosity>" }, @"/verbosity:<verbosity>")]
[InlineData(new string[] { "--verbosity", "<verbosity>" }, @"/verbosity:<verbosity>")]
[InlineData(new string[] { "-v", "minimal" }, @"/verbosity:minimal")]
[InlineData(new string[] { "--verbosity", "minimal" }, @"/verbosity:minimal")]
public void MsbuildInvocationIsCorrect(string[] args, string expectedAdditionalArgs)
{
expectedAdditionalArgs = (string.IsNullOrEmpty(expectedAdditionalArgs) ? "" : $" {expectedAdditionalArgs}");

View file

@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using FluentAssertions;
using System.Linq;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Xunit;
@ -13,20 +14,22 @@ namespace Microsoft.DotNet.Tests.ParserTests
public class ArgumentForwardingExtensionsTests
{
[Fact]
public void An_outgoing_command_line_can_be_generated_based_on_a_parse_result()
public void AnOutgoingCommandLineCanBeGeneratedBasedOnAParseResult()
{
var command = Command("the-command", "",
Option("-o|--one", "",
ZeroOrOneArgument.ForwardAs("/i:{0}")),
ZeroOrOneArgument
.ForwardAs(o => $"/i:{o.Arguments.Single()}")),
Option("-t|--two", "",
ZeroOrOneArgument.ForwardAs("/s:{0}")));
NoArguments
.ForwardAs("/s:true")));
var result = command.Parse("the-command -t argument-two-value -o 123");
var result = command.Parse("the-command -t -o 123");
result["the-command"]
.OptionValuesToBeForwarded()
.Should()
.BeEquivalentTo("/i:123", "/s:argument-two-value");
.BeEquivalentTo("/i:123", "/s:true");
}
[Fact]
@ -34,7 +37,8 @@ namespace Microsoft.DotNet.Tests.ParserTests
{
var command = Command("the-command", "",
Option("-x", "",
ZeroOrMoreArguments.ForwardAs(o => $"/x:{string.Join("&", o.Arguments)}")));
ZeroOrMoreArguments
.ForwardAs(o => $"/x:{string.Join("&", o.Arguments)}")));
var result = command.Parse("the-command -x one -x two");
@ -43,5 +47,21 @@ namespace Microsoft.DotNet.Tests.ParserTests
.Should()
.BeEquivalentTo("/x:one&two");
}
[Fact]
public void AnArgumentCanBeForwardedAsIs()
{
var command = Command("the-command", "",
Option("-x", "",
ZeroOrMoreArguments
.Forward()));
var result = command.Parse("the-command -x one");
result["the-command"]
.OptionValuesToBeForwarded()
.Should()
.BeEquivalentTo("one");
}
}
}

View file

@ -22,13 +22,13 @@ namespace Microsoft.DotNet.Tests.ParserTests
[Fact]
public void RestoreCapturesArgumentsToForwardToMSBuildWhenTargetIsSpecified()
{
var parser = Parser.Instance["dotnet"]["restore"];
var parser = Parser.Instance;
var result = parser.Parse(@".\some.csproj --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true");
var result = parser.Parse(@"dotnet restore .\some.csproj --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true");
output.WriteLine(result.Diagram());
result["restore"]
result["dotnet"]["restore"]
.Arguments
.Should()
.BeEquivalentTo(@".\some.csproj", @"/p:SkipInvalidConfigurations=true");
@ -37,13 +37,13 @@ namespace Microsoft.DotNet.Tests.ParserTests
[Fact]
public void RestoreCapturesArgumentsToForwardToMSBuildWhenTargetIsNotSpecified()
{
var parser = Parser.Instance["dotnet"]["restore"];
var parser = Parser.Instance;
var result = parser.Parse(@"--packages c:\.nuget\packages /p:SkipInvalidConfigurations=true");
var result = parser.Parse(@"dotnet restore --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true");
output.WriteLine(result.Diagram());
result["restore"]
result["dotnet"]["restore"]
.Arguments
.Should()
.BeEquivalentTo(@"/p:SkipInvalidConfigurations=true");