| 1 | #Requires -Version 5.1 |
| 2 | param( |
| 3 | [switch]$SkipTests |
| 4 | ) |
| 5 | |
| 6 | $ErrorActionPreference = "Stop" |
| 7 | $Root = Split-Path -Parent $PSScriptRoot |
| 8 | Set-Location $Root |
| 9 | |
| 10 | function Test-IsInteractive { |
| 11 | try { |
| 12 | return [Environment]::UserInteractive -and -not [Console]::IsInputRedirected |
| 13 | } catch { |
| 14 | return $Host.Name -eq "ConsoleHost" |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | function 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 | |
| 26 | function 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 | |
| 70 | Ensure-Rust |
| 71 | |
| 72 | Write-Host "Building posprint for Windows (x64)..." |
| 73 | |
| 74 | if (-not $SkipTests) { |
| 75 | cargo test |
| 76 | if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } |
| 77 | } |
| 78 | |
| 79 | cargo build --release |
| 80 | if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } |
| 81 | |
| 82 | $exe = Join-Path $Root "target\release\posprint.exe" |
| 83 | if (-not (Test-Path $exe)) { |
| 84 | Write-Error "Build failed: $exe not found" |
| 85 | } |
| 86 | |
| 87 | Write-Host "OK: $exe" |
| 88 | Get-Item $exe | Select-Object FullName, Length, LastWriteTime |