2015-12-14 17:39:29 -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.
2016-03-09 11:36:16 -08:00
using System.IO ;
using System.Linq ;
using System.Runtime.InteropServices ;
2016-04-08 16:27:09 -07:00
using System.Threading.Tasks ;
2016-04-28 16:30:32 -07:00
using Microsoft.DotNet.Cli.Utils ;
using Microsoft.DotNet.InternalAbstractions ;
using Microsoft.DotNet.ProjectModel ;
2015-12-14 17:39:29 -08:00
2015-12-30 17:02:59 -08:00
namespace Microsoft.DotNet.Tools.Test.Utilities
2015-12-14 17:39:29 -08:00
{
public sealed class PublishCommand : TestCommand
{
2016-02-16 15:30:39 -08:00
private const string PublishSubfolderName = "publish" ;
2016-03-09 11:36:16 -08:00
private readonly Project _project ;
private readonly string _path ;
private readonly string _framework ;
private readonly string _runtime ;
private readonly string _config ;
2016-03-29 14:15:26 -07:00
private readonly bool _noBuild ;
2016-03-09 11:36:16 -08:00
private readonly string _output ;
2016-04-12 12:36:26 -07:00
private readonly string _buidBasePathDirectory ;
2015-12-14 17:39:29 -08:00
2016-04-12 12:36:26 -07:00
public PublishCommand ( string projectPath ,
string framework = "" ,
string runtime = "" ,
string output = "" ,
string config = "" ,
bool noBuild = false ,
string buildBasePath = "" )
2015-12-14 17:39:29 -08:00
: base ( "dotnet" )
{
_path = projectPath ;
_project = ProjectReader . GetProject ( projectPath ) ;
_framework = framework ;
_runtime = runtime ;
_output = output ;
2016-01-11 13:40:47 -08:00
_config = config ;
2016-03-29 14:15:26 -07:00
_noBuild = noBuild ;
2016-04-12 12:36:26 -07:00
_buidBasePathDirectory = buildBasePath ;
2015-12-14 17:39:29 -08:00
}
2016-04-08 16:27:09 -07:00
public override Task < CommandResult > ExecuteAsync ( string args = "" )
{
args = $"publish {BuildArgs()} {args}" ;
return base . ExecuteAsync ( args ) ;
}
2016-03-09 11:36:16 -08:00
public override CommandResult Execute ( string args = "" )
2016-01-11 13:40:47 -08:00
{
2015-12-14 17:39:29 -08:00
args = $"publish {BuildArgs()} {args}" ;
return base . Execute ( args ) ;
}
2016-01-11 13:40:47 -08:00
public override CommandResult ExecuteWithCapturedOutput ( string args = "" )
{
args = $"publish {BuildArgs()} {args}" ;
return base . ExecuteWithCapturedOutput ( args ) ;
}
2016-03-09 11:36:16 -08:00
public string ProjectName = > _project . Name ;
2015-12-14 17:39:29 -08:00
2016-03-09 11:36:16 -08:00
private string BuildRelativeOutputPath ( bool portable )
2015-12-14 17:39:29 -08:00
{
2016-01-11 13:40:47 -08:00
// lets try to build an approximate output path
2015-12-14 17:39:29 -08:00
string config = string . IsNullOrEmpty ( _config ) ? "Debug" : _config ;
2016-01-11 13:40:47 -08:00
string framework = string . IsNullOrEmpty ( _framework ) ?
2015-12-14 17:39:29 -08:00
_project . GetTargetFrameworks ( ) . First ( ) . FrameworkName . GetShortFolderName ( ) : _framework ;
2016-04-28 16:30:32 -07:00
2016-03-09 11:36:16 -08:00
if ( ! portable )
{
2016-04-28 16:30:32 -07:00
var runtime = string . IsNullOrEmpty ( _runtime ) ? RuntimeEnvironmentRidExtensions . GetLegacyRestoreRuntimeIdentifier ( ) : _runtime ;
2016-03-09 11:36:16 -08:00
return Path . Combine ( config , framework , runtime , PublishSubfolderName ) ;
}
else
{
return Path . Combine ( config , framework , PublishSubfolderName ) ;
}
2015-12-14 17:39:29 -08:00
}
2016-03-09 11:36:16 -08:00
public DirectoryInfo GetOutputDirectory ( bool portable = false )
2015-12-14 17:39:29 -08:00
{
if ( ! string . IsNullOrEmpty ( _output ) )
{
2016-01-26 06:39:13 -08:00
return new DirectoryInfo ( _output ) ;
2015-12-14 17:39:29 -08:00
}
2016-03-09 11:36:16 -08:00
string output = Path . Combine ( _project . ProjectDirectory , "bin" , BuildRelativeOutputPath ( portable ) ) ;
2015-12-14 17:39:29 -08:00
return new DirectoryInfo ( output ) ;
}
2016-03-15 11:50:14 -07:00
public string GetPortableOutputName ( )
{
return $"{_project.Name}.dll" ;
}
2015-12-14 17:39:29 -08:00
public string GetOutputExecutable ( )
{
2016-03-17 13:46:46 -07:00
return _project . Name + GetExecutableExtension ( ) ;
}
public string GetExecutableExtension ( )
{
2016-04-21 13:41:22 -07:00
#if NET451
return ".exe" ;
#else
2016-03-17 13:46:46 -07:00
return RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? ".exe" : "" ;
2016-04-21 13:41:22 -07:00
#endif
2015-12-14 17:39:29 -08:00
}
private string BuildArgs ( )
{
2016-04-12 12:36:26 -07:00
return $"{_path} {FrameworkOption} {RuntimeOption} {OutputOption} {ConfigOption} {NoBuildFlag} {BuildBasePathOption}" ;
2015-12-14 17:39:29 -08:00
}
2016-03-09 11:36:16 -08:00
private string FrameworkOption = > string . IsNullOrEmpty ( _framework ) ? "" : $"-f {_framework}" ;
private string RuntimeOption = > string . IsNullOrEmpty ( _runtime ) ? "" : $"-r {_runtime}" ;
private string OutputOption = > string . IsNullOrEmpty ( _output ) ? "" : $"-o \" { _output } \ "" ;
private string ConfigOption = > string . IsNullOrEmpty ( _config ) ? "" : $"-c {_output}" ;
2016-04-28 16:30:32 -07:00
private string NoBuildFlag = > _noBuild ? "--no-build" : "" ;
2016-04-12 12:36:26 -07:00
private string BuildBasePathOption = > string . IsNullOrEmpty ( _buidBasePathDirectory ) ? "" : $"-b {_buidBasePathDirectory}" ;
2015-12-14 17:39:29 -08:00
}
}