Initial Cache parser

This commit is contained in:
Piotr Puszkiewicz 2017-03-09 16:12:08 -08:00
parent 3ebdf2909d
commit e0da3090e6
4 changed files with 75 additions and 99 deletions

View file

@ -27,6 +27,7 @@ namespace Microsoft.DotNet.Cli
RemoveCommandParser.Remove(),
ListCommandParser.List(),
NuGetCommandParser.NuGet(),
CacheCommandParser.Cache(),
Create.Command("msbuild", ""),
Create.Command("vstest", ""),
CompleteCommandParser.Complete(),

View file

@ -0,0 +1,57 @@
// 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;
using Microsoft.DotNet.Tools.Cache;
namespace Microsoft.DotNet.Cli
{
internal static class CacheCommandParser
{
public static Command Cache() =>
Create.Command(
LocalizableStrings.AppFullName,
LocalizableStrings.AppDescription,
Accept.ZeroOrMoreArguments,
CommonOptions.HelpOption(),
Create.Option(
"-e|--entries",
LocalizableStrings.ProjectEntryDescription,
Accept.ExactlyOneArgument
.With(name: LocalizableStrings.ProjectEntries)
.Forward()),
CommonOptions.FrameworkOption(),
Create.Option(
"--framework-version",
LocalizableStrings.FrameworkVersionOptionDescription,
Accept.ExactlyOneArgument
.With(name: LocalizableStrings.FrameworkVersionOption)
.ForwardAs(o => $"/p:FX_Version={o.Arguments.Single()}")),
CommonOptions.RuntimeOption(),
CommonOptions.ConfigurationOption(),
Create.Option(
"-o|--output",
LocalizableStrings.OutputOptionDescription,
Accept.ExactlyOneArgument
.With(name: LocalizableStrings.OutputOption)
.ForwardAs(o => $"/p:ComposeDir={o.Arguments.Single()}")),
Create.Option(
"-w |--working-dir",
LocalizableStrings.IntermediateWorkingDirOptionDescription,
Accept.ExactlyOneArgument
.With(name: LocalizableStrings.IntermediateWorkingDirOption)
.ForwardAs(o => $"/p:ComposeWorkingDir={o.Arguments.Single()}")),
Create.Option(
"--preserve-working-dir",
LocalizableStrings.PreserveIntermediateWorkingDirOptionDescription,
Accept.NoArguments
.ForwardAs(o => $"/p:PreserveComposeWorkingDir=true")),
Create.Option(
"--skip-optimization",
LocalizableStrings.SkipOptimizationOptionDescription,
Accept.NoArguments
.ForwardAs("/p:SkipOptimization=true")),
CommonOptions.VerbosityOption());
}
}

View file

@ -9,7 +9,7 @@ namespace Microsoft.DotNet.Tools.Cache
public const string AppDescription = "Caches the specified assemblies for the .NET Platform. By default, these will be optimized for the target runtime and framework.";
public const string ProjectEntries = "ProjectEntries";
public const string ProjectEntries = "PROJECT_ENTRIES";
public const string ProjectEntryDescription = "The XML file that contains the list of packages to be cached.";

View file

@ -23,113 +23,31 @@ namespace Microsoft.DotNet.Tools.Cache
{
DebugHelper.HandleDebugSwitch(ref args);
var app = new CommandLineApplication(throwOnUnexpectedArg: false);
app.Name = "dotnet cache";
app.FullName = LocalizableStrings.AppFullName;
app.Description = LocalizableStrings.AppDescription;
app.AllowArgumentSeparator = true;
app.ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText;
app.HelpOption("-h|--help");
var msbuildArgs = new List<string>();
CommandOption projectArgument = app.Option(
$"-e|--entries <{LocalizableStrings.ProjectEntries}>", LocalizableStrings.ProjectEntryDescription,
CommandOptionType.SingleValue);
var parser = Parser.Instance;
CommandOption frameworkOption = app.Option(
$"-f|--framework <{LocalizableStrings.FrameworkOption}>", LocalizableStrings.FrameworkOptionDescription,
CommandOptionType.SingleValue);
var result = parser.ParseFrom("dotnet cache", args);
CommandOption runtimeOption = app.Option(
$"-r|--runtime <{LocalizableStrings.RuntimeOption}>", LocalizableStrings.RuntimeOptionDescription,
CommandOptionType.SingleValue);
Reporter.Output.WriteLine(result.Diagram());
CommandOption outputOption = app.Option(
$"-o|--output <{LocalizableStrings.OutputOption}>", LocalizableStrings.OutputOptionDescription,
CommandOptionType.SingleValue);
result.ShowHelpIfRequested();
CommandOption fxOption = app.Option(
$"--framework-version <{LocalizableStrings.FrameworkVersionOption}>", LocalizableStrings.FrameworkVersionOptionDescription,
CommandOptionType.SingleValue);
var appliedBuildOptions = result["dotnet"]["cache"];
CommandOption skipOptimizationOption = app.Option(
$"--skip-optimization", LocalizableStrings.SkipOptimizationOptionDescription,
CommandOptionType.NoValue);
CommandOption workingDir = app.Option(
$"-w |--working-dir <{LocalizableStrings.IntermediateWorkingDirOption}>", LocalizableStrings.IntermediateWorkingDirOptionDescription,
CommandOptionType.SingleValue);
CommandOption preserveWorkingDir = app.Option(
$"--preserve-working-dir", LocalizableStrings.PreserveIntermediateWorkingDirOptionDescription,
CommandOptionType.NoValue);
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
List<string> msbuildArgs = null;
app.OnExecute(() =>
if (!result.HasOption("-e"))
{
msbuildArgs = new List<string>();
if (string.IsNullOrEmpty(projectArgument.Value()))
{
throw new InvalidOperationException(LocalizableStrings.SpecifyEntries);
}
msbuildArgs.Add("/t:ComposeCache");
msbuildArgs.Add(projectArgument.Value());
if (!string.IsNullOrEmpty(frameworkOption.Value()))
{
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
}
if (!string.IsNullOrEmpty(runtimeOption.Value()))
{
msbuildArgs.Add($"/p:RuntimeIdentifier={runtimeOption.Value()}");
}
if (!string.IsNullOrEmpty(outputOption.Value()))
{
var outputPath = Path.GetFullPath(outputOption.Value());
msbuildArgs.Add($"/p:ComposeDir={outputPath}");
}
if (!string.IsNullOrEmpty(fxOption.Value()))
{
msbuildArgs.Add($"/p:FX_Version={fxOption.Value()}");
}
if (!string.IsNullOrEmpty(workingDir.Value()))
{
msbuildArgs.Add($"/p:ComposeWorkingDir={workingDir.Value()}");
}
if (skipOptimizationOption.HasValue())
{
msbuildArgs.Add($"/p:SkipOptimization={skipOptimizationOption.HasValue()}");
}
if (preserveWorkingDir.HasValue())
{
msbuildArgs.Add($"/p:PreserveComposeWorkingDir={preserveWorkingDir.HasValue()}");
}
if (!string.IsNullOrEmpty(verbosityOption.Value()))
{
msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}");
}
msbuildArgs.AddRange(app.RemainingArguments);
return 0;
});
int exitCode = app.Execute(args);
if (msbuildArgs == null)
{
throw new CommandCreationException(exitCode);
throw new InvalidOperationException(LocalizableStrings.SpecifyEntries);
}
var msbuildArgs = msbuildArgs = new List<string>();
msbuildArgs.Add("/t:ComposeCache");
msbuildArgs.AddRange(appliedBuildOptions.OptionValuesToBeForwarded());
msbuildArgs.AddRange(appliedBuildOptions.Arguments);
return new CacheCommand(msbuildArgs, msbuildPath);
}