Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Replacing the commandline parser in dotnet-dependency-tool-invoker wi…
Browse files Browse the repository at this point in the history
…th the CliCommandLineParser.
  • Loading branch information
livarcocc committed May 12, 2017
1 parent 2b70427 commit 3a5c75b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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")));
}
}
68 changes: 49 additions & 19 deletions TestAssets/TestPackages/dotnet-dependency-tool-invoker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
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
{
Expand All @@ -16,51 +18,79 @@ public static int Main(string[] args)
{
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");
Console.WriteLine(parseResult.Command().HelpView());
return 0;
}

return 1;
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down

0 comments on commit 3a5c75b

Please sign in to comment.