build.ps1 2.6 KB

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