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