dotnet restore using new parser
This commit is contained in:
parent
0094fd4e08
commit
69bc90dc6f
5 changed files with 164 additions and 164 deletions
41
src/dotnet/ArgumentForwardingExtensions.cs
Normal file
41
src/dotnet/ArgumentForwardingExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,18 +7,21 @@ namespace Microsoft.DotNet.Cli
|
||||||
internal static class CommonOptions
|
internal static class CommonOptions
|
||||||
{
|
{
|
||||||
public static Option HelpOption() =>
|
public static Option HelpOption() =>
|
||||||
Create.Option("-h|--help",
|
Create.Option(
|
||||||
"Show help information",
|
"-h|--help",
|
||||||
Accept.NoArguments,
|
"Show help information",
|
||||||
materialize: o => o.Option.Command().HelpView());
|
Accept.NoArguments,
|
||||||
|
materialize: o => o.Option.Command().HelpView());
|
||||||
|
|
||||||
public static Option VerbosityOption() =>
|
public static Option VerbosityOption() =>
|
||||||
Create.Option("-v|--verbosity",
|
Create.Option(
|
||||||
"Set the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]",
|
"-v|--verbosity",
|
||||||
Accept.AnyOneOf("q", "quiet",
|
"Set the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]",
|
||||||
"m", "minimal",
|
Accept.AnyOneOf("q", "quiet",
|
||||||
"n", "normal",
|
"m", "minimal",
|
||||||
"d", "detailed"));
|
"n", "normal",
|
||||||
|
"d", "detailed")
|
||||||
|
.ForwardAs("/verbosity:{0}"));
|
||||||
|
|
||||||
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
|
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
|
||||||
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));
|
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));
|
||||||
|
|
|
@ -6,7 +6,7 @@ using Microsoft.DotNet.Cli.CommandLine;
|
||||||
using Microsoft.DotNet.Cli.Utils;
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
using Microsoft.DotNet.Tools.MSBuild;
|
using Microsoft.DotNet.Tools.MSBuild;
|
||||||
using Microsoft.DotNet.Cli;
|
using Microsoft.DotNet.Cli;
|
||||||
using System.Diagnostics;
|
using Parser = Microsoft.DotNet.Cli.Parser;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Tools.Restore
|
namespace Microsoft.DotNet.Tools.Restore
|
||||||
{
|
{
|
||||||
|
@ -21,133 +21,21 @@ namespace Microsoft.DotNet.Tools.Restore
|
||||||
{
|
{
|
||||||
DebugHelper.HandleDebugSwitch(ref args);
|
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",
|
"/NoLogo",
|
||||||
FullName = LocalizableStrings.AppFullName,
|
"/t:Restore",
|
||||||
Description = LocalizableStrings.AppDescription,
|
"/ConsoleLoggerParameters:Verbosity=Minimal"
|
||||||
HandleRemainingArguments = true,
|
|
||||||
ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText
|
|
||||||
};
|
};
|
||||||
|
|
||||||
cmd.HelpOption("-h|--help");
|
msbuildArgs.AddRange(restore.ArgsToBeForwarded());
|
||||||
|
msbuildArgs.AddRange(restore.Arguments);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new RestoreCommand(msbuildArgs, msbuildPath);
|
return new RestoreCommand(msbuildArgs, msbuildPath);
|
||||||
}
|
}
|
||||||
|
@ -169,4 +57,4 @@ namespace Microsoft.DotNet.Tools.Restore
|
||||||
return cmd.Execute();
|
return cmd.Execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,33 +9,54 @@ namespace Microsoft.DotNet.Cli
|
||||||
{
|
{
|
||||||
public static Command Restore() =>
|
public static Command Restore() =>
|
||||||
Create.Command("restore",
|
Create.Command("restore",
|
||||||
".NET dependency restorer",
|
".NET dependency restorer",
|
||||||
Accept.ZeroOrOneArgument,
|
Accept.OneOrMoreArguments,
|
||||||
CommonOptions.HelpOption(),
|
CommonOptions.HelpOption(),
|
||||||
Create.Option("-s|--source",
|
Create.Option(
|
||||||
"Specifies a NuGet package source to use during the restore.",
|
"-s|--source",
|
||||||
Accept.ExactlyOneArgument
|
"Specifies a NuGet package source to use during the restore.",
|
||||||
.With(name: "SOURCE")),
|
Accept.OneOrMoreArguments
|
||||||
Create.Option("-r|--runtime",
|
.With(name: "SOURCE")
|
||||||
"Target runtime to restore packages for.",
|
.ForwardAs(o => $"/p:RestoreSources={string.Join("%3B", o.Arguments)}")),
|
||||||
Accept.AnyOneOf(Suggest.RunTimesFromProjectFile)
|
Create.Option(
|
||||||
.With(name: "RUNTIME_IDENTIFIER")),
|
"-r|--runtime",
|
||||||
Create.Option("--packages",
|
"Target runtime to restore packages for.",
|
||||||
"Directory to install packages in.",
|
Accept.OneOrMoreArguments
|
||||||
Accept.ExactlyOneArgument
|
.WithSuggestionsFrom(_ => Suggest.RunTimesFromProjectFile())
|
||||||
.With(name: "PACKAGES_DIRECTORY")),
|
.With(name: "RUNTIME_IDENTIFIER")
|
||||||
Create.Option("--disable-parallel",
|
.ForwardAs(o => $"/p:RuntimeIdentifiers={string.Join("%3B", o.Arguments)}")),
|
||||||
"Disables restoring multiple projects in parallel."),
|
Create.Option(
|
||||||
Create.Option("--configfile",
|
"--packages",
|
||||||
"The NuGet configuration file to use.",
|
"Directory to install packages in.",
|
||||||
Accept.ExactlyOneArgument
|
Accept.ExactlyOneArgument
|
||||||
.With(name: "FILE")),
|
.With(name: "PACKAGES_DIRECTORY")
|
||||||
Create.Option("--no-cache",
|
.ForwardAs("/p:RestorePackagesPath={0}")),
|
||||||
"Do not cache packages and http requests."),
|
Create.Option(
|
||||||
Create.Option("--ignore-failed-sources",
|
"--disable-parallel",
|
||||||
"Treat package source failures as warnings."),
|
"Disables restoring multiple projects in parallel.",
|
||||||
Create.Option("--no-dependencies",
|
Accept.NoArguments
|
||||||
"Set this flag to ignore project to project references and only restore the root project"),
|
.ForwardAs("/p:RestoreDisableParallel=true")),
|
||||||
CommonOptions.VerbosityOption());
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue