2016-03-07 12:20:50 -08:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.IO.Compression ;
using System.Runtime.InteropServices ;
using Microsoft.DotNet.Cli.Build.Framework ;
using Microsoft.Extensions.PlatformAbstractions ;
using static Microsoft . DotNet . Cli . Build . Framework . BuildHelpers ;
namespace Microsoft.DotNet.Cli.Build
{
public static class PackageTargets
{
2016-03-15 14:21:20 -07:00
[ Target ( nameof ( PackageTargets . CopyCLISDKLayout ) ,
2016-03-16 17:54:44 -07:00
nameof ( PackageTargets . CopySharedHostLayout ) ,
nameof ( PackageTargets . CopySharedFxLayout ) ,
2016-03-15 17:01:50 -07:00
nameof ( PackageTargets . CopyCombinedFrameworkSDKHostLayout ) ,
nameof ( PackageTargets . CopyCombinedFrameworkHostLayout ) ) ]
2016-03-07 12:20:50 -08:00
public static BuildTargetResult InitPackage ( BuildTargetContext c )
{
Directory . CreateDirectory ( Dirs . Packages ) ;
return c . Success ( ) ;
}
[ Target ( nameof ( PrepareTargets . Init ) ,
nameof ( PackageTargets . InitPackage ) ,
nameof ( PackageTargets . GenerateVersionBadge ) ,
nameof ( PackageTargets . GenerateCompressedFile ) ,
nameof ( InstallerTargets . GenerateInstaller ) ,
2016-03-15 16:41:18 -07:00
nameof ( InstallerTargets . TestInstaller ) ,
2016-03-07 12:20:50 -08:00
nameof ( PackageTargets . GenerateNugetPackages ) ) ]
2016-03-07 14:52:41 -08:00
[Environment("DOTNET_BUILD_SKIP_PACKAGING", null, "0", "false")]
2016-03-07 12:20:50 -08:00
public static BuildTargetResult Package ( BuildTargetContext c )
{
return c . Success ( ) ;
}
[Target]
public static BuildTargetResult GenerateVersionBadge ( BuildTargetContext c )
{
var buildVersion = c . BuildContext . Get < BuildVersion > ( "BuildVersion" ) ;
var versionSvg = Path . Combine ( Dirs . RepoRoot , "resources" , "images" , "version_badge.svg" ) ;
var outputVersionSvg = c . BuildContext . Get < string > ( "VersionBadge" ) ;
var versionSvgContent = File . ReadAllText ( versionSvg ) ;
versionSvgContent = versionSvgContent . Replace ( "ver_number" , buildVersion . SimpleVersion ) ;
File . WriteAllText ( outputVersionSvg , versionSvgContent ) ;
return c . Success ( ) ;
}
2016-03-14 18:54:45 -07:00
[Target]
public static BuildTargetResult CopyCLISDKLayout ( BuildTargetContext c )
{
var cliSdkRoot = Path . Combine ( Dirs . Output , "obj" , "clisdk" ) ;
if ( Directory . Exists ( cliSdkRoot ) )
{
2016-03-15 14:06:54 -07:00
Utils . DeleteDirectory ( cliSdkRoot ) ;
2016-03-14 18:54:45 -07:00
}
2016-03-16 17:54:44 -07:00
Directory . CreateDirectory ( cliSdkRoot ) ;
Utils . CopyDirectoryRecursively ( Path . Combine ( Dirs . Stage2 , "sdk" ) , cliSdkRoot , true ) ;
c . BuildContext [ "CLISDKRoot" ] = cliSdkRoot ;
return c . Success ( ) ;
}
[Target]
public static BuildTargetResult CopySharedHostLayout ( BuildTargetContext c )
{
var sharedHostRoot = Path . Combine ( Dirs . Output , "obj" , "sharedHost" ) ;
if ( Directory . Exists ( sharedHostRoot ) )
2016-03-15 12:30:46 -07:00
{
2016-03-16 17:54:44 -07:00
Utils . DeleteDirectory ( sharedHostRoot ) ;
2016-03-15 12:30:46 -07:00
}
2016-03-16 17:54:44 -07:00
Directory . CreateDirectory ( sharedHostRoot ) ;
2016-03-15 12:30:46 -07:00
2016-03-16 17:54:44 -07:00
foreach ( var file in Directory . GetFiles ( Dirs . Stage2 , "*" , SearchOption . TopDirectoryOnly ) )
2016-03-14 18:54:45 -07:00
{
2016-03-16 17:54:44 -07:00
var destFile = file . Replace ( Dirs . Stage2 , sharedHostRoot ) ;
2016-03-14 18:54:45 -07:00
File . Copy ( file , destFile , true ) ;
}
2016-03-16 17:54:44 -07:00
c . BuildContext [ "SharedHostPublishRoot" ] = sharedHostRoot ;
return c . Success ( ) ;
}
[Target]
public static BuildTargetResult CopySharedFxLayout ( BuildTargetContext c )
{
var sharedFxRoot = Path . Combine ( Dirs . Output , "obj" , "sharedFx" ) ;
if ( Directory . Exists ( sharedFxRoot ) )
{
Utils . DeleteDirectory ( sharedFxRoot ) ;
}
Directory . CreateDirectory ( sharedFxRoot ) ;
Utils . CopyDirectoryRecursively ( Path . Combine ( Dirs . Stage2 , "shared" ) , sharedFxRoot , true ) ;
c . BuildContext [ "SharedFrameworkPublishRoot" ] = sharedFxRoot ;
2016-03-14 18:54:45 -07:00
return c . Success ( ) ;
}
2016-03-15 17:01:50 -07:00
[Target]
public static BuildTargetResult CopyCombinedFrameworkSDKHostLayout ( BuildTargetContext c )
{
var combinedRoot = Path . Combine ( Dirs . Output , "obj" , "combined-framework-sdk-host" ) ;
2016-03-16 17:54:44 -07:00
if ( Directory . Exists ( combinedRoot ) )
{
Utils . DeleteDirectory ( combinedRoot ) ;
}
2016-03-15 17:01:50 -07:00
string sdkPublishRoot = c . BuildContext . Get < string > ( "CLISDKRoot" ) ;
Utils . CopyDirectoryRecursively ( sdkPublishRoot , combinedRoot ) ;
string sharedFrameworkPublishRoot = c . BuildContext . Get < string > ( "SharedFrameworkPublishRoot" ) ;
Utils . CopyDirectoryRecursively ( sharedFrameworkPublishRoot , combinedRoot ) ;
string sharedHostPublishRoot = c . BuildContext . Get < string > ( "SharedHostPublishRoot" ) ;
Utils . CopyDirectoryRecursively ( sharedHostPublishRoot , combinedRoot ) ;
c . BuildContext [ "CombinedFrameworkSDKHostRoot" ] = combinedRoot ;
return c . Success ( ) ;
}
[Target]
public static BuildTargetResult CopyCombinedFrameworkHostLayout ( BuildTargetContext c )
{
var combinedRoot = Path . Combine ( Dirs . Output , "obj" , "combined-framework-host" ) ;
2016-03-16 17:54:44 -07:00
if ( Directory . Exists ( combinedRoot ) )
{
Utils . DeleteDirectory ( combinedRoot ) ;
}
2016-03-15 17:01:50 -07:00
string sharedFrameworkPublishRoot = c . BuildContext . Get < string > ( "SharedFrameworkPublishRoot" ) ;
Utils . CopyDirectoryRecursively ( sharedFrameworkPublishRoot , combinedRoot ) ;
string sharedHostPublishRoot = c . BuildContext . Get < string > ( "SharedHostPublishRoot" ) ;
Utils . CopyDirectoryRecursively ( sharedHostPublishRoot , combinedRoot ) ;
c . BuildContext [ "CombinedFrameworkHostRoot" ] = combinedRoot ;
return c . Success ( ) ;
}
2016-03-07 12:20:50 -08:00
[Target(nameof(PackageTargets.GenerateZip), nameof(PackageTargets.GenerateTarBall))]
public static BuildTargetResult GenerateCompressedFile ( BuildTargetContext c )
{
return c . Success ( ) ;
}
[Target(nameof(PackageTargets.InitPackage))]
[BuildPlatforms(BuildPlatform.Windows)]
public static BuildTargetResult GenerateZip ( BuildTargetContext c )
{
2016-03-15 17:01:50 -07:00
CreateZipFromDirectory ( c . BuildContext . Get < string > ( "CombinedFrameworkSDKHostRoot" ) , c . BuildContext . Get < string > ( "CombinedFrameworkSDKHostCompressedFile" ) ) ;
CreateZipFromDirectory ( c . BuildContext . Get < string > ( "CombinedFrameworkHostRoot" ) , c . BuildContext . Get < string > ( "CombinedFrameworkHostCompressedFile" ) ) ;
2016-03-07 12:20:50 -08:00
return c . Success ( ) ;
}
[Target(nameof(PackageTargets.InitPackage))]
[BuildPlatforms(BuildPlatform.Unix)]
public static BuildTargetResult GenerateTarBall ( BuildTargetContext c )
{
2016-03-15 17:01:50 -07:00
CreateTarBallFromDirectory ( c . BuildContext . Get < string > ( "CombinedFrameworkSDKHostRoot" ) , c . BuildContext . Get < string > ( "CombinedFrameworkSDKHostCompressedFile" ) ) ;
CreateTarBallFromDirectory ( c . BuildContext . Get < string > ( "CombinedFrameworkHostRoot" ) , c . BuildContext . Get < string > ( "CombinedFrameworkHostCompressedFile" ) ) ;
2016-03-07 12:20:50 -08:00
return c . Success ( ) ;
}
[Target]
[BuildPlatforms(BuildPlatform.Windows)]
public static BuildTargetResult GenerateNugetPackages ( BuildTargetContext c )
{
var versionSuffix = c . BuildContext . Get < BuildVersion > ( "BuildVersion" ) . VersionSuffix ;
var env = GetCommonEnvVars ( c ) ;
Cmd ( "powershell" , "-NoProfile" , "-NoLogo" ,
2016-03-16 17:54:44 -07:00
Path . Combine ( Dirs . RepoRoot , "packaging" , "nuget" , "package.ps1" ) , Dirs . Stage2 , versionSuffix )
2016-03-07 12:20:50 -08:00
. Environment ( env )
. Execute ( )
. EnsureSuccessful ( ) ;
return c . Success ( ) ;
}
internal static Dictionary < string , string > GetCommonEnvVars ( BuildTargetContext c )
{
// Set up the environment variables previously defined by common.sh/ps1
// This is overkill, but I want to cover all the variables used in all OSes (including where some have the same names)
var buildVersion = c . BuildContext . Get < BuildVersion > ( "BuildVersion" ) ;
var configuration = c . BuildContext . Get < string > ( "Configuration" ) ;
var architecture = PlatformServices . Default . Runtime . RuntimeArchitecture ;
var env = new Dictionary < string , string > ( )
{
{ "RID" , PlatformServices . Default . Runtime . GetRuntimeIdentifier ( ) } ,
{ "OSNAME" , PlatformServices . Default . Runtime . OperatingSystem } ,
{ "TFM" , "dnxcore50" } ,
{ "REPOROOT" , Dirs . RepoRoot } ,
{ "OutputDir" , Dirs . Output } ,
{ "Stage1Dir" , Dirs . Stage1 } ,
{ "Stage1CompilationDir" , Dirs . Stage1Compilation } ,
{ "Stage2Dir" , Dirs . Stage2 } ,
{ "STAGE2_DIR" , Dirs . Stage2 } ,
{ "Stage2CompilationDir" , Dirs . Stage2Compilation } ,
{ "HostDir" , Dirs . Corehost } ,
{ "PackageDir" , Path . Combine ( Dirs . Packages ) } , // Legacy name
{ "TestBinRoot" , Dirs . TestOutput } ,
{ "TestPackageDir" , Dirs . TestPackages } ,
{ "MajorVersion" , buildVersion . Major . ToString ( ) } ,
{ "MinorVersion" , buildVersion . Minor . ToString ( ) } ,
{ "PatchVersion" , buildVersion . Patch . ToString ( ) } ,
{ "CommitCountVersion" , buildVersion . CommitCountString } ,
{ "COMMIT_COUNT_VERSION" , buildVersion . CommitCountString } ,
{ "DOTNET_CLI_VERSION" , buildVersion . SimpleVersion } ,
{ "DOTNET_MSI_VERSION" , buildVersion . GenerateMsiVersion ( ) } ,
{ "VersionSuffix" , buildVersion . VersionSuffix } ,
{ "CONFIGURATION" , configuration } ,
{ "ARCHITECTURE" , architecture }
} ;
return env ;
}
2016-03-08 21:52:34 -08:00
private static void CreateZipFromDirectory ( string directory , string artifactPath )
{
if ( File . Exists ( artifactPath ) )
{
File . Delete ( artifactPath ) ;
}
ZipFile . CreateFromDirectory ( directory , artifactPath , CompressionLevel . Optimal , false ) ;
}
2016-03-08 22:42:16 -08:00
private static void CreateTarBallFromDirectory ( string directory , string artifactPath )
{
if ( File . Exists ( artifactPath ) )
{
File . Delete ( artifactPath ) ;
}
Cmd ( "tar" , "-czf" , artifactPath , "-C" , directory , "." )
. Execute ( )
. EnsureSuccessful ( ) ;
}
2016-03-07 12:20:50 -08:00
}
}