Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

184 rader
5.6 KiB

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