build.ps1 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. [CmdletBinding()]
  2. Param(
  3. [Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
  4. [string[]]$BuildArguments
  5. )
  6. Write-Output "PowerShell $($PSVersionTable.PSEdition) version $($PSVersionTable.PSVersion)"
  7. Set-StrictMode -Version 2.0; $ErrorActionPreference = "Stop"; $ConfirmPreference = "None"; trap { Write-Error $_ -ErrorAction Continue; exit 1 }
  8. $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
  9. ###########################################################################
  10. # CONFIGURATION
  11. ###########################################################################
  12. $BuildProjectFile = "$PSScriptRoot\build\_build.csproj"
  13. $TempDirectory = "$PSScriptRoot\\.nuke\temp"
  14. $DotNetGlobalFile = "$PSScriptRoot\\global.json"
  15. $DotNetInstallUrl = "https://dot.net/v1/dotnet-install.ps1"
  16. $DotNetChannel = "Current"
  17. $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
  18. $env:DOTNET_CLI_TELEMETRY_OPTOUT = 1
  19. $env:DOTNET_MULTILEVEL_LOOKUP = 0
  20. ###########################################################################
  21. # EXECUTION
  22. ###########################################################################
  23. function ExecSafe([scriptblock] $cmd) {
  24. & $cmd
  25. if ($LASTEXITCODE) { exit $LASTEXITCODE }
  26. }
  27. # If dotnet CLI is installed globally and it matches requested version, use for execution
  28. if ($null -ne (Get-Command "dotnet" -ErrorAction SilentlyContinue) -and `
  29. $(dotnet --version) -and $LASTEXITCODE -eq 0) {
  30. $env:DOTNET_EXE = (Get-Command "dotnet").Path
  31. }
  32. else {
  33. # Download install script
  34. $DotNetInstallFile = "$TempDirectory\dotnet-install.ps1"
  35. New-Item -ItemType Directory -Path $TempDirectory -Force | Out-Null
  36. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  37. (New-Object System.Net.WebClient).DownloadFile($DotNetInstallUrl, $DotNetInstallFile)
  38. # If global.json exists, load expected version
  39. if (Test-Path $DotNetGlobalFile) {
  40. $DotNetGlobal = $(Get-Content $DotNetGlobalFile | Out-String | ConvertFrom-Json)
  41. if ($DotNetGlobal.PSObject.Properties["sdk"] -and $DotNetGlobal.sdk.PSObject.Properties["version"]) {
  42. $DotNetVersion = $DotNetGlobal.sdk.version
  43. }
  44. }
  45. # Install by channel or version
  46. $DotNetDirectory = "$TempDirectory\dotnet-win"
  47. if (!(Test-Path variable:DotNetVersion)) {
  48. ExecSafe { & $DotNetInstallFile -InstallDir $DotNetDirectory -Channel $DotNetChannel -NoPath }
  49. } else {
  50. ExecSafe { & $DotNetInstallFile -InstallDir $DotNetDirectory -Version $DotNetVersion -NoPath }
  51. }
  52. $env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"
  53. }
  54. Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)"
  55. ExecSafe { & $env:DOTNET_EXE build $BuildProjectFile /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet }
  56. ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile --no-build -- $BuildArguments }