|
- #Requires -Version 5.1
- <#
- .SYNOPSIS
- Build roadamp.exe
-
- .PARAMETER OutDir
- Output directory. Defaults to the project root (where this script lives).
-
- .PARAMETER Version
- Version string embedded via -ldflags. Defaults to the current git tag/commit.
-
- .EXAMPLE
- .\build.ps1
- .\build.ps1 -OutDir C:\Deploy\roadamp
- #>
- param(
- [string]$OutDir = $PSScriptRoot,
- [string]$Version = ""
- )
-
- Set-StrictMode -Version Latest
- $ErrorActionPreference = 'Stop'
-
- Push-Location $PSScriptRoot
- try {
- # Resolve version from git if not supplied
- if (-not $Version) {
- $tag = git describe --tags --always --dirty 2>$null
- $Version = if ($tag) { $tag } else { "dev" }
- }
-
- $out = Join-Path $OutDir "roadamp.exe"
-
- Write-Host "Building roadamp $Version -> $out" -ForegroundColor Cyan
-
- # ── Patch sw.js with build version ────────────────────────────────────────
- # The service worker embeds a cache-bust token so installed PWA clients
- # pick up new assets after each release. We replace __BUILDVER__ in the
- # source file, run go build (which embeds it), then restore via git.
- $swPath = Join-Path $PSScriptRoot 'web\static\sw.js'
- $swOriginal = Get-Content $swPath -Raw
- try {
- $swPatched = $swOriginal -replace '__BUILDVER__', $Version
- [System.IO.File]::WriteAllText($swPath, $swPatched, [System.Text.Encoding]::UTF8)
-
- # ── Compile ───────────────────────────────────────────────────────────
- $env:GOOS = "windows"
- $env:GOARCH = "amd64"
- $env:CGO_ENABLED = "0"
-
- go build `
- -trimpath `
- -ldflags "-s -w -X main.Version=$Version" `
- -o $out `
- ./cmd/roadamp
- }
- finally {
- # Always restore sw.js so the template token is preserved in the repo.
- [System.IO.File]::WriteAllText($swPath, $swOriginal, [System.Text.Encoding]::UTF8)
- }
-
- $size = [math]::Round((Get-Item $out).Length / 1MB, 2)
- Write-Host "Done $out (${size} MB)" -ForegroundColor Green
- }
- finally {
- Pop-Location
- }
|