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