1- $CakeVersion = " 0.25.0"
2- $DotNetChannel = " LTS" ;
3- $DotNetVersion = " 2.1.4" ;
4- $IsRunningOnUnix = [System.Environment ]::OSVersion.Platform -eq [System.PlatformID ]::Unix
1+ # !/usr/bin/env pwsh
2+ $DotNetInstallerUri = ' https://dot.net/v1/dotnet-install.ps1' ;
3+ $DotNetUnixInstallerUri = ' https://dot.net/v1/dotnet-install.sh'
4+ $DotNetChannel = ' LTS'
5+ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path - Parent
6+
7+ [string ] $CakeVersion = ' '
8+ [string ] $DotNetVersion = ' '
9+ foreach ($line in Get-Content (Join-Path $PSScriptRoot ' build.config' ))
10+ {
11+ if ($line -like ' CAKE_VERSION=*' ) {
12+ $CakeVersion = $line.SubString (13 )
13+ }
14+ elseif ($line -like ' DOTNET_VERSION=*' ) {
15+ $DotNetVersion = $line.SubString (15 )
16+ }
17+ }
18+
19+
20+ if ([string ]::IsNullOrEmpty($CakeVersion ) -or [string ]::IsNullOrEmpty($DotNetVersion )) {
21+ ' Failed to parse Cake / .NET Core SDK Version'
22+ exit 1
23+ }
524
625# Make sure tools folder exists
7- $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path - Parent
826$ToolPath = Join-Path $PSScriptRoot " tools"
927if (! (Test-Path $ToolPath )) {
1028 Write-Verbose " Creating tools directory..."
11- New-Item - Path $ToolPath - Type directory | out-null
29+ New-Item - Path $ToolPath - Type Directory - Force | out-null
30+ }
31+
32+
33+ if ($PSVersionTable.PSEdition -ne ' Core' ) {
34+ # Attempt to set highest encryption available for SecurityProtocol.
35+ # PowerShell will not set this by default (until maybe .NET 4.6.x). This
36+ # will typically produce a message for PowerShell v2 (just an info
37+ # message though)
38+ try {
39+ # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
40+ # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
41+ # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
42+ # installed (.NET 4.5 is an in-place upgrade).
43+ [System.Net.ServicePointManager ]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
44+ } catch {
45+ Write-Output ' Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3'
46+ }
1247}
1348
1449# ##########################################################################
1550# INSTALL .NET CORE CLI
1651# ##########################################################################
1752
53+ $env: DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
54+ $env: DOTNET_CLI_TELEMETRY_OPTOUT = 1
55+ $env: DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX = 1
56+
57+
1858Function Remove-PathVariable ([string ]$VariableToRemove )
1959{
60+ $SplitChar = ' ;'
61+ if ($IsMacOS -or $IsLinux ) {
62+ $SplitChar = ' :'
63+ }
64+
2065 $path = [Environment ]::GetEnvironmentVariable(" PATH" , " User" )
2166 if ($path -ne $null )
2267 {
23- $newItems = $path.Split (' ; ' , [StringSplitOptions ]::RemoveEmptyEntries) | Where-Object { " $ ( $_ ) " -inotlike $VariableToRemove }
24- [Environment ]::SetEnvironmentVariable(" PATH" , [System.String ]::Join(' ; ' , $newItems ), " User" )
68+ $newItems = $path.Split ($SplitChar , [StringSplitOptions ]::RemoveEmptyEntries) | Where-Object { " $ ( $_ ) " -inotlike $VariableToRemove }
69+ [Environment ]::SetEnvironmentVariable(" PATH" , [System.String ]::Join($SplitChar , $newItems ), " User" )
2570 }
2671
2772 $path = [Environment ]::GetEnvironmentVariable(" PATH" , " Process" )
2873 if ($path -ne $null )
2974 {
30- $newItems = $path.Split (' ; ' , [StringSplitOptions ]::RemoveEmptyEntries) | Where-Object { " $ ( $_ ) " -inotlike $VariableToRemove }
31- [Environment ]::SetEnvironmentVariable(" PATH" , [System.String ]::Join(' ; ' , $newItems ), " Process" )
75+ $newItems = $path.Split ($SplitChar , [StringSplitOptions ]::RemoveEmptyEntries) | Where-Object { " $ ( $_ ) " -inotlike $VariableToRemove }
76+ [Environment ]::SetEnvironmentVariable(" PATH" , [System.String ]::Join($SplitChar , $newItems ), " Process" )
3277 }
3378}
3479
3580# Get .NET Core CLI path if installed.
36- $env: DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
37- $env: DOTNET_CLI_TELEMETRY_OPTOUT = 1
38- $FoundDotNetCliVersion = $null
39-
40-
81+ $FoundDotNetCliVersion = $null ;
4182if (Get-Command dotnet - ErrorAction SilentlyContinue) {
42- $FoundDotNetCliVersion = dotnet -- version
83+ $FoundDotNetCliVersion = dotnet -- version;
4384}
4485
45- if ($FoundDotNetCliVersion -ne $DotNetVersion )
46- {
86+ if ($FoundDotNetCliVersion -ne $DotNetVersion ) {
4787 $InstallPath = Join-Path $PSScriptRoot " .dotnet"
48- Remove-PathVariable " $InstallPath "
49- $env: PATH = " $InstallPath ;$env: PATH "
50-
5188 if (! (Test-Path $InstallPath )) {
52- New-Item - ItemType Directory - Force $InstallPath | Out-Null ;
89+ New-Item - Path $InstallPath - ItemType Directory - Force | Out-Null ;
5390 }
5491
55- [string ] $InstalledDotNetVersion = Get-ChildItem - Path ./ .dotnet - File `
56- | Where-Object { $_.Name -eq ' dotnet' -or $_.Name -eq ' dotnet.exe' } `
57- | ForEach-Object { & $_.FullName -- version }
92+ if ($IsMacOS -or $IsLinux ) {
93+ $ScriptPath = Join-Path $InstallPath ' dotnet-install.sh'
94+ (New-Object System.Net.WebClient).DownloadFile($DotNetUnixInstallerUri , $ScriptPath );
95+ & bash $ScriptPath -- version " $DotNetVersion " -- install-dir " $InstallPath " -- channel " $DotNetChannel " -- no- path
5896
59- if ($InstalledDotNetVersion -eq $DotNetVersion )
60- {
61- }
62- elseif ($IsRunningOnUnix )
63- {
64- $DotNetInstallerUri = " https://raw.githubusercontent.com/dotnet/cli/v2.1.4/scripts/obtain/dotnet-install.sh" ;
65- (New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri , " $InstallPath /dotnet-install.sh" );
66- sudo bash " $InstallPath /dotnet-install.sh" -- version $DotNetVersion -- install-dir " $InstallPath " -- no- path
97+ Remove-PathVariable " $InstallPath "
98+ $env: PATH = " $ ( $InstallPath ) :$env: PATH "
6799 }
68- else
69- {
70- $DotNetInstallerUri = " https://raw.githubusercontent.com/dotnet/cli/v2.1.4/scripts/obtain/dotnet-install.ps1" ;
71- (New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri , " $InstallPath \dotnet-install.ps1" );
72- & $InstallPath \dotnet- install.ps1 - Channel $DotNetChannel - Version $DotNetVersion - InstallDir $InstallPath ;
100+ else {
101+ $ScriptPath = Join-Path $InstallPath ' dotnet-install.ps1'
102+ (New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri , $ScriptPath );
103+ & $ScriptPath - Channel $DotNetChannel - Version $DotNetVersion - InstallDir $InstallPath ;
104+
105+ Remove-PathVariable " $InstallPath "
106+ $env: PATH = " $InstallPath ;$env: PATH "
73107 }
108+ $env: DOTNET_ROOT = $InstallPath
74109}
75110
76111# ##########################################################################
77112# INSTALL CAKE
78113# ##########################################################################
79114
80- Add-Type - AssemblyName System.IO.Compression.FileSystem
81- Function Unzip
82- {
83- param ([string ]$zipfile , [string ]$outpath )
115+ # Make sure Cake has been installed.
116+ [string ] $CakeExePath = ' '
117+ [string ] $CakeInstalledVersion = Get-Command dotnet- cake - ErrorAction SilentlyContinue | % {& $_.Source -- version}
84118
85- [System.IO.Compression.ZipFile ]::ExtractToDirectory($zipfile , $outpath )
119+ if ($CakeInstalledVersion -eq $CakeVersion ) {
120+ # Cake found locally
121+ $CakeExePath = (Get-Command dotnet- cake).Source
86122}
123+ else {
124+ $CakePath = [System.IO.Path ]::Combine($ToolPath , ' .store' , ' cake.tool' , $CakeVersion ) # Old PowerShell versions Join-Path only supports one child path
87125
126+ $CakeExePath = (Get-ChildItem - Path $ToolPath - Filter " dotnet-cake*" - File| ForEach-Object FullName | Select-Object - First 1 )
88127
89- # Make sure Cake has been installed.
90- $CakePath = Join-Path $ToolPath " Cake.$CakeVersion "
91- $CakeExePath = Join-Path $CakePath " Cake.exe"
92- $CakeZipPath = Join-Path $ToolPath " Cake.zip"
93- if (! (Test-Path $CakeExePath )) {
94- Write-Host " Installing Cake $CakeVersion ..."
95- (New-Object System.Net.WebClient).DownloadFile(" https://www.nuget.org/api/v2/package/Cake/$CakeVersion " , $CakeZipPath )
96- Unzip $CakeZipPath $CakePath
97- Remove-Item $CakeZipPath
128+
129+ if ((! (Test-Path - Path $CakePath - PathType Container)) -or (! (Test-Path $CakeExePath - PathType Leaf))) {
130+
131+ if ((! [string ]::IsNullOrEmpty($CakeExePath )) -and (Test-Path $CakeExePath - PathType Leaf))
132+ {
133+ & dotnet tool uninstall -- tool- path $ToolPath Cake.Tool
134+ }
135+
136+ & dotnet tool install -- tool- path $ToolPath -- version $CakeVersion Cake.Tool
137+ if ($LASTEXITCODE -ne 0 )
138+ {
139+ ' Failed to install cake'
140+ exit 1
141+ }
142+ $CakeExePath = (Get-ChildItem - Path $ToolPath - Filter " dotnet-cake*" - File| ForEach-Object FullName | Select-Object - First 1 )
143+ }
98144}
99145
100146# ##########################################################################
101147# RUN BUILD SCRIPT
102148# ##########################################################################
103- if ($IsRunningOnUnix )
149+ & " $CakeExePath " ./ build.cake -- bootstrap
150+ if ($LASTEXITCODE -eq 0 )
104151{
105- & mono " $CakeExePath " -- bootstrap
106- if ($LASTEXITCODE -eq 0 )
107- {
108- & mono " $CakeExePath " $args
109- }
110- exit $LASTEXITCODE
152+ & " $CakeExePath " ./ build.cake $args
111153}
112- else
113- {
114- & " $CakeExePath " -- bootstrap
115- if ($LASTEXITCODE -eq 0 )
116- {
117- & " $CakeExePath " $args
118- }
119- exit $LASTEXITCODE
120- }
154+ exit $LASTEXITCODE
0 commit comments