@@ -21,88 +21,136 @@ The build script target to run.
2121The build configuration to use.
2222. PARAMETER Verbosity
2323Specifies the amount of information to be displayed.
24- . PARAMETER Experimental
25- Tells Cake to use the latest Roslyn release.
26- . PARAMETER WhatIf
27- Performs a dry run of the build script.
28- No tasks will be executed.
29- . PARAMETER Mono
30- Tells Cake to use the Mono scripting engine.
24+ . PARAMETER ShowDescription
25+ Shows description about tasks.
26+ . PARAMETER DryRun
27+ Performs a dry run.
3128. PARAMETER SkipToolPackageRestore
3229Skips restoring of packages.
3330. PARAMETER ScriptArgs
3431Remaining arguments are added here.
3532
3633. LINK
37- http ://cakebuild.net
34+ https ://cakebuild.net
3835
3936#>
4037
4138[CmdletBinding ()]
4239Param (
43- [string ]$Script = " build.cake " ,
44- [string ]$Target = " Default " ,
45- [string ]$Configuration = " Release " ,
40+ [string ]$Script ,
41+ [string ]$Target ,
42+ [string ]$Configuration ,
4643 [ValidateSet (" Quiet" , " Minimal" , " Normal" , " Verbose" , " Diagnostic" )]
47- [string ]$Verbosity = " Verbose" ,
48- [switch ]$Experimental ,
49- [Alias (" DryRun" , " Noop" )]
50- [switch ]$WhatIf ,
51- [switch ]$Mono ,
44+ [string ]$Verbosity ,
45+ [switch ]$ShowDescription ,
46+ [Alias (" WhatIf" , " Noop" )]
47+ [switch ]$DryRun ,
5248 [switch ]$SkipToolPackageRestore ,
5349 [Parameter (Position = 0 , Mandatory = $false , ValueFromRemainingArguments = $true )]
54- [string []]$ScriptArgs ,
55- [string ]$Version = " 0.0.0" ,
56- [string ]$Build = " 0"
50+ [string []]$ScriptArgs
5751)
5852
59- Write-Host " Preparing to run build script..."
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+ }
6057
61- $PSScriptRoot = split-path - parent $MyInvocation.MyCommand.Definition ;
62- $TOOLS_DIR = Join-Path $PSScriptRoot " tools"
63- $NUGET_EXE = Join-Path $TOOLS_DIR " nuget.exe"
64- $NUGET_URL = " http://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
65- $CAKE_EXE = Join-Path $TOOLS_DIR " Cake/Cake.exe"
66- $PACKAGES_CONFIG = Join-Path $TOOLS_DIR " packages.config"
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+ }
6774
68- # Should we use mono?
69- $UseMono = " " ;
70- if ($Mono.IsPresent ) {
71- Write-Verbose - Message " Using the Mono based scripting engine."
72- $UseMono = " -mono"
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+ }
7398}
7499
75- # Should we use the new Roslyn?
76- $UseExperimental = " " ;
77- if ($Experimental.IsPresent -and ! ($Mono.IsPresent )) {
78- Write-Verbose - Message " Using experimental version of Roslyn."
79- $UseExperimental = " -experimental"
100+ function GetProxyEnabledWebClient
101+ {
102+ $wc = New-Object System.Net.WebClient
103+ $proxy = [System.Net.WebRequest ]::GetSystemWebProxy()
104+ $proxy.Credentials = [System.Net.CredentialCache ]::DefaultCredentials
105+ $wc.Proxy = $proxy
106+ return $wc
107+ }
108+
109+ Write-Host " Preparing to run build script..."
110+
111+ if (! $PSScriptRoot ){
112+ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path - Parent
80113}
81114
82- # Is this a dry run?
83- $UseDryRun = " " ;
84- if ($WhatIf.IsPresent ) {
85- $UseDryRun = " -dryrun"
115+ if (! $Script ){
116+ $Script = Join-Path $PSScriptRoot " build.cake"
86117}
118+ $TOOLS_DIR = Join-Path $PSScriptRoot " tools"
119+ $ADDINS_DIR = Join-Path $TOOLS_DIR " Addins"
120+ $MODULES_DIR = Join-Path $TOOLS_DIR " Modules"
121+ $NUGET_EXE = Join-Path $TOOLS_DIR " nuget.exe"
122+ $CAKE_EXE = Join-Path $TOOLS_DIR " Cake/Cake.exe"
123+ $NUGET_URL = " https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
124+ $PACKAGES_CONFIG = Join-Path $TOOLS_DIR " packages.config"
125+ $PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR " packages.config.md5sum"
126+ $ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR " packages.config"
127+ $MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR " packages.config"
128+
129+ $env: CAKE_PATHS_TOOLS = $TOOLS_DIR
130+ $env: CAKE_PATHS_ADDINS = $ADDINS_DIR
131+ $env: CAKE_PATHS_MODULES = $MODULES_DIR
87132
88133# Make sure tools folder exists
89134if ((Test-Path $PSScriptRoot ) -and ! (Test-Path $TOOLS_DIR )) {
90135 Write-Verbose - Message " Creating tools directory..."
91- New-Item - Path $TOOLS_DIR - Type directory | out-null
136+ New-Item - Path $TOOLS_DIR - Type Directory | Out-Null
92137}
93138
94139# Make sure that packages.config exist.
95140if (! (Test-Path $PACKAGES_CONFIG )) {
96141 Write-Verbose - Message " Downloading packages.config..."
97- try { Invoke-WebRequest - Uri http:// cakebuild.net/ download/ bootstrapper/ packages - OutFile $PACKAGES_CONFIG } catch {
142+ try {
143+ $wc = GetProxyEnabledWebClient
144+ $wc.DownloadFile (" https://cakebuild.net/download/bootstrapper/packages" , $PACKAGES_CONFIG )
145+ } catch {
98146 Throw " Could not download packages.config."
99147 }
100148}
101149
102150# Try find NuGet.exe in path if not exists
103151if (! (Test-Path $NUGET_EXE )) {
104152 Write-Verbose - Message " Trying to find nuget.exe in PATH..."
105- $existingPaths = $Env: Path -Split ' ;' | Where-Object { Test-Path $_ }
153+ $existingPaths = $Env: Path -Split ' ;' | Where-Object { ( ! [ string ]::IsNullOrEmpty( $_ )) -and ( Test-Path $_ - PathType Container) }
106154 $NUGET_EXE_IN_PATH = Get-ChildItem - Path $existingPaths - Filter " nuget.exe" | Select - First 1
107155 if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName )) {
108156 Write-Verbose - Message " Found in PATH at $ ( $NUGET_EXE_IN_PATH.FullName ) ."
@@ -114,25 +162,88 @@ if (!(Test-Path $NUGET_EXE)) {
114162if (! (Test-Path $NUGET_EXE )) {
115163 Write-Verbose - Message " Downloading NuGet.exe..."
116164 try {
117- (New-Object System.Net.WebClient).DownloadFile($NUGET_URL , $NUGET_EXE )
165+ $wc = GetProxyEnabledWebClient
166+ $wc.DownloadFile ($NUGET_URL , $NUGET_EXE )
118167 } catch {
119168 Throw " Could not download NuGet.exe."
120169 }
121170}
122171
172+ # These are automatic variables in PowerShell Core, but not in Windows PowerShell 5.x
173+ if (-not (Test-Path variable:global:ismacos)) {
174+ $IsLinux = $false
175+ $IsMacOS = $false
176+ }
177+
123178# Save nuget.exe path to environment to be available to child processed
124- $ENV: NUGET_EXE = $NUGET_EXE
179+ $env: NUGET_EXE = $NUGET_EXE
180+ $env: NUGET_EXE_INVOCATION = if ($IsLinux -or $IsMacOS ) {
181+ " mono `" $NUGET_EXE `" "
182+ } else {
183+ " `" $NUGET_EXE `" "
184+ }
125185
126186# Restore tools from NuGet?
127187if (-Not $SkipToolPackageRestore.IsPresent ) {
128188 Push-Location
129189 Set-Location $TOOLS_DIR
190+
191+ # Check for changes in packages.config and remove installed tools if true.
192+ [string ] $md5Hash = MD5HashFile $PACKAGES_CONFIG
193+ if ((! (Test-Path $PACKAGES_CONFIG_MD5 )) -Or
194+ ($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
195+ Write-Verbose - Message " Missing or changed package.config hash..."
196+ Get-ChildItem - Exclude packages.config, nuget.exe , Cake.Bakery |
197+ Remove-Item - Recurse - Force
198+ }
199+
130200 Write-Verbose - Message " Restoring tools from NuGet..."
131- $NuGetOutput = Invoke-Expression " &`" $NUGET_EXE `" install -ExcludeVersion -OutputDirectory `" $TOOLS_DIR `" "
201+
202+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $TOOLS_DIR `" "
203+
204+ if ($LASTEXITCODE -ne 0 ) {
205+ Throw " An error occurred while restoring NuGet tools."
206+ }
207+ else
208+ {
209+ $md5Hash | Out-File $PACKAGES_CONFIG_MD5 - Encoding " ASCII"
210+ }
211+ Write-Verbose - Message ($NuGetOutput | Out-String )
212+
213+ Pop-Location
214+ }
215+
216+ # Restore addins from NuGet
217+ if (Test-Path $ADDINS_PACKAGES_CONFIG ) {
218+ Push-Location
219+ Set-Location $ADDINS_DIR
220+
221+ Write-Verbose - Message " Restoring addins from NuGet..."
222+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $ADDINS_DIR `" "
223+
132224 if ($LASTEXITCODE -ne 0 ) {
133- Throw " An error occured while restoring NuGet tools ."
225+ Throw " An error occurred while restoring NuGet addins ."
134226 }
135- Write-Verbose - Message ($NuGetOutput | out-string )
227+
228+ Write-Verbose - Message ($NuGetOutput | Out-String )
229+
230+ Pop-Location
231+ }
232+
233+ # Restore modules from NuGet
234+ if (Test-Path $MODULES_PACKAGES_CONFIG ) {
235+ Push-Location
236+ Set-Location $MODULES_DIR
237+
238+ Write-Verbose - Message " Restoring modules from NuGet..."
239+ $NuGetOutput = Invoke-Expression " & $env: NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `" $MODULES_DIR `" "
240+
241+ if ($LASTEXITCODE -ne 0 ) {
242+ Throw " An error occurred while restoring NuGet modules."
243+ }
244+
245+ Write-Verbose - Message ($NuGetOutput | Out-String )
246+
136247 Pop-Location
137248}
138249
@@ -141,7 +252,23 @@ if (!(Test-Path $CAKE_EXE)) {
141252 Throw " Could not find Cake.exe at $CAKE_EXE "
142253}
143254
255+ $CAKE_EXE_INVOCATION = if ($IsLinux -or $IsMacOS ) {
256+ " mono `" $CAKE_EXE `" "
257+ } else {
258+ " `" $CAKE_EXE `" "
259+ }
260+
261+ # Build an array (not a string) of Cake arguments to be joined later
262+ $cakeArguments = @ ()
263+ if ($Script ) { $cakeArguments += " `" $Script `" " }
264+ if ($Target ) { $cakeArguments += " -target=`" $Target `" " }
265+ if ($Configuration ) { $cakeArguments += " -configuration=$Configuration " }
266+ if ($Verbosity ) { $cakeArguments += " -verbosity=$Verbosity " }
267+ if ($ShowDescription ) { $cakeArguments += " -showdescription" }
268+ if ($DryRun ) { $cakeArguments += " -dryrun" }
269+ $cakeArguments += $ScriptArgs
270+
144271# Start Cake
145272Write-Host " Running build script..."
146- Invoke-Expression " & `" $CAKE_EXE `" `" $Script `" -target= `" $Target `" -configuration= `" $Configuration `" -verbosity= `" $Verbosity `" -versionnumber= `" $Version `" -versionbuild= `" $Build `" $UseMono $UseDryRun $UseExperimental $ScriptArgs "
273+ Invoke-Expression " & $CAKE_EXE_INVOCATION $ ( $cakeArguments -join " " ) "
147274exit $LASTEXITCODE
0 commit comments