2016-03-07 12:20:50 -08:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.IO.Compression ;
2016-03-18 14:36:50 -07:00
using System.Net.Http ;
2016-03-07 12:20:50 -08:00
using System.Runtime.InteropServices ;
using Microsoft.DotNet.Cli.Build.Framework ;
2016-04-28 16:30:32 -07:00
using Microsoft.DotNet.InternalAbstractions ;
2016-03-07 12:20:50 -08:00
using static Microsoft . DotNet . Cli . Build . Framework . BuildHelpers ;
namespace Microsoft.DotNet.Cli.Build
{
public class MsiTargets
{
private const string ENGINE = "engine.exe" ;
2016-03-18 14:36:50 -07:00
private const string WixVersion = "3.10.2" ;
2016-03-07 12:20:50 -08:00
private static string WixRoot
{
get
{
2016-03-18 14:36:50 -07:00
return Path . Combine ( Dirs . Output , $"WixTools.{WixVersion}" ) ;
2016-03-07 12:20:50 -08:00
}
}
2016-03-08 13:39:41 -08:00
private static string SdkMsi { get ; set ; }
2016-03-07 12:20:50 -08:00
2016-03-08 13:39:41 -08:00
private static string SdkBundle { get ; set ; }
2016-03-07 12:20:50 -08:00
2016-03-08 15:22:33 -08:00
private static string SharedHostMsi { get ; set ; }
2016-03-08 19:32:31 -08:00
private static string SharedFrameworkMsi { get ; set ; }
2016-03-31 10:02:12 -07:00
private static string SdkEngine { get ; set ; }
2016-03-07 14:52:41 -08:00
private static string MsiVersion { get ; set ; }
2016-04-15 12:11:10 -07:00
private static string CliDisplayVersion { get ; set ; }
2016-03-07 14:52:41 -08:00
2016-04-15 12:11:10 -07:00
private static string CliNugetVersion { get ; set ; }
2016-03-07 14:52:41 -08:00
2016-04-15 12:11:10 -07:00
private static string Arch { get ; } = CurrentArchitecture . Current . ToString ( ) ;
2016-03-07 14:52:41 -08:00
2016-03-07 12:20:50 -08:00
private static void AcquireWix ( BuildTargetContext c )
{
if ( File . Exists ( Path . Combine ( WixRoot , "candle.exe" ) ) )
{
return ;
}
Directory . CreateDirectory ( WixRoot ) ;
c . Info ( "Downloading WixTools.." ) ;
2016-03-18 14:36:50 -07:00
DownloadFile ( $"https://dotnetcli.blob.core.windows.net/build/wix/wix.{WixVersion}.zip" , Path . Combine ( WixRoot , "WixTools.zip" ) ) ;
2016-03-07 12:20:50 -08:00
c . Info ( "Extracting WixTools.." ) ;
2016-03-18 14:36:50 -07:00
ZipFile . ExtractToDirectory ( Path . Combine ( WixRoot , "WixTools.zip" ) , WixRoot ) ;
}
private static void DownloadFile ( string uri , string destinationPath )
{
using ( var httpClient = new HttpClient ( ) )
{
var getTask = httpClient . GetStreamAsync ( uri ) ;
using ( var outStream = File . OpenWrite ( destinationPath ) )
{
getTask . Result . CopyTo ( outStream ) ;
}
}
2016-03-07 12:20:50 -08:00
}
[Target]
[BuildPlatforms(BuildPlatform.Windows)]
public static BuildTargetResult InitMsi ( BuildTargetContext c )
{
2016-03-16 17:54:44 -07:00
SdkBundle = c . BuildContext . Get < string > ( "CombinedFrameworkSDKHostInstallerFile" ) ;
2016-03-08 13:39:41 -08:00
SdkMsi = Path . ChangeExtension ( SdkBundle , "msi" ) ;
2016-03-31 10:02:12 -07:00
SdkEngine = GetEngineName ( SdkBundle ) ;
2016-03-07 14:52:41 -08:00
2016-03-08 19:32:31 -08:00
SharedFrameworkMsi = Path . ChangeExtension ( c . BuildContext . Get < string > ( "SharedFrameworkInstallerFile" ) , "msi" ) ;
2016-05-16 15:30:53 -07:00
SharedHostMsi = Path . ChangeExtension ( c . BuildContext . Get < string > ( "SharedHostInstallerFile" ) , "msi" ) ;
2016-03-08 15:22:33 -08:00
2016-03-07 14:52:41 -08:00
var buildVersion = c . BuildContext . Get < BuildVersion > ( "BuildVersion" ) ;
MsiVersion = buildVersion . GenerateMsiVersion ( ) ;
2016-04-15 12:11:10 -07:00
CliDisplayVersion = buildVersion . SimpleVersion ;
CliNugetVersion = buildVersion . NuGetVersion ;
2016-03-07 14:52:41 -08:00
2016-03-07 12:20:50 -08:00
AcquireWix ( c ) ;
return c . Success ( ) ;
}
[ Target ( nameof ( MsiTargets . InitMsi ) ,
2016-03-08 13:39:41 -08:00
nameof ( GenerateCliSdkMsi ) ) ]
2016-03-07 12:20:50 -08:00
[BuildPlatforms(BuildPlatform.Windows)]
public static BuildTargetResult GenerateMsis ( BuildTargetContext c )
{
return c . Success ( ) ;
}
2016-03-17 17:54:01 -07:00
[ Target ( nameof ( MsiTargets . InitMsi ) ,
2016-05-16 15:30:53 -07:00
nameof ( GenerateCliSdkBundle ) ) ]
2016-03-17 17:54:01 -07:00
[BuildPlatforms(BuildPlatform.Windows)]
public static BuildTargetResult GenerateBundles ( BuildTargetContext c )
{
return c . Success ( ) ;
}
2016-03-07 12:20:50 -08:00
[Target]
[BuildPlatforms(BuildPlatform.Windows)]
2016-03-08 13:39:41 -08:00
public static BuildTargetResult GenerateCliSdkMsi ( BuildTargetContext c )
2016-03-07 12:20:50 -08:00
{
2016-03-14 18:54:45 -07:00
var cliSdkRoot = c . BuildContext . Get < string > ( "CLISDKRoot" ) ;
2016-03-24 18:05:14 -07:00
var upgradeCode = Utils . GenerateGuidFromName ( SdkMsi ) . ToString ( ) . ToUpper ( ) ;
2016-04-27 17:20:52 -07:00
var cliSdkBrandName = $"'{Monikers.CLISdkBrandName}'" ;
2016-03-24 18:05:14 -07:00
2016-03-07 12:20:50 -08:00
Cmd ( "powershell" , "-NoProfile" , "-NoLogo" ,
2016-03-17 17:54:01 -07:00
Path . Combine ( Dirs . RepoRoot , "packaging" , "windows" , "clisdk" , "generatemsi.ps1" ) ,
2016-04-26 18:00:02 -07:00
cliSdkRoot , SdkMsi , WixRoot , cliSdkBrandName , MsiVersion , CliDisplayVersion , CliNugetVersion , upgradeCode , Arch )
2016-03-07 12:20:50 -08:00
. Execute ( )
. EnsureSuccessful ( ) ;
return c . Success ( ) ;
}
[Target(nameof(MsiTargets.InitMsi))]
[BuildPlatforms(BuildPlatform.Windows)]
2016-03-17 17:54:01 -07:00
public static BuildTargetResult GenerateCliSdkBundle ( BuildTargetContext c )
2016-03-07 12:20:50 -08:00
{
2016-03-24 18:05:14 -07:00
var upgradeCode = Utils . GenerateGuidFromName ( SdkBundle ) . ToString ( ) . ToUpper ( ) ;
2016-04-27 17:20:52 -07:00
var cliSdkBrandName = $"'{Monikers.CLISdkBrandName}'" ;
2016-03-24 18:05:14 -07:00
2016-03-07 12:20:50 -08:00
Cmd ( "powershell" , "-NoProfile" , "-NoLogo" ,
2016-03-17 17:54:01 -07:00
Path . Combine ( Dirs . RepoRoot , "packaging" , "windows" , "clisdk" , "generatebundle.ps1" ) ,
2016-04-26 18:00:02 -07:00
SdkMsi , SharedFrameworkMsi , SharedHostMsi , SdkBundle , WixRoot , cliSdkBrandName , MsiVersion , CliDisplayVersion , CliNugetVersion , upgradeCode , Arch )
2016-03-07 14:52:41 -08:00
. EnvironmentVariable ( "Stage2Dir" , Dirs . Stage2 )
2016-03-07 12:20:50 -08:00
. Execute ( )
. EnsureSuccessful ( ) ;
return c . Success ( ) ;
}
[Target(nameof(MsiTargets.InitMsi))]
[BuildPlatforms(BuildPlatform.Windows)]
public static BuildTargetResult ExtractEngineFromBundle ( BuildTargetContext c )
{
2016-03-31 10:02:12 -07:00
ExtractEngineFromBundleHelper ( SdkBundle , SdkEngine ) ;
2016-03-07 12:20:50 -08:00
return c . Success ( ) ;
}
[Target(nameof(MsiTargets.InitMsi))]
[BuildPlatforms(BuildPlatform.Windows)]
public static BuildTargetResult ReattachEngineToBundle ( BuildTargetContext c )
{
2016-03-31 10:02:12 -07:00
ReattachEngineToBundleHelper ( SdkBundle , SdkEngine ) ;
return c . Success ( ) ;
}
private static void ExtractEngineFromBundleHelper ( string bundle , string engine )
{
Cmd ( $"{WixRoot}\\insignia.exe" , "-ib" , bundle , "-o" , engine )
2016-03-07 12:20:50 -08:00
. Execute ( )
. EnsureSuccessful ( ) ;
2016-03-31 10:02:12 -07:00
}
private static void ReattachEngineToBundleHelper ( string bundle , string engine )
{
Cmd ( $"{WixRoot}\\insignia.exe" , "-ab" , engine , bundle , "-o" , bundle )
. Execute ( )
. EnsureSuccessful ( ) ;
File . Delete ( engine ) ;
2016-03-07 12:20:50 -08:00
}
2016-05-16 15:30:53 -07:00
private static string GetEngineName ( string bundle )
{
var engine = $"{Path.GetFileNameWithoutExtension(bundle)}-{ENGINE}" ;
return Path . Combine ( Path . GetDirectoryName ( bundle ) , engine ) ;
}
2016-03-07 12:20:50 -08:00
}
}