build.ps1 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. .SYNOPSIS
  8. This is a Powershell script to bootstrap a Cake build.
  9. .DESCRIPTION
  10. This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
  11. and execute your Cake build script with the parameters you provide.
  12. .PARAMETER Script
  13. The build script to execute.
  14. .PARAMETER Target
  15. The build script target to run.
  16. .PARAMETER Configuration
  17. The build configuration to use.
  18. .PARAMETER Verbosity
  19. Specifies the amount of information to be displayed.
  20. .PARAMETER ShowDescription
  21. Shows description about tasks.
  22. .PARAMETER DryRun
  23. Performs a dry run.
  24. .PARAMETER SkipToolPackageRestore
  25. Skips restoring of packages.
  26. .PARAMETER ScriptArgs
  27. Remaining arguments are added here.
  28. .LINK
  29. https://cakebuild.net
  30. #>
  31. [CmdletBinding()]
  32. Param(
  33. [string]$Script = "build.cake",
  34. [string]$Target,
  35. [string]$Configuration,
  36. [ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
  37. [string]$Verbosity,
  38. [switch]$ShowDescription,
  39. [Alias("WhatIf", "Noop")]
  40. [switch]$DryRun,
  41. [switch]$SkipToolPackageRestore,
  42. [Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
  43. [string[]]$ScriptArgs
  44. )
  45. # Attempt to set highest encryption available for SecurityProtocol.
  46. # PowerShell will not set this by default (until maybe .NET 4.6.x). This
  47. # will typically produce a message for PowerShell v2 (just an info
  48. # message though)
  49. try {
  50. # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
  51. # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
  52. # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
  53. # installed (.NET 4.5 is an in-place upgrade).
  54. # PowerShell Core already has support for TLS 1.2 so we can skip this if running in that.
  55. if (-not $IsCoreCLR) {
  56. [System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
  57. }
  58. } catch {
  59. 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'
  60. }
  61. [Reflection.Assembly]::LoadWithPartialName("System.Security") | Out-Null
  62. function MD5HashFile([string] $filePath)
  63. {
  64. if ([string]::IsNullOrEmpty($filePath) -or !(Test-Path $filePath -PathType Leaf))
  65. {
  66. return $null
  67. }
  68. [System.IO.Stream] $file = $null;
  69. [System.Security.Cryptography.MD5] $md5 = $null;
  70. try
  71. {
  72. $md5 = [System.Security.Cryptography.MD5]::Create()
  73. $file = [System.IO.File]::OpenRead($filePath)
  74. return [System.BitConverter]::ToString($md5.ComputeHash($file))
  75. }
  76. finally
  77. {
  78. if ($file -ne $null)
  79. {
  80. $file.Dispose()
  81. }
  82. }
  83. }
  84. function GetProxyEnabledWebClient
  85. {
  86. $wc = New-Object System.Net.WebClient
  87. $proxy = [System.Net.WebRequest]::GetSystemWebProxy()
  88. $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
  89. $wc.Proxy = $proxy
  90. return $wc
  91. }
  92. Write-Host "Preparing to run build script..."
  93. if(!$PSScriptRoot){
  94. $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
  95. }
  96. $TOOLS_DIR = Join-Path $PSScriptRoot "tools"
  97. $ADDINS_DIR = Join-Path $TOOLS_DIR "Addins"
  98. $MODULES_DIR = Join-Path $TOOLS_DIR "Modules"
  99. $NUGET_EXE = Join-Path $TOOLS_DIR "nuget.exe"
  100. $CAKE_EXE = Join-Path $TOOLS_DIR "Cake/Cake.exe"
  101. $NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
  102. $PACKAGES_CONFIG = Join-Path $TOOLS_DIR "packages.config"
  103. $PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR "packages.config.md5sum"
  104. $ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR "packages.config"
  105. $MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR "packages.config"
  106. # Make sure tools folder exists
  107. if ((Test-Path $PSScriptRoot) -and !(Test-Path $TOOLS_DIR)) {
  108. Write-Verbose -Message "Creating tools directory..."
  109. New-Item -Path $TOOLS_DIR -Type Directory | Out-Null
  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)
  117. } catch {
  118. Throw "Could not download packages.config."
  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. # Try download NuGet.exe if not exists
  132. if (!(Test-Path $NUGET_EXE)) {
  133. Write-Verbose -Message "Downloading NuGet.exe..."
  134. try {
  135. $wc = GetProxyEnabledWebClient
  136. $wc.DownloadFile($NUGET_URL, $NUGET_EXE)
  137. } catch {
  138. Throw "Could not download NuGet.exe."
  139. }
  140. }
  141. # Save nuget.exe path to environment to be available to child processed
  142. $env:NUGET_EXE = $NUGET_EXE
  143. $env:NUGET_EXE_INVOCATION = if ($IsLinux -or $IsMacOS) {
  144. "mono `"$NUGET_EXE`""
  145. } else {
  146. "`"$NUGET_EXE`""
  147. }
  148. # Restore tools from NuGet?
  149. if(-Not $SkipToolPackageRestore.IsPresent) {
  150. Push-Location
  151. Set-Location $TOOLS_DIR
  152. # Check for changes in packages.config and remove installed tools if true.
  153. [string] $md5Hash = MD5HashFile $PACKAGES_CONFIG
  154. if((!(Test-Path $PACKAGES_CONFIG_MD5)) -Or
  155. ($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
  156. Write-Verbose -Message "Missing or changed package.config hash..."
  157. Get-ChildItem -Exclude packages.config,nuget.exe,Cake.Bakery |
  158. Remove-Item -Recurse -Force
  159. }
  160. Write-Verbose -Message "Restoring tools from NuGet..."
  161. $NuGetOutput = Invoke-Expression "& $env:NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `"$TOOLS_DIR`""
  162. if ($LASTEXITCODE -ne 0) {
  163. Throw "An error occurred 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. Pop-Location
  171. }
  172. # Restore addins from NuGet
  173. if (Test-Path $ADDINS_PACKAGES_CONFIG) {
  174. Push-Location
  175. Set-Location $ADDINS_DIR
  176. Write-Verbose -Message "Restoring addins from NuGet..."
  177. $NuGetOutput = Invoke-Expression "& $env:NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `"$ADDINS_DIR`""
  178. if ($LASTEXITCODE -ne 0) {
  179. Throw "An error occurred while restoring NuGet addins."
  180. }
  181. Write-Verbose -Message ($NuGetOutput | Out-String)
  182. Pop-Location
  183. }
  184. # Restore modules from NuGet
  185. if (Test-Path $MODULES_PACKAGES_CONFIG) {
  186. Push-Location
  187. Set-Location $MODULES_DIR
  188. Write-Verbose -Message "Restoring modules from NuGet..."
  189. $NuGetOutput = Invoke-Expression "& $env:NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `"$MODULES_DIR`""
  190. if ($LASTEXITCODE -ne 0) {
  191. Throw "An error occurred while restoring NuGet modules."
  192. }
  193. Write-Verbose -Message ($NuGetOutput | Out-String)
  194. Pop-Location
  195. }
  196. # Make sure that Cake has been installed.
  197. if (!(Test-Path $CAKE_EXE)) {
  198. Throw "Could not find Cake.exe at $CAKE_EXE"
  199. }
  200. $CAKE_EXE_INVOCATION = if ($IsLinux -or $IsMacOS) {
  201. "mono `"$CAKE_EXE`""
  202. } else {
  203. "`"$CAKE_EXE`""
  204. }
  205. # Build Cake arguments
  206. $cakeArguments = ""
  207. If ($Script) {
  208. $cakeArguments += @("`"$Script`"");
  209. }
  210. if ($Target) { $cakeArguments += "-target=`"$Target`"" }
  211. if ($Configuration) { $cakeArguments += "-configuration=$Configuration" }
  212. if ($Verbosity) { $cakeArguments += "-verbosity=$Verbosity" }
  213. if ($ShowDescription) { $cakeArguments += "-showdescription" }
  214. if ($DryRun) { $cakeArguments += "-dryrun" }
  215. $cakeArguments += $ScriptArgs
  216. # Start Cake
  217. Write-Host "Running build script..."
  218. Invoke-Expression "& $CAKE_EXE_INVOCATION $($cakeArguments -join " ")"
  219. exit $LASTEXITCODE