build.ps1 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. [CmdletBinding()]
  2. Param(
  3. #[switch]$CustomParam,
  4. [Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
  5. [string[]]$BuildArguments
  6. )
  7. Write-Output "Windows PowerShell $($Host.Version)"
  8. Set-StrictMode -Version 2.0; $ErrorActionPreference = "Stop"; $ConfirmPreference = "None"; trap { exit 1 }
  9. $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
  10. ###########################################################################
  11. # CONFIGURATION
  12. ###########################################################################
  13. $BuildProjectFile = "$PSScriptRoot\nukebuild\_build.csproj"
  14. $TempDirectory = "$PSScriptRoot\\.tmp"
  15. $DotNetGlobalFile = "$PSScriptRoot\\global.json"
  16. $DotNetInstallUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1"
  17. $DotNetChannel = "Current"
  18. $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
  19. $env:DOTNET_CLI_TELEMETRY_OPTOUT = 1
  20. $env:NUGET_XMLDOC_MODE = "skip"
  21. ###########################################################################
  22. # EXECUTION
  23. ###########################################################################
  24. function ExecSafe([scriptblock] $cmd) {
  25. & $cmd
  26. if ($LASTEXITCODE) { exit $LASTEXITCODE }
  27. }
  28. # If global.json exists, load expected version
  29. if (Test-Path $DotNetGlobalFile) {
  30. $DotNetGlobal = $(Get-Content $DotNetGlobalFile | Out-String | ConvertFrom-Json)
  31. if ($DotNetGlobal.PSObject.Properties["sdk"] -and $DotNetGlobal.sdk.PSObject.Properties["version"]) {
  32. $DotNetVersion = $DotNetGlobal.sdk.version
  33. }
  34. }
  35. # If dotnet is installed locally, and expected version is not set or installation matches the expected version
  36. if ($null -ne (Get-Command "dotnet" -ErrorAction SilentlyContinue) -and `
  37. (!(Test-Path variable:DotNetVersion) -or $(& dotnet --version) -eq $DotNetVersion)) {
  38. $env:DOTNET_EXE = (Get-Command "dotnet").Path
  39. }
  40. else {
  41. $DotNetDirectory = "$TempDirectory\dotnet-win"
  42. $env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"
  43. # Download install script
  44. $DotNetInstallFile = "$TempDirectory\dotnet-install.ps1"
  45. mkdir -force $TempDirectory > $null
  46. (New-Object System.Net.WebClient).DownloadFile($DotNetInstallUrl, $DotNetInstallFile)
  47. # Install by channel or version
  48. if (!(Test-Path variable:DotNetVersion)) {
  49. ExecSafe { & $DotNetInstallFile -InstallDir $DotNetDirectory -Channel $DotNetChannel -NoPath }
  50. } else {
  51. ExecSafe { & $DotNetInstallFile -InstallDir $DotNetDirectory -Version $DotNetVersion -NoPath }
  52. }
  53. $env:PATH="$DotNetDirectory;$env:PATH"
  54. }
  55. Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)"
  56. ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile -- $BuildArguments }