Files
mdrs-client-rust/scripts/build-release-windows.ps1
T
orrisroot e1081aeff4 fix(scripts): keep .env values out of the PowerShell session
A .ps1 runs inside the caller's PowerShell process, so the values the
Windows release script exported from .env outlived the script. A
later run in the same session kept the old GITEA_TOKEN even after .env
was edited, because existing environment variables take precedence.

- Read .env into a script-local table and resolve settings through
  Get-Setting, with environment variables still taking precedence
- Stop changing the caller's current location; build and archive
  with paths anchored at the repository root
- Skip .env lines without "=" instead of failing under StrictMode
2026-11-04 20:15:14 +09:00

114 lines
4.1 KiB
PowerShell

# Build release archive for Windows (x86_64-pc-windows-msvc).
#
# Usage:
# .\scripts\build-release-windows.ps1 [-Version <VERSION>]
#
# If -Version is not provided, it is read from Cargo.toml.
# Set GITEA_TOKEN (and optionally GITEA_SERVER_URL / GITEA_REPOSITORY)
# to upload the archive to a Gitea release automatically.
# These can be provided via scripts\.env.
param(
[string]$Version = ""
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = Split-Path -Parent $ScriptDir
# Load .env if present. The values are kept in a script-local table rather
# than written to the process environment: a .ps1 runs inside the caller's
# PowerShell process, so anything set there would outlive the script and
# shadow later edits to .env in the same session.
$DotEnv = @{}
$EnvFile = Join-Path $ScriptDir ".env"
if (Test-Path $EnvFile) {
Get-Content $EnvFile | ForEach-Object {
if ($_ -match '^\s*#' -or $_ -notmatch '=') { return }
$parts = $_ -split '=', 2
$DotEnv[$parts[0].Trim()] = $parts[1].Trim()
}
}
# Resolve a setting: an environment variable takes precedence over .env
function Get-Setting([string]$Name) {
$value = [System.Environment]::GetEnvironmentVariable($Name)
if ($value) { return $value }
if ($DotEnv.ContainsKey($Name)) { return $DotEnv[$Name] }
return $null
}
# Determine version
if (-not $Version) {
$cargoToml = Get-Content (Join-Path $RepoRoot "Cargo.toml") -Raw
if ($cargoToml -match 'version\s*=\s*"([^"]+)"') {
$Version = $Matches[1]
} else {
Write-Error "Could not determine version from Cargo.toml"
exit 1
}
}
$Tag = "v$Version"
$Target = "x86_64-pc-windows-msvc"
Write-Host "==> Building mdrs $Version for Windows ($Target)"
rustup target add $Target
cargo build --release --target $Target --manifest-path (Join-Path $RepoRoot "Cargo.toml")
$Archive = "mdrs-$Version-$Target.zip"
$ArchivePath = Join-Path $RepoRoot $Archive
Compress-Archive -Force -Path (Join-Path $RepoRoot "target\$Target\release\mdrs.exe") -DestinationPath $ArchivePath
# Written alongside the archive so `mdrs selfupdate` can check what it fetched.
$Checksum = "$Archive.sha256"
$Hash = (Get-FileHash -Algorithm SHA256 -Path $ArchivePath).Hash.ToLower()
"$Hash $Archive" | Set-Content -NoNewline -Encoding ascii (Join-Path $RepoRoot $Checksum)
Write-Host " Created: $Archive"
# Upload to Gitea if token is provided
$GiteaToken = Get-Setting "GITEA_TOKEN"
if (-not $GiteaToken) {
Write-Host ""
Write-Host "GITEA_TOKEN not set -- skipping upload."
Write-Host "Set GITEA_TOKEN (and GITEA_SERVER_URL, GITEA_REPOSITORY) in scripts\.env to enable upload."
exit 0
}
$ServerUrl = Get-Setting "GITEA_SERVER_URL"
$Repository = Get-Setting "GITEA_REPOSITORY"
if (-not $ServerUrl) { Write-Error "GITEA_SERVER_URL is required for upload"; exit 1 }
if (-not $Repository) { Write-Error "GITEA_REPOSITORY is required for upload"; exit 1 }
$Headers = @{ Authorization = "Bearer $GiteaToken"; "Content-Type" = "application/json" }
Write-Host ""
Write-Host "==> Creating Gitea release $Tag ..."
try {
Invoke-RestMethod -Method Post -Uri "$ServerUrl/api/v1/repos/$Repository/releases" `
-Headers $Headers `
-Body (ConvertTo-Json @{ tag_name = $Tag; name = $Tag }) | Out-Null
} catch {
# Release may already exist; continue
}
$Release = Invoke-RestMethod -Method Get -Uri "$ServerUrl/api/v1/repos/$Repository/releases/tags/$Tag" `
-Headers @{ Authorization = "Bearer $GiteaToken" }
$ReleaseId = $Release.id
foreach ($Name in @($Archive, $Checksum)) {
Write-Host "==> Uploading $Name (release id: $ReleaseId) ..."
$AssetPath = Join-Path $RepoRoot $Name
& curl.exe -sf -X POST `
-H "Authorization: Bearer $GiteaToken" `
-F "attachment=@$AssetPath" `
"$ServerUrl/api/v1/repos/$Repository/releases/$ReleaseId/assets" | Out-Null
if ($LASTEXITCODE -ne 0) { Write-Error "Upload failed (exit code $LASTEXITCODE)"; exit 1 }
Write-Host " Done."
}
Write-Host ""
Write-Host "==> Upload complete: $ServerUrl/$Repository/releases/tag/$Tag"