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 ;
2015-12-10 13:06:33 -08:00
namespace Microsoft.DotNet.Tools.Build
{
2016-01-30 21:47:50 -08:00
public class BuildCommand
2015-12-10 13:06:33 -08:00
{
2016-10-27 18:46:43 -07:00
public static int Run ( string [ ] args )
2015-12-10 13:06:33 -08:00
{
DebugHelper . HandleDebugSwitch ( ref args ) ;
2016-10-27 18:46:43 -07:00
CommandLineApplication app = new CommandLineApplication ( throwOnUnexpectedArg : false ) ;
app . Name = "dotnet build" ;
app . FullName = ".NET Builder" ;
app . Description = "Builder for the .NET Platform. Delegates to the MSBuild 'Build' target in the project file." ;
app . AllowArgumentSeparator = true ;
app . ArgumentSeparatorHelpText = HelpMessageStrings . MSBuildAdditionalArgsHelpText ;
app . HelpOption ( "-h|--help" ) ;
2015-12-10 13:06:33 -08:00
2016-10-27 18:46:43 -07:00
CommandArgument projectArgument = app . Argument ( "<PROJECT>" ,
"The MSBuild project file to build. If a project file is not specified," +
" MSBuild searches the current working directory for a file that has a file extension that ends in `proj` and uses that file." ) ;
2016-04-19 19:14:59 -07:00
2016-10-27 18:46:43 -07:00
CommandOption outputOption = app . Option ( "-o|--output <OUTPUT_DIR>" , "Directory in which to place outputs" , CommandOptionType . SingleValue ) ;
CommandOption frameworkOption = app . Option ( "-f|--framework <FRAMEWORK>" , "Compile a specific framework" , CommandOptionType . SingleValue ) ;
CommandOption configurationOption = app . Option ( "-c|--configuration <CONFIGURATION>" , "Configuration under which to build" , CommandOptionType . SingleValue ) ;
CommandOption versionSuffixOption = app . Option ( "--version-suffix <VERSION_SUFFIX>" , "Defines the value for the $(VersionSuffix) property in the project" , CommandOptionType . SingleValue ) ;
CommandOption noIncrementalOption = app . Option ( "--no-incremental" , "Set this flag to turn off incremental build" , CommandOptionType . NoValue ) ;
CommandOption noDependenciesOption = app . Option ( "--no-dependencies" , "Set this flag to ignore project to project references and only build the root project" , CommandOptionType . NoValue ) ;
2016-04-27 16:04:26 -07:00
2016-10-27 18:46:43 -07:00
app . OnExecute ( ( ) = >
2016-04-27 16:04:26 -07:00
{
2016-10-27 18:46:43 -07:00
List < string > 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
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" ) ;
}
msbuildArgs . AddRange ( app . RemainingArguments ) ;
return new MSBuildForwardingApp ( msbuildArgs ) . Execute ( ) ;
} ) ;
return app . Execute ( args ) ;
2015-12-10 13:06:33 -08:00
}
}
}