fix(installer): detect Windows architecture without RuntimeInformation

This commit is contained in:
drakezhang
2026-09-09 18:10:57 +08:00
parent 0fe36bc0b1
commit ac0525ffee
2 changed files with 55 additions and 5 deletions
+10 -4
View File
@@ -3,7 +3,7 @@
install.ps1 — install the bsk CLI on Windows from GitHub Releases.
.DESCRIPTION
Downloads the latest (or pinned) bsk release for Windows x64,
Downloads the latest (or pinned) bsk release for Windows x64 or ARM64,
extracts bsk.exe to a user-local directory, and adds it to PATH.
Usage:
@@ -38,11 +38,17 @@ function Write-Die {
# ── Platform / architecture detection ─────────────────────────────────────────
function Get-PlatformTriple {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture
# RuntimeInformation.ProcessArchitecture can be unavailable in Windows PowerShell 5.1.
# WOW64 exposes the native architecture separately from the 32-bit process.
$arch = $env:PROCESSOR_ARCHITEW6432
if (-not $arch) { $arch = $env:PROCESSOR_ARCHITECTURE }
if (-not $arch) {
Write-Die "could not detect Windows architecture: PROCESSOR_ARCHITEW6432 and PROCESSOR_ARCHITECTURE are empty"
}
switch ($arch) {
"X64" { $archId = "x64" }
"Arm64" { $archId = "arm64" }
"AMD64" { $archId = "x64" }
"ARM64" { $archId = "arm64" }
default { Write-Die "unsupported architecture: $arch (x64 and ARM64 only)" }
}
+45 -1
View File
@@ -1,5 +1,8 @@
#Requires -Version 5.1
param([string]$BskPath = (Join-Path $PSScriptRoot "../target/debug/bsk.exe"))
param(
[string]$BskPath = (Join-Path $PSScriptRoot "../target/debug/bsk.exe"),
[switch]$ArchitectureOnly
)
$ErrorActionPreference = "Stop"
# Load definitions, never Main: no downloads or changes to the real user PATH.
@@ -27,6 +30,47 @@ function Assert-Fails([scriptblock]$Action) {
if (-not $failed) { throw "expected failure" }
}
# Isolate the fatal-error stub and restore the real process environment.
& {
function Write-Die([string]$Message) { throw $Message }
$oldProcessArch = $env:PROCESSOR_ARCHITECTURE
$oldNativeArch = $env:PROCESSOR_ARCHITEW6432
try {
$cases = @(
@{ Process = 'AMD64'; Native = $null; Arch = 'x64'; Triple = 'x86_64-pc-windows-msvc' }
@{ Process = 'ARM64'; Native = $null; Arch = 'arm64'; Triple = 'aarch64-pc-windows-msvc' }
@{ Process = 'amd64'; Native = $null; Arch = 'x64'; Triple = 'x86_64-pc-windows-msvc' }
@{ Process = 'x86'; Native = 'AMD64'; Arch = 'x64'; Triple = 'x86_64-pc-windows-msvc' }
@{ Process = 'x86'; Native = 'ARM64'; Arch = 'arm64'; Triple = 'aarch64-pc-windows-msvc' }
@{ Process = $null; Native = 'AMD64'; Arch = 'x64'; Triple = 'x86_64-pc-windows-msvc' }
@{ Process = 'x86'; Native = $null; Error = 'unsupported architecture: x86 (x64 and ARM64 only)' }
@{ Process = 'AMD64'; Native = 'IA64'; Error = 'unsupported architecture: IA64 (x64 and ARM64 only)' }
@{ Process = $null; Native = $null; Error = 'could not detect Windows architecture: PROCESSOR_ARCHITEW6432 and PROCESSOR_ARCHITECTURE are empty' }
)
foreach ($case in $cases) {
$env:PROCESSOR_ARCHITECTURE = $case.Process
$env:PROCESSOR_ARCHITEW6432 = $case.Native
if ($case.Error) {
$message = $null
try { Get-PlatformTriple | Out-Null } catch { $message = $_.Exception.Message }
Assert-Equal $message $case.Error
}
else {
$platform = Get-PlatformTriple
Assert-Equal $platform.ArchId $case.Arch
Assert-Equal $platform.TargetTriple $case.Triple
Assert-Equal $platform.PlatformKey "windows-$($case.Arch)"
}
}
Write-Host "Windows installer architecture regressions passed ($($PSVersionTable.PSVersion))"
}
finally {
$env:PROCESSOR_ARCHITECTURE = $oldProcessArch
$env:PROCESSOR_ARCHITEW6432 = $oldNativeArch
}
}
if ($ArchitectureOnly) { return }
$dir = 'C:\Users\Alice\.local\bin'
foreach ($existing in @('', 'C:\WindowsApps', 'C:\A;C:\B')) {
$script:UserPath = $existing