publish command using new parser

This commit is contained in:
jonsequitur 2017-03-08 17:53:07 -08:00
parent fd6f7e48b5
commit 1a0ba24883
2 changed files with 24 additions and 98 deletions

View file

@ -1,12 +1,10 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // 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. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using Microsoft.DotNet.Cli; using Microsoft.DotNet.Cli;
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 System.Collections.Generic;
using System.Diagnostics;
namespace Microsoft.DotNet.Tools.Publish namespace Microsoft.DotNet.Tools.Publish
{ {
@ -21,100 +19,19 @@ namespace Microsoft.DotNet.Tools.Publish
{ {
DebugHelper.HandleDebugSwitch(ref args); DebugHelper.HandleDebugSwitch(ref args);
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false); var msbuildArgs = new List<string>();
app.Name = "dotnet publish";
app.FullName = LocalizableStrings.AppFullName;
app.Description = LocalizableStrings.AppDescription;
app.HandleRemainingArguments = true;
app.ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText;
app.HelpOption("-h|--help");
CommandArgument projectArgument = app.Argument($"<{LocalizableStrings.ProjectArgument}>", var parser = Parser.Instance;
LocalizableStrings.ProjectArgDescription);
CommandOption frameworkOption = app.Option( var result = parser.ParseFrom("dotnet publish", args);
$"-f|--framework <{LocalizableStrings.FrameworkOption}>", LocalizableStrings.FrameworkOptionDescription,
CommandOptionType.SingleValue);
CommandOption runtimeOption = app.Option( msbuildArgs.Add("/t:Publish");
$"-r|--runtime <{LocalizableStrings.RuntimeOption}>", LocalizableStrings.RuntimeOptionDescription,
CommandOptionType.SingleValue);
CommandOption outputOption = app.Option( var appliedPublishOption = result.AppliedOptions["publish"];
$"-o|--output <{LocalizableStrings.OutputOption}>", LocalizableStrings.OutputOptionDescription,
CommandOptionType.SingleValue);
CommandOption configurationOption = app.Option( msbuildArgs.AddRange(appliedPublishOption.Arguments);
$"-c|--configuration <{LocalizableStrings.ConfigurationOption}>", LocalizableStrings.ConfigurationOptionDescription,
CommandOptionType.SingleValue);
CommandOption versionSuffixOption = app.Option( msbuildArgs.AddRange(appliedPublishOption.ArgsToBeForwarded());
$"--version-suffix <{LocalizableStrings.VersionSuffixOption}>", LocalizableStrings.VersionSuffixOptionDescription,
CommandOptionType.SingleValue);
CommandOption filterProjOption = app.Option(
$"--filter <{LocalizableStrings.FilterProjOption}>", LocalizableStrings.FilterProjOptionDescription,
CommandOptionType.SingleValue);
CommandOption verbosityOption = AddVerbosityOption(app);
List<string> msbuildArgs = null;
app.OnExecute(() =>
{
msbuildArgs = new List<string>();
msbuildArgs.Add("/t:Publish");
if (!string.IsNullOrEmpty(projectArgument.Value))
{
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()))
{
msbuildArgs.Add($"/p:PublishDir={outputOption.Value()}");
}
if (!string.IsNullOrEmpty(configurationOption.Value()))
{
msbuildArgs.Add($"/p:Configuration={configurationOption.Value()}");
}
if (!string.IsNullOrEmpty(versionSuffixOption.Value()))
{
msbuildArgs.Add($"/p:VersionSuffix={versionSuffixOption.Value()}");
}
if (!string.IsNullOrEmpty(filterProjOption.Value()))
{
msbuildArgs.Add($"/p:FilterProjectFiles={filterProjOption.Value()}");
}
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);
}
return new PublishCommand(msbuildArgs, msbuildPath); return new PublishCommand(msbuildArgs, msbuildPath);
} }
@ -136,4 +53,4 @@ namespace Microsoft.DotNet.Tools.Publish
return cmd.Execute(); return cmd.Execute();
} }
} }
} }

View file

@ -10,27 +10,36 @@ namespace Microsoft.DotNet.Cli
public static Command Publish() => public static Command Publish() =>
Create.Command("publish", Create.Command("publish",
".NET Publisher", ".NET Publisher",
Accept.ExactlyOneArgument, Accept.ZeroOrMoreArguments,
CommonOptions.HelpOption(), CommonOptions.HelpOption(),
Create.Option("-f|--framework", Create.Option("-f|--framework",
"Target framework to publish for. The target framework has to be specified in the project file.", "Target framework to publish for. The target framework has to be specified in the project file.",
Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile) Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile)
.With(name: "FRAMEWORK")), .With(name: "FRAMEWORK")
.ForwardAs("/p:TargetFramework={0}")),
Create.Option("-r|--runtime", Create.Option("-r|--runtime",
"Publish the project for a given runtime. This is used when creating self-contained deployment. Default is to publish a framework-dependent app.", "Publish the project for a given runtime. This is used when creating self-contained deployment. Default is to publish a framework-dependent app.",
Accept.AnyOneOf(Suggest.RunTimesFromProjectFile) Accept.AnyOneOf(Suggest.RunTimesFromProjectFile)
.With(name: "RUNTIME_IDENTIFIER")), .With(name: "RUNTIME_IDENTIFIER")
.ForwardAs("/p:RuntimeIdentifier={0}")),
Create.Option("-o|--output", Create.Option("-o|--output",
"Output directory in which to place the published artifacts.", "Output directory in which to place the published artifacts.",
Accept.ExactlyOneArgument Accept.ExactlyOneArgument
.With(name: "OUTPUT_DIR")), .With(name: "OUTPUT_DIR")
.ForwardAs("/p:PublishDir={0}")),
Create.Option("-c|--configuration", "Configuration to use for building the project. Default for most projects is \"Debug\".", Create.Option("-c|--configuration", "Configuration to use for building the project. Default for most projects is \"Debug\".",
Accept.ExactlyOneArgument Accept.ExactlyOneArgument
.With(name: "CONFIGURATION") .With(name: "CONFIGURATION")
.WithSuggestionsFrom("DEBUG", "RELEASE")), .WithSuggestionsFrom("DEBUG", "RELEASE")
.ForwardAs("/p:Configuration={0}")),
Create.Option("--version-suffix", "Defines the value for the $(VersionSuffix) property in the project.", Create.Option("--version-suffix", "Defines the value for the $(VersionSuffix) property in the project.",
Accept.ExactlyOneArgument Accept.ExactlyOneArgument
.With(name: "VERSION_SUFFIX")), .With(name: "VERSION_SUFFIX")
.ForwardAs("/p:VersionSuffix={0}")),
Create.Option("--filter", "The XML file that contains the list of packages to be excluded from publish step.",
Accept.ExactlyOneArgument
.With(name: "PROFILE_XML")
.ForwardAs("/p:FilterProjectFiles={0}")),
CommonOptions.VerbosityOption()); CommonOptions.VerbosityOption());
} }
} }