Files
jobboard/deploy/wsl-setup.ps1
T

65 lines
3.0 KiB
PowerShell

<#
.SYNOPSIS
Once per checkout, on Windows: writes this checkout's short commands into
the PowerShell profile. See docs/Install WSL.md.
.DESCRIPTION
Reads DISTRO and CHECKOUT from deploy\release.env beside this script. The
prefix is the checkout folder's name (news-, boilerplate-), or -Name. Writes
one marked block into $PROFILE and replaces it when run again.
#>
param([string] $Name)
$ErrorActionPreference = 'Stop'
$envFile = Join-Path $PSScriptRoot 'release.env'
if (-not (Test-Path $envFile)) { throw "no $envFile - copy release.env.example and fill it in" }
$settings = @{}
foreach ($line in Get-Content $envFile) {
if ($line -match '^\s*(#|$)') { continue }
$key, $value = $line -split '=', 2
$settings[$key.Trim()] = "$value".Trim().Trim('"')
}
$distro = $settings.DISTRO
$checkout = "$($settings.CHECKOUT)".TrimEnd('/')
if (-not $distro -or -not $checkout) { throw "set DISTRO and CHECKOUT in $envFile" }
if (-not $Name) { $Name = Split-Path -Leaf (Split-Path -Parent $PSScriptRoot) }
# Replaces the block between the two marker lines, or appends it.
function Set-ProfileBlock([string] $Key, [string] $Body) {
$path = $PROFILE.CurrentUserCurrentHost
$begin = "# >>> $Key >>>"
$end = "# <<< $Key <<<"
$block = "$begin`r`n$Body`r`n$end"
$text = if (Test-Path $path) { [IO.File]::ReadAllText($path) } else { '' }
$pattern = '(?s)' + [regex]::Escape($begin) + '.*?' + [regex]::Escape($end)
if ($text -match $pattern) {
$text = [regex]::Replace($text, $pattern, { $block })
} else {
$text = $text.TrimEnd() + "`r`n`r`n$block`r`n"
}
New-Item -ItemType Directory -Force -Path (Split-Path $path) | Out-Null
[IO.File]::WriteAllText($path, $text)
Write-Host "profile: wrote $Key to $path"
}
$unc = "\\wsl.localhost\$distro" + ($checkout -replace '/', '\')
$wsl = "wsl.exe -d $distro --cd $checkout"
$ps = "& (Get-Process -Id `$PID).Path -NoProfile -ExecutionPolicy Bypass -File"
$lines = @(
"function $Name-cd { Set-Location '$unc' }"
"function $Name-sh { $wsl }"
"function $Name-backend { $wsl -- bash bin/backend.sh @args }"
# roledesk's additions: the PyCharm run configurations in .run/, from PowerShell.
"function $Name-pull { $wsl -- bash bin/pull.sh @args }"
"function $Name-worker { $wsl -- bash bin/worker.sh @args }"
"function $Name-check { $wsl -- bash bin/check.sh @args }"
"function $Name-1test { $wsl -- bash deploy/1-test-and-promote.sh @args }"
"function $Name-2build { $ps '$unc\deploy\2-build-image.ps1' }"
"function $Name-3push { $ps '$unc\deploy\3-push-image.ps1' }"
"function $Name-0release { $Name-1test @args; if (`$LASTEXITCODE -ne 0) { return }; $Name-2build; if (`$LASTEXITCODE -ne 0) { return }; $Name-3push }"
)
if (Test-Path (Join-Path $PSScriptRoot 'db.sh')) {
$lines += "function $Name-db { $wsl -- bash deploy/db.sh @args }"
}
Set-ProfileBlock "$Name (deploy/wsl-setup.ps1)" ($lines -join "`r`n")
Write-Host 'Open a new PowerShell window to load the profile.'