55# #########################################################################
66
77<#
8+
89. SYNOPSIS
910This is a Powershell script to bootstrap a Cake build.
11+
1012. DESCRIPTION
1113This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
1214and execute your Cake build script with the parameters you provide.
15+
1316. PARAMETER Script
1417The build script to execute.
1518. PARAMETER Target
@@ -18,38 +21,52 @@ The build script target to run.
1821The build configuration to use.
1922. PARAMETER Verbosity
2023Specifies the amount of information to be displayed.
21- . PARAMETER Experimental
22- Tells Cake to use the latest Roslyn release.
23- . PARAMETER WhatIf
24- Performs a dry run of the build script.
25- No tasks will be executed.
26- . PARAMETER Mono
27- Tells Cake to use the Mono scripting engine.
24+ . PARAMETER ShowDescription
25+ Shows description about tasks.
26+ . PARAMETER DryRun
27+ Performs a dry run.
2828. PARAMETER SkipToolPackageRestore
2929Skips restoring of packages.
3030. PARAMETER ScriptArgs
3131Remaining arguments are added here.
32+
3233. LINK
33- http://cakebuild.net
34+ https://cakebuild.net
35+
3436#>
3537
3638[CmdletBinding ()]
3739Param (
3840 [string ]$Script = " recipe.cake" ,
39- [string ]$Target = " Default" ,
40- [ValidateSet (" Release" , " Debug" )]
41- [string ]$Configuration = " Release" ,
41+ [string ]$Target ,
42+ [string ]$Configuration ,
4243 [ValidateSet (" Quiet" , " Minimal" , " Normal" , " Verbose" , " Diagnostic" )]
43- [string ]$Verbosity = " Verbose" ,
44- [switch ]$Experimental ,
45- [Alias (" DryRun" , " Noop" )]
46- [switch ]$WhatIf ,
47- [switch ]$Mono ,
44+ [string ]$Verbosity ,
45+ [switch ]$ShowDescription ,
46+ [Alias (" WhatIf" , " Noop" )]
47+ [switch ]$DryRun ,
4848 [switch ]$SkipToolPackageRestore ,
4949 [Parameter (Position = 0 , Mandatory = $false , ValueFromRemainingArguments = $true )]
5050 [string []]$ScriptArgs
5151)
5252
53+ # Attempt to set highest encryption available for SecurityProtocol.
54+ # PowerShell will not set this by default (until maybe .NET 4.6.x). This
55+ # will typically produce a message for PowerShell v2 (just an info
56+ # message though)
57+ try {
58+ # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
59+ # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
60+ # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
61+ # installed (.NET 4.5 is an in-place upgrade).
62+ # PowerShell Core already has support for TLS 1.2 so we can skip this if running in that.
63+ if (-not $IsCoreCLR ) {
64+ [System.Net.ServicePointManager ]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
65+ }
66+ } catch {
67+ 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'
68+ }
69+
5370[Reflection.Assembly ]::LoadWithPartialName(" System.Security" ) | Out-Null
5471function MD5HashFile ([string ] $filePath )
5572{
@@ -75,57 +92,53 @@ function MD5HashFile([string] $filePath)
7592 }
7693}
7794
95+ function GetProxyEnabledWebClient
96+ {
97+ $wc = New-Object System.Net.WebClient
98+ $proxy = [System.Net.WebRequest ]::GetSystemWebProxy()
99+ $proxy.Credentials = [System.Net.CredentialCache ]::DefaultCredentials
100+ $wc.Proxy = $proxy
101+ return $wc
102+ }
103+
78104Write-Host " Preparing to run build script..."
79105
80106if (! $PSScriptRoot ){
81107 $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path - Parent
82108}
83109
84110$TOOLS_DIR = Join-Path $PSScriptRoot " tools"
111+ $ADDINS_DIR = Join-Path $TOOLS_DIR " Addins"
112+ $MODULES_DIR = Join-Path $TOOLS_DIR " Modules"
85113$NUGET_EXE = Join-Path $TOOLS_DIR " nuget.exe"
86114$CAKE_EXE = Join-Path $TOOLS_DIR " Cake/Cake.exe"
87115$NUGET_URL = " https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
88116$PACKAGES_CONFIG = Join-Path $TOOLS_DIR " packages.config"
89117$PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR " packages.config.md5sum"
90-
91- # Should we use mono?
92- $UseMono = " " ;
93- if ($Mono.IsPresent ) {
94- Write-Verbose - Message " Using the Mono based scripting engine."
95- $UseMono = " -mono"
96- }
97-
98- # Should we use the new Roslyn?
99- $UseExperimental = " " ;
100- if ($Experimental.IsPresent -and ! ($Mono.IsPresent )) {
101- Write-Verbose - Message " Using experimental version of Roslyn."
102- $UseExperimental = " -experimental"
103- }
104-
105- # Is this a dry run?
106- $UseDryRun = " " ;
107- if ($WhatIf.IsPresent ) {
108- $UseDryRun = " -dryrun"
109- }
118+ $ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR " packages.config"
119+ $MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR " packages.config"
110120
111121# Make sure tools folder exists
112122if ((Test-Path $PSScriptRoot ) -and ! (Test-Path $TOOLS_DIR )) {
113123 Write-Verbose - Message " Creating tools directory..."
114- New-Item - Path $TOOLS_DIR - Type directory | out-null
124+ New-Item - Path $TOOLS_DIR - Type Directory | Out-Null
115125}
116126
117127# Make sure that packages.config exist.
118128if (! (Test-Path $PACKAGES_CONFIG )) {
119129 Write-Verbose - Message " Downloading packages.config..."
120- try { (New-Object System.Net.WebClient).DownloadFile(" http://cakebuild.net/download/bootstrapper/packages" , $PACKAGES_CONFIG ) } catch {
130+ try {
131+ $wc = GetProxyEnabledWebClient
132+ $wc.DownloadFile (" https://cakebuild.net/download/bootstrapper/packages" , $PACKAGES_CONFIG )
133+ } catch {
121134 Throw " Could not download packages.config."
122135 }
123136}
124137
125138# Try find NuGet.exe in path if not exists
126139if (! (Test-Path $NUGET_EXE )) {
127140 Write-Verbose - Message " Trying to find nuget.exe in PATH..."
128- $existingPaths = $Env: Path -Split ' ;' | Where-Object { (! [string ]::IsNullOrEmpty($_ )) -and (Test-Path $_ ) }
141+ $existingPaths = $Env: Path -Split ' ;' | Where-Object { (! [string ]::IsNullOrEmpty($_ )) -and (Test-Path $_ - PathType Container ) }
129142 $NUGET_EXE_IN_PATH = Get-ChildItem - Path $existingPaths - Filter " nuget.exe" | Select - First 1
130143 if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName )) {
131144 Write-Verbose - Message " Found in PATH at $ ( $NUGET_EXE_IN_PATH.FullName ) ."
@@ -137,39 +150,82 @@ if (!(Test-Path $NUGET_EXE)) {
137150if (! (Test-Path $NUGET_EXE )) {
138151 Write-Verbose - Message " Downloading NuGet.exe..."
139152 try {
140- (New-Object System.Net.WebClient).DownloadFile($NUGET_URL , $NUGET_EXE )
153+ $wc = GetProxyEnabledWebClient
154+ $wc.DownloadFile ($NUGET_URL , $NUGET_EXE )
141155 } catch {
142156 Throw " Could not download NuGet.exe."
143157 }
144158}
145159
146160# Save nuget.exe path to environment to be available to child processed
147- $ENV: NUGET_EXE = $NUGET_EXE
161+ $env: NUGET_EXE = $NUGET_EXE
162+ $env: NUGET_EXE_INVOCATION = if ($IsLinux -or $IsMacOS ) {
163+ " mono `" $NUGET_EXE `" "
164+ } else {
165+ " `" $NUGET_EXE `" "
166+ }
148167
149168# Restore tools from NuGet?
150169if (-Not $SkipToolPackageRestore.IsPresent ) {
151170 Push-Location
152171 Set-Location $TOOLS_DIR
153172
154173 # Check for changes in packages.config and remove installed tools if true.
155- [string ] $md5Hash = MD5HashFile( $PACKAGES_CONFIG )
174+ [string ] $md5Hash = MD5HashFile $PACKAGES_CONFIG
156175 if ((! (Test-Path $PACKAGES_CONFIG_MD5 )) -Or
157- ($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
176+ ($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
158177 Write-Verbose - Message " Missing or changed package.config hash..."
159- Remove-Item * - Recurse - Exclude packages.config, nuget.exe
178+ Get-ChildItem - Exclude packages.config, nuget.exe , Cake.Bakery |
179+ Remove-Item - Recurse
160180 }
161181
162182 Write-Verbose - Message " Restoring tools from NuGet..."
163- $NuGetOutput = Invoke-Expression " &`" $NUGET_EXE `" install -ExcludeVersion -PreRelease -OutputDirectory `" $TOOLS_DIR `" -Source https://www.myget.org/F/cake/api/v3/index.json"
183+
184+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $TOOLS_DIR `" "
164185
165186 if ($LASTEXITCODE -ne 0 ) {
166- Throw " An error occured while restoring NuGet tools."
187+ Throw " An error occurred while restoring NuGet tools."
167188 }
168189 else
169190 {
170191 $md5Hash | Out-File $PACKAGES_CONFIG_MD5 - Encoding " ASCII"
171192 }
172- Write-Verbose - Message ($NuGetOutput | out-string )
193+ Write-Verbose - Message ($NuGetOutput | Out-String )
194+
195+ Pop-Location
196+ }
197+
198+ # Restore addins from NuGet
199+ if (Test-Path $ADDINS_PACKAGES_CONFIG ) {
200+ Push-Location
201+ Set-Location $ADDINS_DIR
202+
203+ Write-Verbose - Message " Restoring addins from NuGet..."
204+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $ADDINS_DIR `" "
205+
206+ if ($LASTEXITCODE -ne 0 ) {
207+ Throw " An error occurred while restoring NuGet addins."
208+ }
209+
210+ Write-Verbose - Message ($NuGetOutput | Out-String )
211+
212+ Pop-Location
213+ }
214+
215+ # Restore modules from NuGet
216+ if (Test-Path $MODULES_PACKAGES_CONFIG ) {
217+ Push-Location
218+ Set-Location $MODULES_DIR
219+
220+ Write-Verbose - Message " Restoring modules from NuGet..."
221+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $MODULES_DIR `" "
222+
223+ if ($LASTEXITCODE -ne 0 ) {
224+ Throw " An error occurred while restoring NuGet modules."
225+ }
226+
227+ Write-Verbose - Message ($NuGetOutput | Out-String )
228+
173229 Pop-Location
174230}
175231
@@ -178,7 +234,23 @@ if (!(Test-Path $CAKE_EXE)) {
178234 Throw " Could not find Cake.exe at $CAKE_EXE "
179235}
180236
237+ $CAKE_EXE_INVOCATION = if ($IsLinux -or $IsMacOS ) {
238+ " mono `" $CAKE_EXE `" "
239+ } else {
240+ " `" $CAKE_EXE `" "
241+ }
242+
243+
244+ # Build Cake arguments
245+ $cakeArguments = @ (" $Script " );
246+ if ($Target ) { $cakeArguments += " -target=$Target " }
247+ if ($Configuration ) { $cakeArguments += " -configuration=$Configuration " }
248+ if ($Verbosity ) { $cakeArguments += " -verbosity=$Verbosity " }
249+ if ($ShowDescription ) { $cakeArguments += " -showdescription" }
250+ if ($DryRun ) { $cakeArguments += " -dryrun" }
251+ $cakeArguments += $ScriptArgs
252+
181253# Start Cake
182254Write-Host " Running build script..."
183- Invoke-Expression " & `" $CAKE_EXE `" `" $Script `" -target= `" $Target `" -configuration= `" $Configuration `" -verbosity= `" $Verbosity `" $UseMono $UseDryRun $UseExperimental $ScriptArgs "
255+ Invoke-Expression " & $CAKE_EXE_INVOCATION $ ( $cakeArguments -join " " ) "
184256exit $LASTEXITCODE
0 commit comments