Krolyxon
Commit 6efefdd

Branches

Tags

Refs →

bench51install.ps1

112 lines · 4.5 KB

1# Installs bench51 (the bench51-gui desktop app and the bench51 terminal app)
2# from the GitHub releases, on Windows.
3#
4# irm https://raw.githubusercontent.com/krolyxon/bench51/master/install.ps1 | iex
5#
6# With options:
7#
8# & ([scriptblock]::Create((irm https://raw.githubusercontent.com/krolyxon/bench51/master/install.ps1))) -Version v0.2.0
9#
10# Options: -Version <tag> (default latest), -InstallDir <path> (default
11# %LOCALAPPDATA%\Programs\bench51), -Archive <bench51-<target>.zip>,
12# -NoShortcut, -Uninstall. BENCH51_VERSION and BENCH51_INSTALL_DIR set the
13# defaults for -Version and -InstallDir when piping into iex.
14
15param(
16 [string]$Version = $(if ($env:BENCH51_VERSION) { $env:BENCH51_VERSION } else { 'latest' }),
17 [string]$InstallDir = $(if ($env:BENCH51_INSTALL_DIR) { $env:BENCH51_INSTALL_DIR } else { Join-Path $env:LOCALAPPDATA 'Programs\bench51' }),
18 [string]$Archive,
19 [switch]$NoShortcut,
20 [switch]$Uninstall
21)
22
23$ErrorActionPreference = 'Stop'
24# Invoke-WebRequest is much slower with the progress bar on.
25$ProgressPreference = 'SilentlyContinue'
26[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
27
28$Repo = 'krolyxon/bench51'
29$Asset = 'bench51-x86_64-pc-windows-msvc.zip'
30$Shortcut = Join-Path ([Environment]::GetFolderPath('Programs')) 'bench51.lnk'
31
32function Write-Step([string]$Message) {
33 Write-Host "bench51: $Message"
34}
35
36# Adds or removes $Dir in the user's PATH.
37function Edit-UserPath([string]$Dir, [bool]$Add) {
38 $current = [Environment]::GetEnvironmentVariable('Path', 'User')
39 $parts = @(($current -split ';') | Where-Object { $_ -and ($_.TrimEnd('\') -ne $Dir.TrimEnd('\')) })
40 if ($Add) {
41 $parts += $Dir
42 }
43 [Environment]::SetEnvironmentVariable('Path', ($parts -join ';'), 'User')
44}
45
46if ($Uninstall) {
47 Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $InstallDir
48 Remove-Item -Force -ErrorAction SilentlyContinue $Shortcut
49 Edit-UserPath $InstallDir $false
50 Write-Step "removed bench51 from $InstallDir"
51 return
52}
53
54$Temp = Join-Path ([IO.Path]::GetTempPath()) ("bench51-" + [guid]::NewGuid())
55New-Item -ItemType Directory -Path $Temp | Out-Null
56try {
57 if (-not $Archive) {
58 if ($Version -eq 'latest') {
59 $Base = "https://github.com/$Repo/releases/latest/download"
60 } else {
61 if (-not $Version.StartsWith('v')) {
62 $Version = "v$Version"
63 }
64 $Base = "https://github.com/$Repo/releases/download/$Version"
65 }
66 Write-Step "downloading $Asset ($Version)"
67 $Archive = Join-Path $Temp $Asset
68 $Sums = Join-Path $Temp 'SHA256SUMS'
69 Invoke-WebRequest -UseBasicParsing -Uri "$Base/$Asset" -OutFile $Archive
70 Invoke-WebRequest -UseBasicParsing -Uri "$Base/SHA256SUMS" -OutFile $Sums
71 $line = Get-Content $Sums | Where-Object { $_ -match ('\s\*?' + [regex]::Escape($Asset) + '$') } | Select-Object -First 1
72 if (-not $line) {
73 throw "$Asset is not listed in SHA256SUMS"
74 }
75 $expected = ($line -split '\s+')[0].ToLowerInvariant()
76 $actual = (Get-FileHash -Algorithm SHA256 -Path $Archive).Hash.ToLowerInvariant()
77 if ($actual -ne $expected) {
78 throw "checksum mismatch for $Asset"
79 }
80 }
81 if (-not (Test-Path $Archive)) {
82 throw "no such archive: $Archive"
83 }
84
85 $Extracted = Join-Path $Temp 'x'
86 Expand-Archive -Path $Archive -DestinationPath $Extracted -Force
87 $gui = Get-ChildItem -Path $Extracted -Recurse -Filter 'bench51-gui.exe' | Select-Object -First 1
88 if (-not $gui) {
89 throw 'the archive has no bench51-gui.exe'
90 }
91
92 New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
93 Copy-Item -Path (Join-Path $gui.DirectoryName '*') -Destination $InstallDir -Recurse -Force
94 Edit-UserPath $InstallDir $true
95 $env:Path = "$env:Path;$InstallDir"
96 Write-Step "installed bench51 and bench51-gui to $InstallDir"
97
98 if (-not $NoShortcut) {
99 $shell = New-Object -ComObject WScript.Shell
100 $link = $shell.CreateShortcut($Shortcut)
101 $link.TargetPath = Join-Path $InstallDir 'bench51-gui.exe'
102 $link.WorkingDirectory = $env:USERPROFILE
103 $link.Description = 'Intel 8051 assembler, simulator and debugger'
104 $link.Save()
105 Write-Step 'added bench51 to the Start menu'
106 }
107
108 & (Join-Path $InstallDir 'bench51.exe') --version
109 Write-Step 'open a new terminal to use bench51 and bench51-gui from PATH'
110} finally {
111 Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $Temp
112}