build.ps1 6.3 KB

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