1
+ # #########################################################################
2
+ # This is the Cake bootstrapper script for PowerShell.
3
+ # This file was downloaded from https://github.com/cake-build/resources
4
+ # Feel free to change this file to fit your needs.
5
+ # #########################################################################
6
+
7
+ <#
8
+
9
+ . SYNOPSIS
10
+ This is a Powershell script to bootstrap a Cake build.
11
+
12
+ . DESCRIPTION
13
+ This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
14
+ and execute your Cake build script with the parameters you provide.
15
+
16
+ . PARAMETER Script
17
+ The build script to execute.
18
+ . PARAMETER Target
19
+ The build script target to run.
20
+ . PARAMETER Configuration
21
+ The build configuration to use.
22
+ . PARAMETER Verbosity
23
+ Specifies the amount of information to be displayed.
24
+ . PARAMETER ShowDescription
25
+ Shows description about tasks.
26
+ . PARAMETER DryRun
27
+ Performs a dry run.
28
+ . PARAMETER SkipToolPackageRestore
29
+ Skips restoring of packages.
30
+ . PARAMETER ScriptArgs
31
+ Remaining arguments are added here.
32
+
33
+ . LINK
34
+ https://cakebuild.net
35
+
36
+ #>
37
+
38
+ [CmdletBinding ()]
39
+ Param (
40
+ [string ]$Script ,
41
+ [string ]$Target ,
42
+ [string ]$Configuration ,
43
+ [ValidateSet (" Quiet" , " Minimal" , " Normal" , " Verbose" , " Diagnostic" )]
44
+ [string ]$Verbosity ,
45
+ [switch ]$ShowDescription ,
46
+ [Alias (" WhatIf" , " Noop" )]
47
+ [switch ]$DryRun ,
48
+ [switch ]$SkipToolPackageRestore ,
49
+ [Parameter (Position = 0 , Mandatory = $false , ValueFromRemainingArguments = $true )]
50
+ [string []]$ScriptArgs
51
+ )
52
+
53
+ # This is an automatic variable in PowerShell Core, but not in Windows PowerShell 5.x
54
+ if (-not (Test-Path variable:global:IsCoreCLR)) {
55
+ $IsCoreCLR = $false
56
+ }
57
+
58
+ # Attempt to set highest encryption available for SecurityProtocol.
59
+ # PowerShell will not set this by default (until maybe .NET 4.6.x). This
60
+ # will typically produce a message for PowerShell v2 (just an info
61
+ # message though)
62
+ try {
63
+ # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
64
+ # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
65
+ # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
66
+ # installed (.NET 4.5 is an in-place upgrade).
67
+ # PowerShell Core already has support for TLS 1.2 so we can skip this if running in that.
68
+ if (-not $IsCoreCLR ) {
69
+ [System.Net.ServicePointManager ]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
70
+ }
71
+ } catch {
72
+ 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'
73
+ }
74
+
75
+ [Reflection.Assembly ]::LoadWithPartialName(" System.Security" ) | Out-Null
76
+ function MD5HashFile ([string ] $filePath )
77
+ {
78
+ if ([string ]::IsNullOrEmpty($filePath ) -or ! (Test-Path $filePath - PathType Leaf))
79
+ {
80
+ return $null
81
+ }
82
+
83
+ [System.IO.Stream ] $file = $null ;
84
+ [System.Security.Cryptography.MD5 ] $md5 = $null ;
85
+ try
86
+ {
87
+ $md5 = [System.Security.Cryptography.MD5 ]::Create()
88
+ $file = [System.IO.File ]::OpenRead($filePath )
89
+ return [System.BitConverter ]::ToString($md5.ComputeHash ($file ))
90
+ }
91
+ finally
92
+ {
93
+ if ($file -ne $null )
94
+ {
95
+ $file.Dispose ()
96
+ }
97
+
98
+ if ($md5 -ne $null )
99
+ {
100
+ $md5.Dispose ()
101
+ }
102
+ }
103
+ }
104
+
105
+ function GetProxyEnabledWebClient
106
+ {
107
+ $wc = New-Object System.Net.WebClient
108
+ $proxy = [System.Net.WebRequest ]::GetSystemWebProxy()
109
+ $proxy.Credentials = [System.Net.CredentialCache ]::DefaultCredentials
110
+ $wc.Proxy = $proxy
111
+ return $wc
112
+ }
113
+
114
+ Write-Host " Preparing to run build script..."
115
+
116
+ if (! $PSScriptRoot ){
117
+ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path - Parent
118
+ }
119
+
120
+ if (! $Script ){
121
+ $Script = Join-Path $PSScriptRoot " build.cake"
122
+ }
123
+ $TOOLS_DIR = Join-Path $PSScriptRoot " tools"
124
+ $ADDINS_DIR = Join-Path $TOOLS_DIR " Addins"
125
+ $MODULES_DIR = Join-Path $TOOLS_DIR " Modules"
126
+ $NUGET_EXE = Join-Path $TOOLS_DIR " nuget.exe"
127
+ $CAKE_EXE = Join-Path $TOOLS_DIR " Cake/Cake.exe"
128
+ $NUGET_URL = " https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
129
+ $PACKAGES_CONFIG = Join-Path $TOOLS_DIR " packages.config"
130
+ $PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR " packages.config.md5sum"
131
+ $ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR " packages.config"
132
+ $MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR " packages.config"
133
+
134
+ $env: CAKE_PATHS_TOOLS = $TOOLS_DIR
135
+ $env: CAKE_PATHS_ADDINS = $ADDINS_DIR
136
+ $env: CAKE_PATHS_MODULES = $MODULES_DIR
137
+
138
+ # Make sure tools folder exists
139
+ if ((Test-Path $PSScriptRoot ) -and ! (Test-Path $TOOLS_DIR )) {
140
+ Write-Verbose - Message " Creating tools directory..."
141
+ New-Item - Path $TOOLS_DIR - Type Directory | Out-Null
142
+ }
143
+
144
+ # Make sure that packages.config exist.
145
+ if (! (Test-Path $PACKAGES_CONFIG )) {
146
+ Write-Verbose - Message " Downloading packages.config..."
147
+ try {
148
+ $wc = GetProxyEnabledWebClient
149
+ $wc.DownloadFile (" https://cakebuild.net/download/bootstrapper/packages" , $PACKAGES_CONFIG )
150
+ } catch {
151
+ Throw " Could not download packages.config."
152
+ }
153
+ }
154
+
155
+ # Try find NuGet.exe in path if not exists
156
+ if (! (Test-Path $NUGET_EXE )) {
157
+ Write-Verbose - Message " Trying to find nuget.exe in PATH..."
158
+ $existingPaths = $Env: Path -Split ' ;' | Where-Object { (! [string ]::IsNullOrEmpty($_ )) -and (Test-Path $_ - PathType Container) }
159
+ $NUGET_EXE_IN_PATH = Get-ChildItem - Path $existingPaths - Filter " nuget.exe" | Select - First 1
160
+ if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName )) {
161
+ Write-Verbose - Message " Found in PATH at $ ( $NUGET_EXE_IN_PATH.FullName ) ."
162
+ $NUGET_EXE = $NUGET_EXE_IN_PATH.FullName
163
+ }
164
+ }
165
+
166
+ # Try download NuGet.exe if not exists
167
+ if (! (Test-Path $NUGET_EXE )) {
168
+ Write-Verbose - Message " Downloading NuGet.exe..."
169
+ try {
170
+ $wc = GetProxyEnabledWebClient
171
+ $wc.DownloadFile ($NUGET_URL , $NUGET_EXE )
172
+ } catch {
173
+ Throw " Could not download NuGet.exe."
174
+ }
175
+ }
176
+
177
+ # These are automatic variables in PowerShell Core, but not in Windows PowerShell 5.x
178
+ if (-not (Test-Path variable:global:ismacos)) {
179
+ $IsLinux = $false
180
+ $IsMacOS = $false
181
+ }
182
+
183
+ # Save nuget.exe path to environment to be available to child processed
184
+ $env: NUGET_EXE = $NUGET_EXE
185
+ $env: NUGET_EXE_INVOCATION = if ($IsLinux -or $IsMacOS ) {
186
+ " mono `" $NUGET_EXE `" "
187
+ } else {
188
+ " `" $NUGET_EXE `" "
189
+ }
190
+
191
+ # Restore tools from NuGet?
192
+ if (-Not $SkipToolPackageRestore.IsPresent ) {
193
+ Push-Location
194
+ Set-Location $TOOLS_DIR
195
+
196
+ # Check for changes in packages.config and remove installed tools if true.
197
+ [string ] $md5Hash = MD5HashFile $PACKAGES_CONFIG
198
+ if ((! (Test-Path $PACKAGES_CONFIG_MD5 )) -Or
199
+ ($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
200
+ Write-Verbose - Message " Missing or changed package.config hash..."
201
+ Get-ChildItem - Exclude packages.config, nuget.exe , Cake.Bakery |
202
+ Remove-Item - Recurse - Force
203
+ }
204
+
205
+ Write-Verbose - Message " Restoring tools from NuGet..."
206
+
207
+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $TOOLS_DIR `" "
208
+
209
+ if ($LASTEXITCODE -ne 0 ) {
210
+ Throw " An error occurred while restoring NuGet tools."
211
+ }
212
+ else
213
+ {
214
+ $md5Hash | Out-File $PACKAGES_CONFIG_MD5 - Encoding " ASCII"
215
+ }
216
+ Write-Verbose - Message ($NuGetOutput | Out-String )
217
+
218
+ Pop-Location
219
+ }
220
+
221
+ # Restore addins from NuGet
222
+ if (Test-Path $ADDINS_PACKAGES_CONFIG ) {
223
+ Push-Location
224
+ Set-Location $ADDINS_DIR
225
+
226
+ Write-Verbose - Message " Restoring addins from NuGet..."
227
+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $ADDINS_DIR `" "
228
+
229
+ if ($LASTEXITCODE -ne 0 ) {
230
+ Throw " An error occurred while restoring NuGet addins."
231
+ }
232
+
233
+ Write-Verbose - Message ($NuGetOutput | Out-String )
234
+
235
+ Pop-Location
236
+ }
237
+
238
+ # Restore modules from NuGet
239
+ if (Test-Path $MODULES_PACKAGES_CONFIG ) {
240
+ Push-Location
241
+ Set-Location $MODULES_DIR
242
+
243
+ Write-Verbose - Message " Restoring modules from NuGet..."
244
+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $MODULES_DIR `" "
245
+
246
+ if ($LASTEXITCODE -ne 0 ) {
247
+ Throw " An error occurred while restoring NuGet modules."
248
+ }
249
+
250
+ Write-Verbose - Message ($NuGetOutput | Out-String )
251
+
252
+ Pop-Location
253
+ }
254
+
255
+ # Make sure that Cake has been installed.
256
+ if (! (Test-Path $CAKE_EXE )) {
257
+ Throw " Could not find Cake.exe at $CAKE_EXE "
258
+ }
259
+
260
+ $CAKE_EXE_INVOCATION = if ($IsLinux -or $IsMacOS ) {
261
+ " mono `" $CAKE_EXE `" "
262
+ } else {
263
+ " `" $CAKE_EXE `" "
264
+ }
265
+
266
+ # Build an array (not a string) of Cake arguments to be joined later
267
+ $cakeArguments = @ ()
268
+ if ($Script ) { $cakeArguments += " `" $Script `" " }
269
+ if ($Target ) { $cakeArguments += " --target=`" $Target `" " }
270
+ if ($Configuration ) { $cakeArguments += " --configuration=$Configuration " }
271
+ if ($Verbosity ) { $cakeArguments += " --verbosity=$Verbosity " }
272
+ if ($ShowDescription ) { $cakeArguments += " --showdescription" }
273
+ if ($DryRun ) { $cakeArguments += " --dryrun" }
274
+ $cakeArguments += $ScriptArgs
275
+
276
+ # Start Cake
277
+ Write-Host " Running build script..."
278
+ Invoke-Expression " & $CAKE_EXE_INVOCATION $ ( $cakeArguments -join " " ) "
279
+ $cakeExitCode = $LASTEXITCODE
280
+
281
+ # Clean up environment variables that were created earlier in this bootstrapper
282
+ $env: CAKE_PATHS_TOOLS = $null
283
+ $env: CAKE_PATHS_ADDINS = $null
284
+ $env: CAKE_PATHS_MODULES = $null
285
+
286
+ # Return exit code
287
+ exit $cakeExitCode
0 commit comments