<# .SYNOPSIS Shared plumbing for the release scripts. Dot-source it; do not run it. .DESCRIPTION Dot-sourcing runs in the caller's scope, so everything defined here -- $root, $settings, Setting, Assert-Ok, Write-Step, Remove-ReleaseTemp -- is the caller's afterwards. One copy of the release.env reader, because three copies is three places for it to drift. #> Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' #: The repository root, worked out from this file rather than from the working #: directory, so the scripts can be run from anywhere. $root = Split-Path -Parent $PSScriptRoot #: A checkout inside WSL, run from Windows as \\wsl.localhost\\... Its #: .venv is a Linux one, so the steps that run Python hand that part to the #: distro. $Wsl.Groups[1] is the distro; ConvertTo-LinuxPath turns a path under #: the checkout into the distro's own. See docs/wsl.md. $WslPattern = '^\\\\wsl(?:\.localhost|\$)\\([^\\]+)(\\.*)$' $Wsl = [regex]::Match($root, $WslPattern) function ConvertTo-LinuxPath([string] $Path) { [regex]::Match($Path, $WslPattern).Groups[2].Value -replace '\\', '/' } function Assert-Ok([string] $What) { if ($LASTEXITCODE -ne 0) { throw "$What failed (exit $LASTEXITCODE)" } } function Write-Step([string] $Text) { Write-Host '' Write-Host "== $Text" -ForegroundColor Cyan } $envFile = Join-Path $PSScriptRoot 'release.env' if (-not (Test-Path $envFile)) { throw "no deploy\release.env - copy deploy\release.env.example and fill it in" } $settings = @{} foreach ($line in Get-Content $envFile) { if ($line -match '^\s*(#|$)') { continue } # A line with no `=` is a typo, not a setting. Say so here rather than # letting it surface later as a null reference three functions away. if ($line -notmatch '=') { throw "release.env: not a KEY=VALUE line: $line" } $key, $value = $line -split '=', 2 $settings[$key.Trim()] = $value.Trim().Trim('"') } function Setting([string] $Name, [string] $Default, [switch] $Required) { if ($settings.ContainsKey($Name) -and $settings[$Name]) { return $settings[$Name] } if ($Required) { throw "set $Name in deploy\release.env" } return $Default } #: What temporary directories this release is allowed to delete. Everything #: below is created by these scripts and named for it. $Name = Setting NAME -Required # Only ever deletes a -* entry directly under the temp directory. A # guard, not a convenience: these paths are built by string concatenation and # handed to `Remove-Item -Recurse -Force`, and one empty variable in that # sentence is a very bad afternoon. function Remove-ReleaseTemp([string[]] $Paths) { $tempRoot = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()).TrimEnd('\') + '\' foreach ($path in $Paths) { $resolved = [IO.Path]::GetFullPath($path) if (-not $resolved.StartsWith($tempRoot, [StringComparison]::OrdinalIgnoreCase) -or -not ([IO.Path]::GetFileName($resolved)).StartsWith("$Name-")) { throw "Unsafe release temporary path: $resolved" } if (Test-Path -LiteralPath $resolved) { Remove-Item -LiteralPath $resolved -Recurse -Force } } }