Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
222 lines (188 loc) · 8.41 KB
/
Copy pathinstall.ps1
File metadata and controls
222 lines (188 loc) · 8.41 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
<#
.SYNOPSIS
termd installer for Windows.
.DESCRIPTION
Downloads the latest termd release, verifies it against the published
SHA-256 checksums, and installs it to a per-user location that needs no
administrator rights.
.EXAMPLE
irm https://raw.githubusercontent.com/PatchMon/termd/main/install.ps1 | iex
.EXAMPLE
# Pinning a version or changing the location, when running the script directly:
.\install.ps1 -Version v1.2.0 -InstallDir "D:\tools\termd"
.NOTES
When piped into iex, parameters cannot be passed, so the equivalent
environment variables are honoured:
TERMD_INSTALL_DIR install path
TERMD_VERSION release tag to install (default: latest)
TERMD_GITHUB_REPO owner/repo to install from
#>
[CmdletBinding()]
param(
[string]$InstallDir = $env:TERMD_INSTALL_DIR,
[string]$Version = $env:TERMD_VERSION,
[string]$Repo = $env:TERMD_GITHUB_REPO
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Refuse before computing any Windows-only path. LOCALAPPDATA does not exist
# elsewhere, and letting it through produces a null parameter-binding error
# instead of a message that says what is actually wrong.
#
# The platform test works on both Windows PowerShell 5.1 and PowerShell Core;
# the automatic $IsWindows variable exists only on the latter.
if ([Environment]::OSVersion.Platform -ne 'Win32NT') {
Write-Host ""
Write-Host " X This installer is for Windows." -ForegroundColor Red
Write-Host " On Linux, macOS or FreeBSD use install.sh:" -ForegroundColor Red
Write-Host " curl -fsSL https://raw.githubusercontent.com/PatchMon/termd/main/install.sh | sh"
Write-Host ""
exit 1
}
if (-not $Repo) { $Repo = 'PatchMon/termd' }
if (-not $InstallDir) {
# GetFolderPath is more dependable than the environment variable, which a
# stripped-down or service account environment may not define.
$InstallDir = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'Programs\termd'
}
function Write-Step { param([string]$m) Write-Host " -> $m" -ForegroundColor Blue }
function Write-Ok { param([string]$m) Write-Host " OK $m" -ForegroundColor Green }
function Write-Warn { param([string]$m) Write-Host " ! $m" -ForegroundColor Yellow }
function Write-Failure { param([string]$m) Write-Host " X $m" -ForegroundColor Red }
function Show-Banner {
Write-Host ""
Write-Host " +---------------------------------+" -ForegroundColor Magenta
Write-Host " | |" -ForegroundColor Magenta
Write-Host " | termd installer |" -ForegroundColor Magenta
Write-Host " | terminal markdown |" -ForegroundColor Magenta
Write-Host " | |" -ForegroundColor Magenta
Write-Host " +---------------------------------+" -ForegroundColor Magenta
Write-Host ""
}
function Get-Architecture {
# RuntimeInformation is the reliable source, but it is absent on the
# older .NET Frameworks that Windows PowerShell 5.1 may sit on.
try {
$osArch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()
} catch {
# A 32-bit PowerShell on 64-bit Windows reports x86 in
# PROCESSOR_ARCHITECTURE and the real architecture in the W6432 variant.
$osArch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
}
switch -Regex ($osArch) {
'^(X64|AMD64)$' { return 'amd64' }
'^ARM64$' { return 'arm64' }
'^(X86)$' { return '386' }
default { throw "Unsupported architecture: $osArch" }
}
}
function Get-LatestTag {
$uri = "https://api.github.com/repos/$Repo/releases/latest"
# GitHub rejects API requests that do not identify themselves.
$headers = @{ 'User-Agent' = 'termd-installer'; 'Accept' = 'application/vnd.github+json' }
try {
return (Invoke-RestMethod -Uri $uri -Headers $headers).tag_name
} catch {
throw "Could not read the latest release from GitHub: $($_.Exception.Message)"
}
}
function Add-ToUserPath {
param([string]$Directory)
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
if (-not $userPath) { $userPath = '' }
# Compare trimmed, case-insensitively, so a trailing slash or different
# casing does not add the directory a second time on every run.
$entries = $userPath -split ';' | Where-Object { $_ } | ForEach-Object { $_.TrimEnd('\') }
if ($entries -contains $Directory.TrimEnd('\')) {
return $false
}
$updated = if ($userPath.TrimEnd(';')) { "$($userPath.TrimEnd(';'));$Directory" } else { $Directory }
[Environment]::SetEnvironmentVariable('Path', $updated, 'User')
# Also update this session, so termd works without reopening the terminal.
$env:Path = "$env:Path;$Directory"
return $true
}
function Install-Termd {
Show-Banner
# TLS 1.2 is not the default on Windows PowerShell 5.1, and GitHub requires it.
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
} catch {
Write-Warn "Could not select TLS 1.2; continuing with the system default."
}
$arch = Get-Architecture
Write-Ok "Detected architecture: $arch"
if (-not $Version) {
Write-Step "Fetching latest release..."
$Version = Get-LatestTag
}
Write-Ok "Version: $Version"
$archive = "termd-windows-$arch.zip"
$base = "https://github.com/$Repo/releases/download/$Version"
$work = Join-Path ([IO.Path]::GetTempPath()) ("termd-" + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $work -Force | Out-Null
try {
$zipPath = Join-Path $work $archive
Write-Step "Downloading $archive..."
Invoke-WebRequest -Uri "$base/$archive" -OutFile $zipPath -UseBasicParsing
Write-Ok "Downloaded."
# Verify before extracting: an archive that fails its checksum should
# never reach the filesystem as an executable.
Write-Step "Verifying checksum..."
$sumsPath = Join-Path $work 'checksums.txt'
try {
Invoke-WebRequest -Uri "$base/checksums.txt" -OutFile $sumsPath -UseBasicParsing
$expected = Select-String -Path $sumsPath -Pattern ([regex]::Escape($archive)) |
Select-Object -First 1 |
ForEach-Object { ($_.Line -split '\s+')[0] }
if (-not $expected) {
Write-Warn "No checksum entry for $archive. Skipping verification."
} else {
$actual = (Get-FileHash -Path $zipPath -Algorithm SHA256).Hash
if ($actual -ine $expected) {
throw "SHA-256 mismatch for ${archive}: expected $expected, got $actual"
}
Write-Ok "SHA-256 checksum verified."
}
} catch [System.Net.WebException] {
Write-Warn "Could not download checksums.txt. Skipping verification."
}
Write-Step "Extracting..."
Expand-Archive -Path $zipPath -DestinationPath $work -Force
$exe = Get-ChildItem -Path $work -Filter 'termd.exe' -Recurse | Select-Object -First 1
if (-not $exe) { throw "termd.exe was not found in $archive" }
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$target = Join-Path $InstallDir 'termd.exe'
Write-Step "Installing to $target..."
try {
Copy-Item -Path $exe.FullName -Destination $target -Force
} catch [System.IO.IOException] {
throw "Could not write $target. If termd is currently running, close it and try again."
}
Write-Ok "Installed."
if (Add-ToUserPath -Directory $InstallDir) {
Write-Ok "Added $InstallDir to your user PATH."
} else {
Write-Ok "$InstallDir is already on your PATH."
}
Write-Host ""
& $target --version
Write-Host ""
Write-Host " Done. Run 'termd README.md' to try it." -ForegroundColor Green
Write-Host " If 'termd' is not found, open a new terminal window." -ForegroundColor DarkGray
Write-Host ""
}
finally {
Remove-Item -Path $work -Recurse -Force -ErrorAction SilentlyContinue
}
}
try {
Install-Termd
} catch {
Write-Host ""
Write-Failure $_.Exception.Message
Write-Host ""
exit 1
}