|
| 1 | +#!/usr/bin/env powershell |
| 2 | + |
| 3 | +# This powershell script is intended to be "dot-sourced" by other scripts. |
| 4 | +# It's purpose is identical to that of the `lib.sh` script for Linux environments. |
| 5 | + |
| 6 | +# Behave similar to `set -e` in bash, but ONLY for powershell commandlets! |
| 7 | +# For all legacy, program, and script calls use Run-Command() or Check-Exit() |
| 8 | +$ErrorActionPreference = 'Stop' |
| 9 | + |
| 10 | +# Any golang compilation needs to know what it's building for. |
| 11 | +$Env:GOOS = "windows" |
| 12 | +$Env:GOARCH = "amd64" |
| 13 | + |
| 14 | +# Items only relevant in a CI environment. |
| 15 | +if ($Env:CI -eq "true") { |
| 16 | + # Unnecessary and intrusive. They claim parameter/variable |
| 17 | + # values aren't collected, but there could be a bug leading |
| 18 | + # to a concern over leaking of some sensitive-value. Stop this. |
| 19 | + $Env:POWERSHELL_TELEMETRY_OPTOUT = "true" |
| 20 | + |
| 21 | + # Unnecessary and potentially disruptive. Powershell will |
| 22 | + # never ever be updated during automation execution. Stop this. |
| 23 | + $Env:POWERSHELL_UPDATECHECK = "off" |
| 24 | + |
| 25 | + # Color in output may confuse tooling and makes logs hard to read. |
| 26 | + # TODO: There are probably other places where color needs to be disabled |
| 27 | + # in a slightly different way :( |
| 28 | + $Env:NO_COLOR = "true" |
| 29 | + |
| 30 | + # Defined by .cirrus.yml for use by all the linux tasks. |
| 31 | + # Drop all global envs which have unix paths, defaults are fine. |
| 32 | + Remove-Item Env:\GOPATH -ErrorAction:Ignore |
| 33 | + Remove-Item Env:\GOSRC -ErrorAction:Ignore |
| 34 | + Remove-Item Env:\GOCACHE -ErrorAction:Ignore |
| 35 | + |
| 36 | + # Defined by Cirrus-CI |
| 37 | + # Drop large known env variables (an env > 32k will break MSI/ICE validation) |
| 38 | + Remove-Item Env:\CIRRUS_COMMIT_MESSAGE -ErrorAction:Ignore |
| 39 | + Remove-Item Env:\CIRRUS_CHANGE_MESSAGE -ErrorAction:Ignore |
| 40 | + Remove-Item Env:\CIRRUS_PR_BODY -ErrorAction:Ignore |
| 41 | +} |
| 42 | + |
| 43 | +# Non-powershell commands do not halt execution on error! This helper |
| 44 | +# should be called after every critical operation to check and halt on a |
| 45 | +# non-zero exit code. Be careful not to use this for powershell commandlets |
| 46 | +# (builtins)! They set '$?' to "True" (failed) or "False" success so calling |
| 47 | +# this would mask failures. Rely on $ErrorActionPreference = 'Stop' instead. |
| 48 | +function Check-Exit { |
| 49 | + $result = $LASTEXITCODE # WARNING: might not be a number! |
| 50 | + if ( ($result -ne $null) -and ($result -ne 0) ) { |
| 51 | + # https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.callstackframe |
| 52 | + $caller = (Get-PSCallStack)[1] |
| 53 | + Write-Host "Exit code = '$result' from $($caller.ScriptName):$($caller.ScriptLineNumber)" |
| 54 | + Exit $result |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +# Small helper to avoid needing to write 'Check-Exit' after every |
| 59 | +# non-powershell instruction. It simply prints then executes the _QUOTED_ |
| 60 | +# argument followed by Check-Exit. |
| 61 | +# N/B: Escape any nested quotes with back-tick ("`") characters. |
| 62 | +# WARNING: DO NOT use this with powershell builtins! It will not do what you expect! |
| 63 | +function Run-Command { |
| 64 | + param ( |
| 65 | + [string] $command |
| 66 | + ) |
| 67 | + |
| 68 | + Write-Host $command |
| 69 | + |
| 70 | + Invoke-Expression $command |
| 71 | + Check-Exit |
| 72 | +} |
0 commit comments