Merge pull request #6578 from livarcocc/eliminate_cross_project_source

Replacing the commandline parser in dotnet-dependency-tool-invoker
This commit is contained in:
Livar 2017-05-14 10:19:52 -07:00 committed by GitHub
commit 58f709ec7e
5 changed files with 139 additions and 137 deletions

View file

@ -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<T>(this AppliedOption parseResult, string alias)
{
return parseResult
.AppliedOptions
.Where(o => o.HasAlias(alias))
.Select(o => o.Value<T>())
.SingleOrDefault();
}
public static string SingleArgumentOrDefault(this AppliedOption parseResult, string alias)
{
return parseResult
.AppliedOptions
.Where(o => o.HasAlias(alias))
.Select(o => o.Arguments.Single())
.SingleOrDefault();
}
}
}

View file

@ -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<string> 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>",
"Configuration under which to build",
CommandOptionType.SingleValue);
_outputOption = _app.Option(
"-o|--output <OUTPUT_DIR>",
"Directory in which to find the binaries to be run",
CommandOptionType.SingleValue);
_buildBasePath = _app.Option(
"-b|--build-base-path <OUTPUT_DIR>",
"Directory in which to find temporary outputs",
CommandOptionType.SingleValue);
_frameworkOption = _app.Option(
"-f|--framework <FRAMEWORK>",
"Looks for test binaries for a specific framework",
CommandOptionType.SingleValue);
_runtimeOption = _app.Option(
"-r|--runtime <RUNTIME_IDENTIFIER>",
"Look for test binaries for a for the specified runtime",
CommandOptionType.SingleValue);
_projectPath = _app.Option(
"-p|--project-path <PROJECT_JSON_PATH>",
"Path to Project.json that contains the tool dependency",
CommandOptionType.SingleValue);
_command = _app.Argument(
"<COMMAND>",
"The command to execute.");
}
}
}

View file

@ -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")));
}
}

View file

@ -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<NuGetFramework>("framework");
var configuration = appliedOptions.ValueOrDefault<string>("configuration");
if (string.IsNullOrEmpty(configuration))
{
configuration = Constants.DefaultConfiguration;
}
var output = appliedOptions.SingleArgumentOrDefault("output");
var projectPath = appliedOptions.ValueOrDefault<string>("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<string> 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<string> appArguments)
{
try
{
var exitCode = commandFactory.Create(
$"dotnet-{dotnetParams.Command}",
dotnetParams.RemainingArguments,
$"dotnet-{command}",
appArguments,
framework,
dotnetParams.Config)
configuration)
.ForwardStdErr()
.ForwardStdOut()
.Execute()

View file

@ -16,13 +16,10 @@
<SdkNugetVersion Condition=" '$(SdkNugetVersion)' == ''">$(CliVersionPrefix)-*</SdkNugetVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\..\src\dotnet\CommandLine\*.cs;..\..\..\src\dotnet\CommonLocalizableStrings.cs;" Exclude="bin\**;obj\**;**\*.xproj;packages\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NuGet.Frameworks" Version="$(CLI_NuGet_Version)" />
<PackageReference Include="Microsoft.DotNet.Cli.Utils" Version="$(SdkNugetVersion)" />
<PackageReference Include="Microsoft.DotNet.Cli.CommandLine" Version="$(CliCommandLineParserVersion)" />
<PackageReference Include="System.Linq" Version="4.3.0" />
</ItemGroup>