2015-12-10 13:06:33 -08:00
|
|
|
|
// 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;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
2016-05-02 11:32:24 -07:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
using Microsoft.DotNet.Tools.MSBuild;
|
2017-02-14 10:41:58 -08:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.DotNet.Cli;
|
2015-12-10 13:06:33 -08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Build
|
|
|
|
|
{
|
2017-02-23 11:24:36 -08:00
|
|
|
|
public class BuildCommand : MSBuildForwardingApp
|
2015-12-10 13:06:33 -08:00
|
|
|
|
{
|
2017-02-14 11:38:01 -08:00
|
|
|
|
public BuildCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
|
2017-02-23 11:24:36 -08:00
|
|
|
|
: base(msbuildArgs, msbuildPath)
|
2015-12-10 13:06:33 -08:00
|
|
|
|
{
|
2017-02-14 10:41:58 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 11:38:01 -08:00
|
|
|
|
public static BuildCommand FromArgs(string[] args, string msbuildPath = null)
|
2017-02-14 10:41:58 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false);
|
|
|
|
|
app.Name = "dotnet build";
|
2016-11-15 17:12:26 -08:00
|
|
|
|
app.FullName = LocalizableStrings.AppFullName;
|
|
|
|
|
app.Description = LocalizableStrings.AppDescription;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
app.ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText;
|
2017-02-14 10:41:58 -08:00
|
|
|
|
app.HandleRemainingArguments = true;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
app.HelpOption("-h|--help");
|
2015-12-10 13:06:33 -08:00
|
|
|
|
|
2016-11-30 13:06:21 -08:00
|
|
|
|
CommandArgument projectArgument = app.Argument($"<{LocalizableStrings.ProjectArgumentValueName}>", LocalizableStrings.ProjectArgumentDescription);
|
2016-04-19 19:14:59 -07:00
|
|
|
|
|
2016-11-30 13:06:21 -08:00
|
|
|
|
CommandOption outputOption = app.Option($"-o|--output <{LocalizableStrings.OutputOptionName}>", LocalizableStrings.OutputOptionDescription, CommandOptionType.SingleValue);
|
|
|
|
|
CommandOption frameworkOption = app.Option($"-f|--framework <{LocalizableStrings.FrameworkOptionName}>", LocalizableStrings.FrameworkOptionDescription, CommandOptionType.SingleValue);
|
2016-10-31 16:22:10 -07:00
|
|
|
|
CommandOption runtimeOption = app.Option(
|
2016-11-30 13:06:21 -08:00
|
|
|
|
$"-r|--runtime <{LocalizableStrings.RuntimeOptionName}>", LocalizableStrings.RuntimeOptionDescription,
|
2016-10-31 16:22:10 -07:00
|
|
|
|
CommandOptionType.SingleValue);
|
2017-01-30 19:15:44 -06:00
|
|
|
|
CommandOption configurationOption = app.Option($"-c|--configuration <{LocalizableStrings.ConfigurationOptionName}>", LocalizableStrings.ConfigurationOptionDescription, CommandOptionType.SingleValue);
|
2016-11-30 13:06:21 -08:00
|
|
|
|
CommandOption versionSuffixOption = app.Option($"--version-suffix <{LocalizableStrings.VersionSuffixOptionName}>", LocalizableStrings.VersionSuffixOptionDescription, CommandOptionType.SingleValue);
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
2016-11-15 17:12:26 -08:00
|
|
|
|
CommandOption noIncrementalOption = app.Option("--no-incremental", LocalizableStrings.NoIncrementialOptionDescription, CommandOptionType.NoValue);
|
|
|
|
|
CommandOption noDependenciesOption = app.Option("--no-dependencies", LocalizableStrings.NoDependenciesOptionDescription, CommandOptionType.NoValue);
|
2016-11-15 11:56:39 -08:00
|
|
|
|
CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app);
|
2016-04-27 16:04:26 -07:00
|
|
|
|
|
2017-02-16 14:18:23 -08:00
|
|
|
|
List<string> msbuildArgs = null;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
app.OnExecute(() =>
|
2016-04-27 16:04:26 -07:00
|
|
|
|
{
|
2017-02-16 14:18:23 -08:00
|
|
|
|
// this delayed initialization is here intentionally
|
|
|
|
|
// this code will not get run in some cases (i.e. --help)
|
|
|
|
|
msbuildArgs = new List<string>();
|
2016-04-19 19:14:59 -07:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(projectArgument.Value))
|
|
|
|
|
{
|
|
|
|
|
msbuildArgs.Add(projectArgument.Value);
|
|
|
|
|
}
|
2016-04-19 19:14:59 -07:00
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (noIncrementalOption.HasValue())
|
|
|
|
|
{
|
|
|
|
|
msbuildArgs.Add("/t:Rebuild");
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-04-27 16:04:26 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
msbuildArgs.Add("/t:Build");
|
2016-04-27 16:04:26 -07:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
|
|
|
|
if (outputOption.HasValue())
|
2016-04-19 19:14:59 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
msbuildArgs.Add($"/p:OutputPath={outputOption.Value()}");
|
2016-04-19 19:14:59 -07:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
|
|
|
|
if (frameworkOption.HasValue())
|
2016-04-19 19:14:59 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
|
2016-04-19 19:14:59 -07:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
2016-10-31 16:22:10 -07:00
|
|
|
|
if (runtimeOption.HasValue())
|
|
|
|
|
{
|
|
|
|
|
msbuildArgs.Add($"/p:RuntimeIdentifier={runtimeOption.Value()}");
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (configurationOption.HasValue())
|
2016-04-19 19:14:59 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
msbuildArgs.Add($"/p:Configuration={configurationOption.Value()}");
|
2016-04-19 19:14:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
if (versionSuffixOption.HasValue())
|
2016-04-19 19:14:59 -07:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
msbuildArgs.Add($"/p:VersionSuffix={versionSuffixOption.Value()}");
|
2016-04-19 19:14:59 -07:00
|
|
|
|
}
|
2016-10-27 18:46:43 -07:00
|
|
|
|
|
|
|
|
|
if (noDependenciesOption.HasValue())
|
|
|
|
|
{
|
|
|
|
|
msbuildArgs.Add("/p:BuildProjectReferences=false");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 11:56:39 -08:00
|
|
|
|
if (verbosityOption.HasValue())
|
|
|
|
|
{
|
|
|
|
|
msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 17:06:03 -08:00
|
|
|
|
msbuildArgs.Add($"/clp:Summary");
|
|
|
|
|
|
2016-10-27 18:46:43 -07:00
|
|
|
|
msbuildArgs.AddRange(app.RemainingArguments);
|
|
|
|
|
|
2017-02-14 10:41:58 -08:00
|
|
|
|
return 0;
|
2016-10-27 18:46:43 -07:00
|
|
|
|
});
|
|
|
|
|
|
2017-02-14 11:38:01 -08:00
|
|
|
|
int exitCode = app.Execute(args);
|
2017-02-16 14:18:23 -08:00
|
|
|
|
if (msbuildArgs == null)
|
2017-02-14 10:41:58 -08:00
|
|
|
|
{
|
2017-02-15 10:38:41 -08:00
|
|
|
|
throw new CommandCreationException(exitCode);
|
2017-02-14 10:41:58 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 14:18:23 -08:00
|
|
|
|
return new BuildCommand(msbuildArgs, msbuildPath);
|
2017-02-14 10:41:58 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Run(string[] args)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
|
|
|
|
|
BuildCommand cmd;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
cmd = FromArgs(args);
|
|
|
|
|
}
|
2017-02-15 10:38:41 -08:00
|
|
|
|
catch (CommandCreationException e)
|
2017-02-14 10:41:58 -08:00
|
|
|
|
{
|
|
|
|
|
return e.ExitCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmd.Execute();
|
|
|
|
|
}
|
2015-12-10 13:06:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|