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
+ . SYNOPSIS
9
+ This is a Powershell script to bootstrap a Cake build.
10
+ . DESCRIPTION
11
+ This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
12
+ and execute your Cake build script with the parameters you provide.
13
+ . PARAMETER Script
14
+ The build script to execute.
15
+ . PARAMETER Target
16
+ The build script target to run.
17
+ . PARAMETER Configuration
18
+ The build configuration to use.
19
+ . PARAMETER Verbosity
20
+ Specifies the amount of information to be displayed.
21
+ . PARAMETER ShowDescription
22
+ Shows description about tasks.
23
+ . PARAMETER DryRun
24
+ Performs a dry run.
25
+ . PARAMETER Experimental
26
+ Uses the nightly builds of the Roslyn script engine.
27
+ . PARAMETER Mono
28
+ Uses the Mono Compiler rather than the Roslyn script engine.
29
+ . PARAMETER SkipToolPackageRestore
30
+ Skips restoring of packages.
31
+ . PARAMETER ScriptArgs
32
+ Remaining arguments are added here.
33
+ . LINK
34
+ https://cakebuild.net
35
+ #>
36
+
37
+ [CmdletBinding ()]
38
+ Param (
39
+ [string ]$Script = " build.cake" ,
40
+ [string ]$Target ,
41
+ [string ]$Configuration ,
42
+ [ValidateSet (" Quiet" , " Minimal" , " Normal" , " Verbose" , " Diagnostic" )]
43
+ [string ]$Verbosity ,
44
+ [switch ]$ShowDescription ,
45
+ [Alias (" WhatIf" , " Noop" )]
46
+ [switch ]$DryRun ,
47
+ [switch ]$Experimental ,
48
+ [switch ]$Mono ,
49
+ [switch ]$SkipToolPackageRestore ,
50
+ [Parameter (Position = 0 , Mandatory = $false , ValueFromRemainingArguments = $true )]
51
+ [string []]$ScriptArgs
52
+ )
53
+
54
+ [Reflection.Assembly ]::LoadWithPartialName(" System.Security" ) | Out-Null
55
+ function MD5HashFile ([string ] $filePath )
56
+ {
57
+ if ([string ]::IsNullOrEmpty($filePath ) -or ! (Test-Path $filePath - PathType Leaf))
58
+ {
59
+ return $null
60
+ }
61
+
62
+ [System.IO.Stream ] $file = $null ;
63
+ [System.Security.Cryptography.MD5 ] $md5 = $null ;
64
+ try
65
+ {
66
+ $md5 = [System.Security.Cryptography.MD5 ]::Create()
67
+ $file = [System.IO.File ]::OpenRead($filePath )
68
+ return [System.BitConverter ]::ToString($md5.ComputeHash ($file ))
69
+ }
70
+ finally
71
+ {
72
+ if ($file -ne $null )
73
+ {
74
+ $file.Dispose ()
75
+ }
76
+ }
77
+ }
78
+
79
+ function GetProxyEnabledWebClient
80
+ {
81
+ $wc = New-Object System.Net.WebClient
82
+ $proxy = [System.Net.WebRequest ]::GetSystemWebProxy()
83
+ $proxy.Credentials = [System.Net.CredentialCache ]::DefaultCredentials
84
+ $wc.Proxy = $proxy
85
+ return $wc
86
+ }
87
+
88
+ Write-Host " Preparing to run build script..."
89
+
90
+ if (! $PSScriptRoot ){
91
+ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path - Parent
92
+ }
93
+
94
+ $TOOLS_DIR = Join-Path $PSScriptRoot " tools"
95
+ $ADDINS_DIR = Join-Path $TOOLS_DIR " Addins"
96
+ $MODULES_DIR = Join-Path $TOOLS_DIR " Modules"
97
+ $NUGET_EXE = Join-Path $TOOLS_DIR " nuget.exe"
98
+ $CAKE_EXE = Join-Path $TOOLS_DIR " Cake/Cake.exe"
99
+ $NUGET_URL = " https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
100
+ $PACKAGES_CONFIG = Join-Path $TOOLS_DIR " packages.config"
101
+ $PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR " packages.config.md5sum"
102
+ $ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR " packages.config"
103
+ $MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR " packages.config"
104
+
105
+ # Make sure tools folder exists
106
+ if ((Test-Path $PSScriptRoot ) -and ! (Test-Path $TOOLS_DIR )) {
107
+ Write-Verbose - Message " Creating tools directory..."
108
+ New-Item - Path $TOOLS_DIR - Type directory | out-null
109
+ }
110
+
111
+ # Make sure that packages.config exist.
112
+ if (! (Test-Path $PACKAGES_CONFIG )) {
113
+ Write-Verbose - Message " Downloading packages.config..."
114
+ try {
115
+ $wc = GetProxyEnabledWebClient
116
+ $wc.DownloadFile (" https://cakebuild.net/download/bootstrapper/packages" , $PACKAGES_CONFIG ) } catch {
117
+ Throw " Could not download packages.config."
118
+ }
119
+ }
120
+
121
+ # Try find NuGet.exe in path if not exists
122
+ if (! (Test-Path $NUGET_EXE )) {
123
+ Write-Verbose - Message " Trying to find nuget.exe in PATH..."
124
+ $existingPaths = $Env: Path -Split ' ;' | Where-Object { (! [string ]::IsNullOrEmpty($_ )) -and (Test-Path $_ - PathType Container) }
125
+ $NUGET_EXE_IN_PATH = Get-ChildItem - Path $existingPaths - Filter " nuget.exe" | Select - First 1
126
+ if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName )) {
127
+ Write-Verbose - Message " Found in PATH at $ ( $NUGET_EXE_IN_PATH.FullName ) ."
128
+ $NUGET_EXE = $NUGET_EXE_IN_PATH.FullName
129
+ }
130
+ }
131
+
132
+ # Try download NuGet.exe if not exists
133
+ if (! (Test-Path $NUGET_EXE )) {
134
+ Write-Verbose - Message " Downloading NuGet.exe..."
135
+ try {
136
+ $wc = GetProxyEnabledWebClient
137
+ $wc.DownloadFile ($NUGET_URL , $NUGET_EXE )
138
+ } catch {
139
+ Throw " Could not download NuGet.exe."
140
+ }
141
+ }
142
+
143
+ # Save nuget.exe path to environment to be available to child processed
144
+ $ENV: NUGET_EXE = $NUGET_EXE
145
+
146
+ # Restore tools from NuGet?
147
+ if (-Not $SkipToolPackageRestore.IsPresent ) {
148
+ Push-Location
149
+ Set-Location $TOOLS_DIR
150
+
151
+ # Check for changes in packages.config and remove installed tools if true.
152
+ [string ] $md5Hash = MD5HashFile($PACKAGES_CONFIG )
153
+ if ((! (Test-Path $PACKAGES_CONFIG_MD5 )) -Or
154
+ ($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
155
+ Write-Verbose - Message " Missing or changed package.config hash..."
156
+ Remove-Item * - Recurse - Exclude packages.config, nuget.exe
157
+ }
158
+
159
+ Write-Verbose - Message " Restoring tools from NuGet..."
160
+ $NuGetOutput = Invoke-Expression " &`" $NUGET_EXE `" install -ExcludeVersion -OutputDirectory `" $TOOLS_DIR `" "
161
+
162
+ if ($LASTEXITCODE -ne 0 ) {
163
+ Throw " An error occured while restoring NuGet tools."
164
+ }
165
+ else
166
+ {
167
+ $md5Hash | Out-File $PACKAGES_CONFIG_MD5 - Encoding " ASCII"
168
+ }
169
+ Write-Verbose - Message ($NuGetOutput | out-string )
170
+
171
+ Pop-Location
172
+ }
173
+
174
+ # Restore addins from NuGet
175
+ if (Test-Path $ADDINS_PACKAGES_CONFIG ) {
176
+ Push-Location
177
+ Set-Location $ADDINS_DIR
178
+
179
+ Write-Verbose - Message " Restoring addins from NuGet..."
180
+ $NuGetOutput = Invoke-Expression " &`" $NUGET_EXE `" install -ExcludeVersion -OutputDirectory `" $ADDINS_DIR `" "
181
+
182
+ if ($LASTEXITCODE -ne 0 ) {
183
+ Throw " An error occured while restoring NuGet addins."
184
+ }
185
+
186
+ Write-Verbose - Message ($NuGetOutput | out-string )
187
+
188
+ Pop-Location
189
+ }
190
+
191
+ # Restore modules from NuGet
192
+ if (Test-Path $MODULES_PACKAGES_CONFIG ) {
193
+ Push-Location
194
+ Set-Location $MODULES_DIR
195
+
196
+ Write-Verbose - Message " Restoring modules from NuGet..."
197
+ $NuGetOutput = Invoke-Expression " &`" $NUGET_EXE `" install -ExcludeVersion -OutputDirectory `" $MODULES_DIR `" "
198
+
199
+ if ($LASTEXITCODE -ne 0 ) {
200
+ Throw " An error occured while restoring NuGet modules."
201
+ }
202
+
203
+ Write-Verbose - Message ($NuGetOutput | out-string )
204
+
205
+ Pop-Location
206
+ }
207
+
208
+ # Make sure that Cake has been installed.
209
+ if (! (Test-Path $CAKE_EXE )) {
210
+ Throw " Could not find Cake.exe at $CAKE_EXE "
211
+ }
212
+
213
+
214
+
215
+ # Build Cake arguments
216
+ $cakeArguments = @ (" $Script " );
217
+ if ($Target ) { $cakeArguments += " -target=$Target " }
218
+ if ($Configuration ) { $cakeArguments += " -configuration=$Configuration " }
219
+ if ($Verbosity ) { $cakeArguments += " -verbosity=$Verbosity " }
220
+ if ($ShowDescription ) { $cakeArguments += " -showdescription" }
221
+ if ($DryRun ) { $cakeArguments += " -dryrun" }
222
+ if ($Experimental ) { $cakeArguments += " -experimental" }
223
+ if ($Mono ) { $cakeArguments += " -mono" }
224
+ $cakeArguments += $ScriptArgs
225
+
226
+ # Start Cake
227
+ Write-Host " Running build script..."
228
+ & $CAKE_EXE $cakeArguments
229
+ exit $LASTEXITCODE
0 commit comments