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.
// 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.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using System.Collections.Generic;
using System.Diagnostics;
namespace Microsoft.DotNet.Tools.Publish
{
@ -21,100 +19,19 @@ namespace Microsoft.DotNet.Tools.Publish
{
DebugHelper.HandleDebugSwitch(ref args);
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false);
app.Name = "dotnet publish";
app.FullName = LocalizableStrings.AppFullName;
app.Description = LocalizableStrings.AppDescription;
app.HandleRemainingArguments = true;
app.ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText;
app.HelpOption("-h|--help");
var msbuildArgs = new List<string>();
CommandArgument projectArgument = app.Argument($"<{LocalizableStrings.ProjectArgument}>",
LocalizableStrings.ProjectArgDescription);
var parser = Parser.Instance;
CommandOption frameworkOption = app.Option(
$"-f|--framework <{LocalizableStrings.FrameworkOption}>", LocalizableStrings.FrameworkOptionDescription,
CommandOptionType.SingleValue);
var result = parser.ParseFrom("dotnet publish", args);
CommandOption runtimeOption = app.Option(
$"-r|--runtime <{LocalizableStrings.RuntimeOption}>", LocalizableStrings.RuntimeOptionDescription,
CommandOptionType.SingleValue);
msbuildArgs.Add("/t:Publish");
CommandOption outputOption = app.Option(
$"-o|--output <{LocalizableStrings.OutputOption}>", LocalizableStrings.OutputOptionDescription,
CommandOptionType.SingleValue);
var appliedPublishOption = result.AppliedOptions["publish"];
CommandOption configurationOption = app.Option(
$"-c|--configuration <{LocalizableStrings.ConfigurationOption}>", LocalizableStrings.ConfigurationOptionDescription,
CommandOptionType.SingleValue);
msbuildArgs.AddRange(appliedPublishOption.Arguments);
CommandOption versionSuffixOption = app.Option(
$"--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);
}
msbuildArgs.AddRange(appliedPublishOption.ArgsToBeForwarded());
return new PublishCommand(msbuildArgs, msbuildPath);
}
@ -136,4 +53,4 @@ namespace Microsoft.DotNet.Tools.Publish
return cmd.Execute();
}
}
}
}

View file

@ -10,27 +10,36 @@ namespace Microsoft.DotNet.Cli
public static Command Publish() =>
Create.Command("publish",
".NET Publisher",
Accept.ExactlyOneArgument,
Accept.ZeroOrMoreArguments,
CommonOptions.HelpOption(),
Create.Option("-f|--framework",
"Target framework to publish for. The target framework has to be specified in the project file.",
Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile)
.With(name: "FRAMEWORK")),
.With(name: "FRAMEWORK")
.ForwardAs("/p:TargetFramework={0}")),
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.",
Accept.AnyOneOf(Suggest.RunTimesFromProjectFile)
.With(name: "RUNTIME_IDENTIFIER")),
.With(name: "RUNTIME_IDENTIFIER")
.ForwardAs("/p:RuntimeIdentifier={0}")),
Create.Option("-o|--output",
"Output directory in which to place the published artifacts.",
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\".",
Accept.ExactlyOneArgument
.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.",
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());
}
}