-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
362 lines (311 loc) · 16.4 KB
/
install.ps1
File metadata and controls
362 lines (311 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env pwsh
# APVM Installer for Windows
# https://github.com/wp-media/automation-plugin-version-manager
#
# Downloads the latest pre-built APVM CLI binary for Windows
# and installs it to ~/.apvm/bin/ (or $env:APVM_INSTALL/bin/).
#
# Usage (PowerShell 5.1+ on Windows 10+):
# irm https://raw.githubusercontent.com/wp-media/automation-plugin-version-manager/develop/install.ps1 | iex
#
# Environment variables:
# APVM_INSTALL — Override the install directory (default: $HOME\.apvm)
#
# Requirements:
# - PowerShell 5.1+ (ships with Windows 10+)
# - Internet connection
#
# Sources:
# - GitHub release download pattern:
# https://docs.github.com/en/repositories/releasing-projects-on-github/linking-to-releases
# - Invoke-RestMethod:
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod
# - Get-FileHash:
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash
# - Registry-based PATH modification (same pattern as Bun/Deno installers):
# https://learn.microsoft.com/en-us/powershell/scripting/samples/working-with-registry-entries
# - SendMessageTimeout for WM_SETTINGCHANGE broadcast:
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessagetimeoutw
$ErrorActionPreference = "Stop"
# ── Configuration ──────────────────────────────────────────────────────────
$Repo = "wp-media/automation-plugin-version-manager"
$BinaryName = "apvm.exe"
$InstallDir = if ($env:APVM_INSTALL) { $env:APVM_INSTALL } else { "$Home\.apvm" }
$BinDir = "$InstallDir\bin"
$BaseUrl = "https://github.com/$Repo/releases/latest/download"
# ── Colors ─────────────────────────────────────────────────────────────────
# Check if the terminal supports ANSI escape codes (Windows 10 1511+ / PowerShell 7+).
# SupportsVirtualTerminal is a virtual property on PSHostUserInterface; custom host
# implementations (ISE, CI runners, third-party tools) may throw NotImplementedException.
# Wrap in try/catch so a misbehaving host does not abort the script before it starts.
# Source: https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.host.pshostuserinterface.supportsvirtualterminal
# https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
$VtSupport = try { $Host.UI.SupportsVirtualTerminal } catch { $false }
$SupportsAnsi = $VtSupport -or [bool]$env:WT_SESSION -or [bool]$env:TERM_PROGRAM
$ESC = [char]27
if ($SupportsAnsi) {
$C_RED = "$ESC[0;31m"
$C_GREEN = "$ESC[0;32m"
$C_YELLOW = "$ESC[0;33m"
$C_BLUE = "$ESC[0;34m"
$C_BOLD = "$ESC[1m"
$C_DIM = "$ESC[2m"
$C_RESET = "$ESC[0m"
} else {
$C_RED = ""; $C_GREEN = ""; $C_YELLOW = ""; $C_BLUE = ""
$C_BOLD = ""; $C_DIM = ""; $C_RESET = ""
}
# ── Logging ────────────────────────────────────────────────────────────────
function Write-Info { param([string]$Msg) Write-Output "${C_BLUE}info${C_RESET} $Msg" }
function Write-Success { param([string]$Msg) Write-Output "${C_GREEN} +${C_RESET} $Msg" }
function Write-Warn { param([string]$Msg) Write-Output "${C_YELLOW}warn${C_RESET} $Msg" }
function Write-Err { param([string]$Msg) Write-Output "${C_RED}error${C_RESET} $Msg" }
# ── Registry-based environment helpers ─────────────────────────────────────
# Modifying the user PATH via the registry ensures persistence across reboots.
# Broadcasting WM_SETTINGCHANGE tells Explorer and other apps to reload env vars.
# Pattern sourced from Bun (https://bun.sh/install.ps1) and pixi installers.
function Publish-Env {
if (-not ("Win32.NativeMethods" -as [Type])) {
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
}
# HWND_BROADCAST = 0xffff, WM_SETTINGCHANGE = 0x1a
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessagetimeoutw
$HWND_BROADCAST = [IntPtr]0xffff
$WM_SETTINGCHANGE = 0x1a
$result = [UIntPtr]::Zero
[Win32.NativeMethods]::SendMessageTimeout(
$HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero,
"Environment", 2, 5000, [ref]$result
) | Out-Null
}
function Write-Env {
param([string]$Key, [string]$Value)
# Write to HKCU:\Environment (user-level, no admin required).
# https://learn.microsoft.com/en-us/powershell/scripting/samples/working-with-registry-entries
$RegKey = Get-Item -Path 'HKCU:'
$EnvKey = $RegKey.OpenSubKey('Environment', $true)
# OpenSubKey returns $null if the key doesn't exist; create it in that case.
# https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.opensubkey
if ($null -eq $EnvKey) {
$EnvKey = $RegKey.CreateSubKey('Environment')
}
if ($null -eq $Value) {
$EnvKey.DeleteValue($Key)
} else {
$Kind = if ($Value.Contains('%')) {
[Microsoft.Win32.RegistryValueKind]::ExpandString
} elseif ($EnvKey.GetValue($Key)) {
$EnvKey.GetValueKind($Key)
} else {
[Microsoft.Win32.RegistryValueKind]::String
}
$EnvKey.SetValue($Key, $Value, $Kind)
}
Publish-Env
}
function Get-Env {
param([string]$Key)
$RegKey = Get-Item -Path 'HKCU:'
$EnvKey = $RegKey.OpenSubKey('Environment')
# OpenSubKey returns $null if the key doesn't exist (see MSDN RegistryKey.OpenSubKey).
if ($null -eq $EnvKey) { return $null }
$EnvKey.GetValue($Key, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
}
# ── Architecture Detection ─────────────────────────────────────────────────
function Get-Architecture {
# Read from the registry — works reliably even under x64 emulation on ARM64.
# Same approach used by Bun's installer.
# https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details
try {
$Arch = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').PROCESSOR_ARCHITECTURE
if ($Arch) { return $Arch }
} catch {
# Registry read failed (restricted environment). Fall back to the process-level
# environment variable. Note: $env:PROCESSOR_ARCHITECTURE may report x86 in
# 32-bit processes on 64-bit Windows (WoW64), but since we only support AMD64
# and ARM64, this fallback is sufficient for our purposes.
}
return $env:PROCESSOR_ARCHITECTURE
}
# ── Main Install Function ─────────────────────────────────────────────────
function Install-Apvm {
Write-Output ""
Write-Output "${C_BOLD} +----------------------------------+${C_RESET}"
Write-Output "${C_BOLD} | APVM Installer (Windows) |${C_RESET}"
Write-Output "${C_BOLD} +----------------------------------+${C_RESET}"
Write-Output "${C_DIM} https://github.com/$Repo${C_RESET}"
Write-Output ""
# ── Step 1: Detect architecture ────────────────────────────────────────
$Arch = Get-Architecture
if ($Arch -ne "AMD64") {
Write-Err "No pre-built binary available for architecture: $Arch"
Write-Err "APVM provides a pre-built Windows binary for x64 (AMD64) only."
if ($Arch -eq "ARM64") {
Write-Err "ARM64 Windows support may be added in a future release."
}
Write-Err ""
Write-Err "If you have the Rust toolchain installed (>= 1.94.1), build from source:"
Write-Err " cargo install --path crates/cli"
Write-Err " https://github.com/$Repo#install-from-source"
exit 1
}
$Artifact = "apvm-win32-x64-msvc.exe"
Write-Info "Detected platform: ${C_BOLD}Windows x64${C_RESET}"
Write-Info "Artifact: $Artifact"
# ── Step 2: Create temp directory ──────────────────────────────────────
$TmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "apvm-install-$([System.Guid]::NewGuid().ToString('N').Substring(0,8))"
New-Item -ItemType Directory -Path $TmpDir -Force | Out-Null
try {
# ── Step 3: Download binary ────────────────────────────────────────
$BinaryUrl = "$BaseUrl/$Artifact"
$BinaryPath = Join-Path $TmpDir $Artifact
Write-Info "Downloading from latest release..."
try {
# Prefer curl.exe if available — faster than PowerShell's Invoke-RestMethod.
# Note: 'curl' in PowerShell is an alias for Invoke-WebRequest;
# 'curl.exe' invokes the real curl shipped with Windows 10 1803+.
# https://curl.se/windows/
$curlExe = Get-Command curl.exe -ErrorAction SilentlyContinue
if ($curlExe) {
& curl.exe --proto '=https' --tlsv1.2 -fsSL $BinaryUrl -o $BinaryPath
if ($LASTEXITCODE -ne 0) { throw "curl.exe failed with exit code $LASTEXITCODE" }
} else {
# Fallback: Invoke-RestMethod (works on all PowerShell 5.1+ systems)
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod
Invoke-RestMethod -Uri $BinaryUrl -OutFile $BinaryPath
}
} catch {
Write-Err "Failed to download: $BinaryUrl"
Write-Err ""
Write-Err "Possible causes:"
Write-Err " - No internet connection"
Write-Err " - No release has been published yet"
Write-Err " - GitHub is experiencing an outage"
Write-Err ""
Write-Err "Check: https://github.com/$Repo/releases/latest"
exit 1
}
if (-not (Test-Path $BinaryPath)) {
Write-Err "Download succeeded but file not found at: $BinaryPath"
Write-Err "An antivirus may have quarantined the file."
exit 1
}
Write-Success "Downloaded binary"
# ── Step 4: Verify checksum ────────────────────────────────────────
$ChecksumsUrl = "$BaseUrl/checksums.txt"
$ChecksumsPath = Join-Path $TmpDir "checksums.txt"
Write-Info "Verifying checksum (SHA-256)..."
$ChecksumOk = $false
try {
if ($curlExe) {
& curl.exe --proto '=https' --tlsv1.2 -fsSL $ChecksumsUrl -o $ChecksumsPath 2>$null
if ($LASTEXITCODE -ne 0) { throw "curl failed" }
} else {
Invoke-RestMethod -Uri $ChecksumsUrl -OutFile $ChecksumsPath
}
if (Test-Path $ChecksumsPath) {
# Parse checksums.txt: each line is "<hash> <filename>"
$ExpectedLine = Get-Content $ChecksumsPath | Where-Object { $_ -match ("\s+" + [regex]::Escape($Artifact) + '$') }
if ($ExpectedLine) {
$ExpectedHash = ($ExpectedLine -split '\s+')[0]
# Get-FileHash: built-in since PowerShell 4.0
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash
$ActualHash = (Get-FileHash -Path $BinaryPath -Algorithm SHA256).Hash.ToLower()
if ($ActualHash -ne $ExpectedHash) {
Write-Err "Checksum verification failed!"
Write-Err " Expected: $ExpectedHash"
Write-Err " Actual: $ActualHash"
Write-Err ""
Write-Err "The downloaded file may be corrupted or tampered with."
exit 1
}
$ChecksumOk = $true
} else {
Write-Warn "Could not find checksum for '$Artifact' in checksums.txt"
}
}
} catch {
Write-Warn "Could not download checksums.txt - skipping verification"
}
if ($ChecksumOk) {
Write-Success "Checksum verified"
}
# ── Step 5: Install binary ─────────────────────────────────────────
# Create install directory
New-Item -ItemType Directory -Path $BinDir -Force | Out-Null
# Remove existing binary if present (allows updates/reinstalls)
$TargetPath = Join-Path $BinDir $BinaryName
if (Test-Path $TargetPath) {
try {
Remove-Item $TargetPath -Force
} catch {
# Accessing Process.Path (= MainModule.FileName) can throw Win32Exception
# "Access is denied" for processes owned by other users or protected by AV/EDR.
# Source: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.mainmodule
$RunningProcs = Get-Process -Name "apvm" -ErrorAction SilentlyContinue |
Where-Object { try { $_.Path -eq $TargetPath } catch { $false } }
if ($RunningProcs.Count -gt 0) {
Write-Err "Cannot replace existing binary — apvm.exe is currently running."
Write-Err "Please close any running apvm processes and try again."
exit 1
}
Write-Err "Failed to remove existing binary at: $TargetPath"
Write-Err $_
exit 1
}
}
Copy-Item $BinaryPath $TargetPath -Force
Write-Success "Installed to $TargetPath"
# ── Step 6: Add to PATH ────────────────────────────────────────────
$PathUpdated = $false
$UserPath = Get-Env -Key "Path"
$PathEntries = if ($UserPath) { $UserPath -split ';' } else { @() }
if ($PathEntries -notcontains $BinDir) {
$PathEntries += $BinDir
Write-Env -Key 'Path' -Value ($PathEntries -join ';')
$env:PATH = "$BinDir;$env:PATH"
$PathUpdated = $true
Write-Success "Added $BinDir to user PATH"
}
# ── Step 7: Verify and print summary ───────────────────────────────
Write-Output ""
$VersionOutput = $null
try {
$VersionOutput = & $TargetPath --version 2>&1
} catch {
# Binary may fail for various reasons (missing DLLs, etc.)
}
if (-not $VersionOutput) {
Write-Err "Installation completed but the binary could not be executed."
Write-Err "This may indicate a platform mismatch or a missing dependency."
Write-Err "Please report an issue: https://github.com/$Repo/issues"
exit 1
}
Write-Output "${C_GREEN}${C_BOLD} +----------------------------------+${C_RESET}"
Write-Output "${C_GREEN}${C_BOLD} | APVM installed successfully! |${C_RESET}"
Write-Output "${C_GREEN}${C_BOLD} +----------------------------------+${C_RESET}"
Write-Output ""
Write-Output " ${C_DIM}Version :${C_RESET} $VersionOutput"
Write-Output " ${C_DIM}Binary :${C_RESET} $TargetPath"
Write-Output ""
if ($PathUpdated) {
Write-Output " ${C_YELLOW}Restart your terminal${C_RESET} to update your PATH, then run:"
} else {
Write-Output " Run:"
}
Write-Output ""
Write-Output " ${C_BOLD}apvm --help${C_RESET}"
Write-Output ""
} finally {
# Clean up temp directory
if (Test-Path $TmpDir) {
Remove-Item $TmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
Install-Apvm