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