dotnet restore using new parser

This commit is contained in:
Jon Sequeira 2017-03-06 20:53:26 -08:00
parent 0094fd4e08
commit 69bc90dc6f
5 changed files with 164 additions and 164 deletions

View file

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
namespace Microsoft.DotNet.Cli
{
public static class ArgumentForwardingExtensions
{
public static ArgumentsRule ForwardAs(
this ArgumentsRule rule,
string template) =>
rule.MaterializeAs(o =>
new ForwardedArgument(string.Format(template, o.Arguments.Single())));
public static ArgumentsRule ForwardAs(
this ArgumentsRule rule,
Func<AppliedOption, string> format) =>
rule.MaterializeAs(o =>
new ForwardedArgument(format(o)));
public static IEnumerable<string> ArgsToBeForwarded(
this AppliedOption command) =>
command.AppliedOptions
.Select(o => o.Value())
.OfType<ForwardedArgument>()
.Select(o => o.ToString());
private class ForwardedArgument
{
private readonly string _value;
public ForwardedArgument(string value)
{
_value = value;
}
public override string ToString() => _value;
}
}
}

View file

@ -7,18 +7,21 @@ namespace Microsoft.DotNet.Cli
internal static class CommonOptions
{
public static Option HelpOption() =>
Create.Option("-h|--help",
"Show help information",
Accept.NoArguments,
materialize: o => o.Option.Command().HelpView());
Create.Option(
"-h|--help",
"Show help information",
Accept.NoArguments,
materialize: o => o.Option.Command().HelpView());
public static Option VerbosityOption() =>
Create.Option("-v|--verbosity",
"Set the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]",
Accept.AnyOneOf("q", "quiet",
"m", "minimal",
"n", "normal",
"d", "detailed"));
Create.Option(
"-v|--verbosity",
"Set the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]",
Accept.AnyOneOf("q", "quiet",
"m", "minimal",
"n", "normal",
"d", "detailed")
.ForwardAs("/verbosity:{0}"));
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));

View file

@ -6,7 +6,7 @@ using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Cli;
using System.Diagnostics;
using Parser = Microsoft.DotNet.Cli.Parser;
namespace Microsoft.DotNet.Tools.Restore
{
@ -21,133 +21,21 @@ namespace Microsoft.DotNet.Tools.Restore
{
DebugHelper.HandleDebugSwitch(ref args);
CommandLineApplication cmd = new CommandLineApplication(throwOnUnexpectedArg: false)
var parser = Parser.DotnetCommand["restore"];
var result = parser.Parse(args);
var restore = result["restore"];
var msbuildArgs = new List<string>
{
Name = "restore",
FullName = LocalizableStrings.AppFullName,
Description = LocalizableStrings.AppDescription,
HandleRemainingArguments = true,
ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText
"/NoLogo",
"/t:Restore",
"/ConsoleLoggerParameters:Verbosity=Minimal"
};
cmd.HelpOption("-h|--help");
var argRoot = cmd.Argument(
$"[{LocalizableStrings.CmdArgument}]",
LocalizableStrings.CmdArgumentDescription,
multipleValues: true);
var sourceOption = cmd.Option(
$"-s|--source <{LocalizableStrings.CmdSourceOption}>",
LocalizableStrings.CmdSourceOptionDescription,
CommandOptionType.MultipleValue);
var runtimeOption = cmd.Option(
$"-r|--runtime <{LocalizableStrings.CmdRuntimeOption}>",
LocalizableStrings.CmdRuntimeOptionDescription,
CommandOptionType.MultipleValue);
var packagesOption = cmd.Option(
$"--packages <{LocalizableStrings.CmdPackagesOption}>",
LocalizableStrings.CmdPackagesOptionDescription,
CommandOptionType.SingleValue);
var disableParallelOption = cmd.Option(
"--disable-parallel",
LocalizableStrings.CmdDisableParallelOptionDescription,
CommandOptionType.NoValue);
var configFileOption = cmd.Option(
$"--configfile <{LocalizableStrings.CmdConfigFileOption}>",
LocalizableStrings.CmdConfigFileOptionDescription,
CommandOptionType.SingleValue);
var noCacheOption = cmd.Option(
"--no-cache",
LocalizableStrings.CmdNoCacheOptionDescription,
CommandOptionType.NoValue);
var ignoreFailedSourcesOption = cmd.Option(
"--ignore-failed-sources",
LocalizableStrings.CmdIgnoreFailedSourcesOptionDescription,
CommandOptionType.NoValue);
var noDependenciesOption = cmd.Option(
"--no-dependencies",
LocalizableStrings.CmdNoDependenciesOptionDescription,
CommandOptionType.NoValue);
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(cmd);
List<string> msbuildArgs = null;
cmd.OnExecute(() =>
{
msbuildArgs = new List<string>()
{
"/NoLogo",
"/t:Restore",
"/ConsoleLoggerParameters:Verbosity=Minimal"
};
if (sourceOption.HasValue())
{
msbuildArgs.Add($"/p:RestoreSources={string.Join("%3B", sourceOption.Values)}");
}
if (runtimeOption.HasValue())
{
msbuildArgs.Add($"/p:RuntimeIdentifiers={string.Join("%3B", runtimeOption.Values)}");
}
if (packagesOption.HasValue())
{
msbuildArgs.Add($"/p:RestorePackagesPath={packagesOption.Value()}");
}
if (disableParallelOption.HasValue())
{
msbuildArgs.Add($"/p:RestoreDisableParallel=true");
}
if (configFileOption.HasValue())
{
msbuildArgs.Add($"/p:RestoreConfigFile={configFileOption.Value()}");
}
if (noCacheOption.HasValue())
{
msbuildArgs.Add($"/p:RestoreNoCache=true");
}
if (ignoreFailedSourcesOption.HasValue())
{
msbuildArgs.Add($"/p:RestoreIgnoreFailedSources=true");
}
if (noDependenciesOption.HasValue())
{
msbuildArgs.Add($"/p:RestoreRecursive=false");
}
if (verbosityOption.HasValue())
{
msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}");
}
// Add in arguments
msbuildArgs.AddRange(argRoot.Values);
// Add remaining arguments that the parser did not understand
msbuildArgs.AddRange(cmd.RemainingArguments);
return 0;
});
int exitCode = cmd.Execute(args);
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
}
msbuildArgs.AddRange(restore.ArgsToBeForwarded());
msbuildArgs.AddRange(restore.Arguments);
return new RestoreCommand(msbuildArgs, msbuildPath);
}
@ -169,4 +57,4 @@ namespace Microsoft.DotNet.Tools.Restore
return cmd.Execute();
}
}
}
}

View file

@ -9,33 +9,54 @@ namespace Microsoft.DotNet.Cli
{
public static Command Restore() =>
Create.Command("restore",
".NET dependency restorer",
Accept.ZeroOrOneArgument,
CommonOptions.HelpOption(),
Create.Option("-s|--source",
"Specifies a NuGet package source to use during the restore.",
Accept.ExactlyOneArgument
.With(name: "SOURCE")),
Create.Option("-r|--runtime",
"Target runtime to restore packages for.",
Accept.AnyOneOf(Suggest.RunTimesFromProjectFile)
.With(name: "RUNTIME_IDENTIFIER")),
Create.Option("--packages",
"Directory to install packages in.",
Accept.ExactlyOneArgument
.With(name: "PACKAGES_DIRECTORY")),
Create.Option("--disable-parallel",
"Disables restoring multiple projects in parallel."),
Create.Option("--configfile",
"The NuGet configuration file to use.",
Accept.ExactlyOneArgument
.With(name: "FILE")),
Create.Option("--no-cache",
"Do not cache packages and http requests."),
Create.Option("--ignore-failed-sources",
"Treat package source failures as warnings."),
Create.Option("--no-dependencies",
"Set this flag to ignore project to project references and only restore the root project"),
CommonOptions.VerbosityOption());
".NET dependency restorer",
Accept.OneOrMoreArguments,
CommonOptions.HelpOption(),
Create.Option(
"-s|--source",
"Specifies a NuGet package source to use during the restore.",
Accept.OneOrMoreArguments
.With(name: "SOURCE")
.ForwardAs(o => $"/p:RestoreSources={string.Join("%3B", o.Arguments)}")),
Create.Option(
"-r|--runtime",
"Target runtime to restore packages for.",
Accept.OneOrMoreArguments
.WithSuggestionsFrom(_ => Suggest.RunTimesFromProjectFile())
.With(name: "RUNTIME_IDENTIFIER")
.ForwardAs(o => $"/p:RuntimeIdentifiers={string.Join("%3B", o.Arguments)}")),
Create.Option(
"--packages",
"Directory to install packages in.",
Accept.ExactlyOneArgument
.With(name: "PACKAGES_DIRECTORY")
.ForwardAs("/p:RestorePackagesPath={0}")),
Create.Option(
"--disable-parallel",
"Disables restoring multiple projects in parallel.",
Accept.NoArguments
.ForwardAs("/p:RestoreDisableParallel=true")),
Create.Option(
"--configfile",
"The NuGet configuration file to use.",
Accept.ExactlyOneArgument
.With(name: "FILE")
.ForwardAs("/p:RestoreConfigFile={0}")),
Create.Option(
"--no-cache",
"Do not cache packages and http requests.",
Accept.NoArguments
.ForwardAs("/p:RestoreNoCache=true")),
Create.Option(
"--ignore-failed-sources",
"Treat package source failures as warnings.",
Accept.NoArguments
.ForwardAs("/p:RestoreIgnoreFailedSources=true")),
Create.Option(
"--no-dependencies",
"Set this flag to ignore project to project references and only restore the root project",
Accept.NoArguments
.ForwardAs("/p:RestoreRecursive=false")),
CommonOptions.VerbosityOption());
}
}

View file

@ -0,0 +1,47 @@
// 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 FluentAssertions;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Xunit;
using static Microsoft.DotNet.Cli.CommandLine.Accept;
using static Microsoft.DotNet.Cli.CommandLine.Create;
namespace Microsoft.DotNet.Tests.ParserTests
{
public class ArgumentForwardingExtensionsTests
{
[Fact]
public void An_outgoing_command_line_can_be_generated_based_on_a_parse_result()
{
var command = Command("the-command", "",
Option("-o|--one", "",
ZeroOrOneArgument.ForwardAs("/i:{0}")),
Option("-t|--two", "",
ZeroOrOneArgument.ForwardAs("/s:{0}")));
var result = command.Parse("the-command -t argument-two-value -o 123");
result["the-command"]
.ArgsToBeForwarded()
.Should()
.BeEquivalentTo("/i:123", "/s:argument-two-value");
}
[Fact]
public void MultipleArgumentsCanBeJoinedWhenForwarding()
{
var command = Command("the-command", "",
Option("-x", "",
ZeroOrMoreArguments.ForwardAs(o => $"/x:{string.Join("&", o.Arguments)}")));
var result = command.Parse("the-command -x one -x two");
result["the-command"]
.ArgsToBeForwarded()
.Should()
.BeEquivalentTo("/x:one&two");
}
}
}