BlueGit

timfa posprint

Files

posprint / scripts / build-windows.ps1

build-windows.ps1

build-windows.ps1 2.9 KiB Download Raw
1#Requires -Version 5.1
2param(
3 [switch]$SkipTests
4)
5
6$ErrorActionPreference = "Stop"
7$Root = Split-Path -Parent $PSScriptRoot
8Set-Location $Root
9
10function Test-IsInteractive {
11 try {
12 return [Environment]::UserInteractive -and -not [Console]::IsInputRedirected
13 } catch {
14 return $Host.Name -eq "ConsoleHost"
15 }
16}
17
18function Ask-YesNo([string]$Prompt) {
19 if (-not (Test-IsInteractive)) {
20 return $false
21 }
22 $answer = Read-Host "$Prompt [y/N]"
23 return $answer -match '^[Yy]([Ee][Ss])?$'
24}
25
26function Ensure-Rust {
27 if ((Get-Command cargo -ErrorAction SilentlyContinue) -and (Get-Command rustc -ErrorAction SilentlyContinue)) {
28 return
29 }
30
31 $cargoEnv = Join-Path $env:USERPROFILE ".cargo\env"
32 if (Test-Path $cargoEnv) {
33 # Best-effort: put cargo on PATH for this session if rustup is installed.
34 $cargoBin = Join-Path $env:USERPROFILE ".cargo\bin"
35 if ((Test-Path $cargoBin) -and ($env:PATH -notlike "*$cargoBin*")) {
36 $env:PATH = "$cargoBin;$env:PATH"
37 }
38 }
39
40 if ((Get-Command cargo -ErrorAction SilentlyContinue) -and (Get-Command rustc -ErrorAction SilentlyContinue)) {
41 return
42 }
43
44 Write-Host "cargo/rustc not found."
45
46 $winget = Get-Command winget -ErrorAction SilentlyContinue
47 if ($winget -and (Ask-YesNo "Install Rust via winget (Rustlang.Rustup) now?")) {
48 & winget install --id Rustlang.Rustup -e --accept-package-agreements --accept-source-agreements
49 $cargoBin = Join-Path $env:USERPROFILE ".cargo\bin"
50 if ((Test-Path $cargoBin) -and ($env:PATH -notlike "*$cargoBin*")) {
51 $env:PATH = "$cargoBin;$env:PATH"
52 }
53 } elseif (Ask-YesNo "Download and run rustup-init.exe now?") {
54 $tmp = Join-Path $env:TEMP "rustup-init.exe"
55 Invoke-WebRequest -Uri "https://win.rustup.rs/x86_64" -OutFile $tmp
56 & $tmp -y
57 $cargoBin = Join-Path $env:USERPROFILE ".cargo\bin"
58 if ((Test-Path $cargoBin) -and ($env:PATH -notlike "*$cargoBin*")) {
59 $env:PATH = "$cargoBin;$env:PATH"
60 }
61 } else {
62 Write-Error "Install Rust from https://rustup.rs (or: winget install Rustlang.Rustup), then re-run this script."
63 }
64
65 if (-not (Get-Command cargo -ErrorAction SilentlyContinue) -or -not (Get-Command rustc -ErrorAction SilentlyContinue)) {
66 Write-Error "Rust install finished but cargo/rustc are still not on PATH. Open a new PowerShell window and re-run."
67 }
68}
69
70Ensure-Rust
71
72Write-Host "Building posprint for Windows (x64)..."
73
74if (-not $SkipTests) {
75 cargo test
76 if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
77}
78
79cargo build --release
80if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
81
82$exe = Join-Path $Root "target\release\posprint.exe"
83if (-not (Test-Path $exe)) {
84 Write-Error "Build failed: $exe not found"
85}
86
87Write-Host "OK: $exe"
88Get-Item $exe | Select-Object FullName, Length, LastWriteTime