2016-04-19 19:14:59 -07: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-04-20 18:52:23 -05:00
using Microsoft.DotNet.Cli.CommandLine ;
2016-04-19 19:14:59 -07:00
using Microsoft.DotNet.Cli.Utils ;
2016-04-28 16:30:32 -07:00
using Microsoft.DotNet.InternalAbstractions ;
2016-04-22 15:01:56 -07:00
using Microsoft.DotNet.ProjectModel ;
2016-05-02 12:21:12 -05:00
using Microsoft.DotNet.Tools.Common ;
2016-04-19 19:14:59 -07:00
using NuGet.Frameworks ;
// This class is responsible with defining the arguments for the Compile verb.
// It knows how to interpret them and set default values
namespace Microsoft.DotNet.Tools.Compiler
{
public delegate bool OnExecute ( IEnumerable < string > files , IEnumerable < NuGetFramework > frameworks , BuildCommandApp buildCommand ) ;
public class BuildCommandApp
{
public static readonly string NoIncrementalFlag = "--no-incremental" ;
public static readonly string BuildProfileFlag = "--build-profile" ;
private readonly CommandLineApplication _app ;
// options and arguments for compilation
private CommandOption _outputOption ;
private CommandOption _buildBasePath ;
private CommandOption _frameworkOption ;
private CommandOption _runtimeOption ;
private CommandOption _versionSuffixOption ;
private CommandOption _configurationOption ;
private CommandArgument _projectArgument ;
private CommandOption _shouldPrintIncrementalPreconditionsArgument ;
private CommandOption _shouldNotUseIncrementalityArgument ;
private CommandOption _shouldSkipDependenciesArgument ;
public string BuildBasePathValue { get ; set ; }
public string RuntimeValue { get ; set ; }
public string OutputValue { get ; set ; }
public string VersionSuffixValue { get ; set ; }
public string ConfigValue { get ; set ; }
public bool IsNativeValue { get ; set ; }
public bool ShouldPrintIncrementalPreconditions { get ; set ; }
public bool ShouldNotUseIncrementality { get ; set ; }
public bool ShouldSkipDependencies { get ; set ; }
2016-05-02 11:32:24 -07:00
public BuildWorkspace Workspace { get ; private set ; }
2016-04-22 15:01:56 -07:00
2016-04-19 19:14:59 -07:00
// workaround: CommandLineApplication is internal therefore I cannot make _app protected so baseclasses can add their own params
private readonly Dictionary < string , CommandOption > baseClassOptions ;
2016-04-22 15:01:56 -07:00
public BuildCommandApp ( string name , string fullName , string description ) : this ( name , fullName , description , workspace : null ) { }
2016-05-02 11:32:24 -07:00
public BuildCommandApp ( string name , string fullName , string description , BuildWorkspace workspace )
2016-04-19 19:14:59 -07:00
{
2016-04-22 15:01:56 -07:00
Workspace = workspace ;
2016-04-19 19:14:59 -07:00
_app = new CommandLineApplication
{
Name = name ,
FullName = fullName ,
Description = description
} ;
baseClassOptions = new Dictionary < string , CommandOption > ( ) ;
AddCompileParameters ( ) ;
}
private void AddCompileParameters ( )
{
_app . HelpOption ( "-h|--help" ) ;
_outputOption = _app . Option ( "-o|--output <OUTPUT_DIR>" , "Directory in which to place outputs" , CommandOptionType . SingleValue ) ;
_buildBasePath = _app . Option ( "-b|--build-base-path <OUTPUT_DIR>" , "Directory in which to place temporary outputs" , CommandOptionType . SingleValue ) ;
_frameworkOption = _app . Option ( "-f|--framework <FRAMEWORK>" , "Compile a specific framework" , CommandOptionType . SingleValue ) ;
_runtimeOption = _app . Option ( "-r|--runtime <RUNTIME_IDENTIFIER>" , "Produce runtime-specific assets for the specified runtime" , CommandOptionType . SingleValue ) ;
_configurationOption = _app . Option ( "-c|--configuration <CONFIGURATION>" , "Configuration under which to build" , CommandOptionType . SingleValue ) ;
_versionSuffixOption = _app . Option ( "--version-suffix <VERSION_SUFFIX>" , "Defines what `*` should be replaced with in version field in project.json" , CommandOptionType . SingleValue ) ;
_projectArgument = _app . Argument ( "<PROJECT>" , "The project to compile, defaults to the current directory. " +
"Can be one or multiple paths to project.json, project directory " +
2016-05-30 21:55:00 +01:00
"or globbing pattern that matches project.json files" , multipleValues : true ) ;
2016-04-19 19:14:59 -07:00
_shouldPrintIncrementalPreconditionsArgument = _app . Option ( BuildProfileFlag , "Set this flag to print the incremental safety checks that prevent incremental compilation" , CommandOptionType . NoValue ) ;
_shouldNotUseIncrementalityArgument = _app . Option ( NoIncrementalFlag , "Set this flag to turn off incremental build" , CommandOptionType . NoValue ) ;
_shouldSkipDependenciesArgument = _app . Option ( "--no-dependencies" , "Set this flag to ignore project to project references and only build the root project" , CommandOptionType . NoValue ) ;
}
public int Execute ( OnExecute execute , string [ ] args )
{
_app . OnExecute ( ( ) = >
{
if ( _outputOption . HasValue ( ) & & ! _frameworkOption . HasValue ( ) )
{
Reporter . Error . WriteLine ( "When the '--output' option is provided, the '--framework' option must also be provided." ) ;
return 1 ;
}
OutputValue = _outputOption . Value ( ) ;
2016-05-02 12:21:12 -05:00
BuildBasePathValue = PathUtility . GetFullPath ( _buildBasePath . Value ( ) ) ;
2016-04-19 19:14:59 -07:00
ConfigValue = _configurationOption . Value ( ) ? ? Constants . DefaultConfiguration ;
RuntimeValue = _runtimeOption . Value ( ) ;
VersionSuffixValue = _versionSuffixOption . Value ( ) ;
ShouldPrintIncrementalPreconditions = _shouldPrintIncrementalPreconditionsArgument . HasValue ( ) ;
ShouldNotUseIncrementality = _shouldNotUseIncrementalityArgument . HasValue ( ) ;
ShouldSkipDependencies = _shouldSkipDependenciesArgument . HasValue ( ) ;
2016-04-22 15:01:56 -07:00
// Set defaults based on the environment
if ( Workspace = = null )
{
2016-05-02 11:32:24 -07:00
Workspace = BuildWorkspace . Create ( VersionSuffixValue ) ;
2016-04-22 15:01:56 -07:00
}
2016-04-19 19:14:59 -07:00
var files = new ProjectGlobbingResolver ( ) . Resolve ( _projectArgument . Values ) ;
IEnumerable < NuGetFramework > frameworks = null ;
if ( _frameworkOption . HasValue ( ) )
{
2016-04-28 16:30:32 -07:00
frameworks = new [ ] { NuGetFramework . Parse ( _frameworkOption . Value ( ) ) } ;
2016-04-19 19:14:59 -07:00
}
var success = execute ( files , frameworks , this ) ;
return success ? 0 : 1 ;
} ) ;
return _app . Execute ( args ) ;
}
public IEnumerable < string > GetRuntimes ( )
{
var rids = new List < string > ( ) ;
if ( string . IsNullOrEmpty ( RuntimeValue ) )
{
2016-04-28 16:30:32 -07:00
return RuntimeEnvironmentRidExtensions . GetAllCandidateRuntimeIdentifiers ( ) ;
2016-04-19 19:14:59 -07:00
}
else
{
return new [ ] { RuntimeValue } ;
}
}
}
}