2015-12-30 17:02:59 -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 ;
using Microsoft.DotNet.Cli.Utils ;
using System.Runtime.InteropServices ;
using Microsoft.DotNet.ProjectModel ;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public sealed class BuildCommand : TestCommand
{
private Project _project ;
2016-03-01 17:42:44 -08:00
private readonly string _projectPath ;
private readonly string _outputDirectory ;
2016-04-12 12:36:26 -07:00
private readonly string _buildBasePathDirectory ;
2016-03-01 17:42:44 -08:00
private readonly string _configuration ;
private readonly string _framework ;
private readonly string _versionSuffix ;
private readonly bool _noHost ;
private readonly bool _native ;
private readonly string _architecture ;
private readonly string _ilcArgs ;
private readonly string _ilcPath ;
private readonly string _appDepSDKPath ;
private readonly bool _nativeCppMode ;
private readonly string _cppCompilerFlags ;
private readonly bool _buildProfile ;
private readonly bool _noIncremental ;
private readonly bool _noDependencies ;
private readonly string _runtime ;
2016-05-12 10:33:32 -07:00
private readonly bool _verbose ;
2015-12-30 17:02:59 -08:00
private string OutputOption
{
get
{
return _outputDirectory = = string . Empty ?
"" :
2016-01-22 14:05:02 -08:00
$"-o \" { _outputDirectory } \ "" ;
2015-12-30 17:02:59 -08:00
}
}
2016-02-03 10:57:25 -08:00
private string BuildBasePathOption
2015-12-30 17:02:59 -08:00
{
get
{
2016-04-12 12:36:26 -07:00
return _buildBasePathDirectory = = string . Empty ?
2015-12-30 17:02:59 -08:00
"" :
2016-04-12 12:36:26 -07:00
$"-b {_buildBasePathDirectory}" ;
2015-12-30 17:02:59 -08:00
}
}
private string ConfigurationOption
{
get
{
return _configuration = = string . Empty ?
"" :
$"-c {_configuration}" ;
}
}
2016-02-03 10:57:25 -08:00
private string FrameworkOption
{
get
{
return _framework = = string . Empty ?
"" :
$"--framework {_framework}" ;
}
}
2016-03-01 17:42:44 -08:00
2016-02-18 01:09:23 -08:00
private string VersionSuffixOption
{
get
{
return _versionSuffix = = string . Empty ?
"" :
$"--version-suffix {_versionSuffix}" ;
}
}
2015-12-30 17:02:59 -08:00
private string NoHostOption
{
get
{
return _noHost ?
"--no-host" :
"" ;
}
}
private string NativeOption
{
get
{
return _native ?
"--native" :
"" ;
}
}
2016-03-01 17:42:44 -08:00
private string RuntimeOption
{
get
{
return _runtime = = string . Empty ?
"" :
$"--runtime {_runtime}" ;
}
}
2015-12-30 17:02:59 -08:00
private string ArchitectureOption
{
get
{
return _architecture = = string . Empty ?
"" :
$"--arch {_architecture}" ;
}
}
private string IlcArgsOption
{
get
{
return _ilcArgs = = string . Empty ?
"" :
$"--ilcargs {_ilcArgs}" ;
}
}
private string IlcPathOption
{
get
{
return _ilcPath = = string . Empty ?
"" :
$"--ilcpath {_ilcPath}" ;
}
}
private string AppDepSDKPathOption
{
get
{
return _appDepSDKPath = = string . Empty ?
"" :
$"--appdepsdkpath {_appDepSDKPath}" ;
}
}
private string NativeCppModeOption
{
get
{
return _nativeCppMode ?
"--cpp" :
"" ;
}
}
private string CppCompilerFlagsOption
{
get
{
return _cppCompilerFlags = = string . Empty ?
"" :
$"--cppcompilerflags {_cppCompilerFlags}" ;
}
}
2015-12-21 10:42:41 -08:00
private string BuildProfile
{
get
{
return _buildProfile ?
"--build-profile" :
"" ;
}
}
2016-01-21 11:04:45 -08:00
private string NoIncremental
2015-12-21 10:42:41 -08:00
{
get
{
2016-01-21 11:04:45 -08:00
return _noIncremental ?
"--no-incremental" :
2015-12-21 10:42:41 -08:00
"" ;
}
}
2016-02-05 15:29:34 -08:00
private string NoDependencies
{
get
{
return _noDependencies ?
"--no-dependencies" :
"" ;
}
}
2016-05-12 10:33:32 -07:00
private string Verbose
{
get
{
return _verbose ? "--verbose" : "" ;
}
}
2015-12-30 17:02:59 -08:00
public BuildCommand (
string projectPath ,
string output = "" ,
2016-04-12 12:36:26 -07:00
string buildBasePath = "" ,
2015-12-30 17:02:59 -08:00
string configuration = "" ,
2016-02-03 10:57:25 -08:00
string framework = "" ,
2016-03-01 17:42:44 -08:00
string runtime = "" ,
2016-02-18 01:09:23 -08:00
string versionSuffix = "" ,
2015-12-30 17:02:59 -08:00
bool noHost = false ,
bool native = false ,
string architecture = "" ,
string ilcArgs = "" ,
string ilcPath = "" ,
string appDepSDKPath = "" ,
bool nativeCppMode = false ,
2015-12-21 10:42:41 -08:00
string cppCompilerFlags = "" ,
bool buildProfile = true ,
2016-02-05 15:29:34 -08:00
bool noIncremental = false ,
2016-04-29 22:19:35 -05:00
bool noDependencies = false ,
2016-05-12 10:33:32 -07:00
bool verbose = true ,
2016-04-29 22:19:35 -05:00
bool skipLoadProject = false )
2015-12-30 17:02:59 -08:00
: base ( "dotnet" )
{
_projectPath = projectPath ;
2016-04-29 22:19:35 -05:00
if ( ! skipLoadProject )
{
_project = ProjectReader . GetProject ( projectPath ) ;
}
2015-12-30 17:02:59 -08:00
_outputDirectory = output ;
2016-04-12 12:36:26 -07:00
_buildBasePathDirectory = buildBasePath ;
2015-12-30 17:02:59 -08:00
_configuration = configuration ;
2016-02-18 01:09:23 -08:00
_versionSuffix = versionSuffix ;
2016-02-03 10:57:25 -08:00
_framework = framework ;
2016-03-01 17:42:44 -08:00
_runtime = runtime ;
2015-12-30 17:02:59 -08:00
_noHost = noHost ;
_native = native ;
_architecture = architecture ;
_ilcArgs = ilcArgs ;
_ilcPath = ilcPath ;
_appDepSDKPath = appDepSDKPath ;
_nativeCppMode = nativeCppMode ;
_cppCompilerFlags = cppCompilerFlags ;
2015-12-21 10:42:41 -08:00
_buildProfile = buildProfile ;
2016-01-21 11:04:45 -08:00
_noIncremental = noIncremental ;
2016-02-05 15:29:34 -08:00
_noDependencies = noDependencies ;
2016-05-12 10:33:32 -07:00
_verbose = verbose ;
2015-12-30 17:02:59 -08:00
}
public override CommandResult Execute ( string args = "" )
{
2016-05-12 10:33:32 -07:00
args = $"{Verbose} build {BuildArgs()} {args}" ;
2015-12-30 17:02:59 -08:00
return base . Execute ( args ) ;
}
2016-01-08 11:03:14 -08:00
public override CommandResult ExecuteWithCapturedOutput ( string args = "" )
{
2016-05-12 10:33:32 -07:00
args = $"{Verbose} build {BuildArgs()} {args}" ;
2016-01-08 11:03:14 -08:00
return base . ExecuteWithCapturedOutput ( args ) ;
}
2016-03-15 11:50:14 -07:00
public string GetPortableOutputName ( )
{
return $"{_project.Name}.dll" ;
}
2015-12-30 17:02:59 -08:00
public string GetOutputExecutableName ( )
{
2016-03-16 16:39:59 -07:00
return _project . Name + GetExecutableExtension ( ) ;
}
public string GetExecutableExtension ( )
{
2016-04-21 13:41:22 -07:00
#if NET451
return ".exe" ;
#else
2016-03-16 16:39:59 -07:00
return RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? ".exe" : "" ;
2016-04-21 13:41:22 -07:00
#endif
2015-12-30 17:02:59 -08:00
}
private string BuildArgs ( )
{
2016-03-09 11:36:16 -08:00
return $"{BuildProfile} {NoDependencies} {NoIncremental} \" { _projectPath } \ " {OutputOption} {BuildBasePathOption} {ConfigurationOption} {FrameworkOption} {RuntimeOption} {VersionSuffixOption} {NoHostOption} {NativeOption} {ArchitectureOption} {IlcArgsOption} {IlcPathOption} {AppDepSDKPathOption} {NativeCppModeOption} {CppCompilerFlagsOption}" ;
2015-12-30 17:02:59 -08:00
}
}
}