Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
300 lines (263 loc) · 8.34 KB
/
Copy pathinstall.ps1
File metadata and controls
300 lines (263 loc) · 8.34 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
param(
[string]$Version = $env:RPATH_VERSION,
[string]$InstallDir = $env:RPATH_INSTALL_DIR,
[switch]$Yes,
[switch]$NoPath,
[string]$InstallWrappers = $env:RPATH_INSTALL_WRAPPERS,
[switch]$DryRun
)
$ErrorActionPreference = "Stop"
$Repo = "rpathdev/rpath"
function Get-TextOrDefault {
param([object]$Value, [string]$Default)
$text = [string]$Value
if ([string]::IsNullOrWhiteSpace($text)) {
return $Default
}
$text
}
function Get-LocalAppData {
$path = [string]$env:LOCALAPPDATA
if (-not [string]::IsNullOrWhiteSpace($path)) {
return $path
}
$path = [Environment]::GetFolderPath("LocalApplicationData")
if (-not [string]::IsNullOrWhiteSpace($path)) {
return $path
}
$userProfile = [string]$env:USERPROFILE
if (-not [string]::IsNullOrWhiteSpace($userProfile)) {
return (Join-Path $userProfile "AppData\Local")
}
throw "could not resolve LOCALAPPDATA"
}
function Get-RpathArchitecture {
$arch = $null
try {
$runtimeArch = [Runtime.InteropServices.RuntimeInformation]::OSArchitecture
if ($null -ne $runtimeArch) {
$arch = [string]$runtimeArch
}
} catch {
$arch = $null
}
if ([string]::IsNullOrWhiteSpace([string]$arch)) {
$arch = [string]$env:PROCESSOR_ARCHITEW6432
}
if ([string]::IsNullOrWhiteSpace([string]$arch)) {
$arch = [string]$env:PROCESSOR_ARCHITECTURE
}
if ([string]::IsNullOrWhiteSpace([string]$arch)) {
throw "could not detect Windows architecture"
}
([string]$arch).ToLowerInvariant()
}
$Version = Get-TextOrDefault $Version "latest"
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
$InstallDir = Join-Path (Join-Path (Get-LocalAppData) "Programs") "rpath\bin"
}
$InstallWrappers = (Get-TextOrDefault $InstallWrappers "ask").ToLowerInvariant()
if (@("ask", "yes", "no", "all") -notcontains $InstallWrappers) {
throw "-InstallWrappers must be one of: ask, yes, no, all"
}
function Resolve-RpathArtifact {
if ([System.Environment]::OSVersion.Platform -ne [System.PlatformID]::Win32NT) {
throw "install.ps1 only supports Windows"
}
$arch = Get-RpathArchitecture
switch ($arch) {
"amd64" { $name = "rpath-windows-x86_64" }
"x64" { $name = "rpath-windows-x86_64" }
"arm64" { $name = "rpath-windows-aarch64" }
"aarch64" { $name = "rpath-windows-aarch64" }
default { throw "unsupported Windows architecture: $arch" }
}
[pscustomobject]@{
Artifact = $name
Archive = "$name.zip"
Checksum = "$name.zip.sha256"
Binary = "rpath.exe"
}
}
function Get-ReleaseBaseUrl {
if ($Version -eq "latest") {
return "https://github.com/$Repo/releases/latest/download"
}
if (([string]$Version).StartsWith("v")) {
$tag = $Version
} else {
$tag = "v$Version"
}
"https://github.com/$Repo/releases/download/$tag"
}
function Get-InstallRoot {
param([string]$Directory)
if ((Split-Path -Leaf $Directory) -ieq "bin") {
return Split-Path -Parent $Directory
}
$Directory
}
function Test-PathEntry {
param([string]$PathValue, [string]$Entry)
if ([string]::IsNullOrWhiteSpace($PathValue)) {
return $false
}
foreach ($part in ($PathValue -split ";")) {
if ([string]::Equals($part.Trim(), $Entry, [System.StringComparison]::OrdinalIgnoreCase)) {
return $true
}
}
$false
}
function Add-UserPathEntry {
param([string]$Entry)
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if (Test-PathEntry -PathValue $userPath -Entry $Entry) {
return $false
}
if ($DryRun) {
Write-Host "dry run: would add $Entry to the user PATH"
return $true
}
if ([string]::IsNullOrWhiteSpace($userPath)) {
$newPath = $Entry
} else {
$newPath = "$userPath;$Entry"
}
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
if (-not (Test-PathEntry -PathValue $env:Path -Entry $Entry)) {
$env:Path = "$Entry;$env:Path"
}
$true
}
function Test-InteractiveHost {
try {
$null = $Host.UI.RawUI.KeyAvailable
return $true
} catch {
return $Host.Name -ne "Default Host"
}
}
function Read-YesNo {
param([string]$Prompt)
if (-not (Test-InteractiveHost)) {
return $false
}
try {
$answer = Read-Host "$Prompt [y/N]"
} catch {
return $false
}
$answer -match "^(y|yes)$"
}
function Get-DetectedShell {
if ($PSVersionTable.PSEdition -eq "Core") {
return "pwsh"
}
"powershell"
}
function Invoke-WrapperInstall {
param([string]$Mode, [string]$BinaryPath)
if ($Mode -eq "all") {
if ($DryRun) {
Write-Host "dry run: would run $BinaryPath install --all"
} else {
& $BinaryPath install --all
}
return
}
$shell = Get-DetectedShell
if ($DryRun) {
Write-Host "dry run: would run $BinaryPath install --shell $shell"
} else {
& $BinaryPath install --shell $shell
}
}
function Maybe-InstallWrappers {
param([string]$BinaryPath)
switch ($InstallWrappers) {
"no" {
Write-Host "skipping shell wrapper installation"
}
"yes" {
Invoke-WrapperInstall -Mode "yes" -BinaryPath $BinaryPath
}
"all" {
Invoke-WrapperInstall -Mode "all" -BinaryPath $BinaryPath
}
"ask" {
if ($Yes) {
Invoke-WrapperInstall -Mode "yes" -BinaryPath $BinaryPath
} elseif (Read-YesNo "Install the rpath shell wrapper for this PowerShell now?") {
Invoke-WrapperInstall -Mode "yes" -BinaryPath $BinaryPath
} else {
Write-Host "skipping shell wrapper installation; run rpath install later if you want live PATH refresh"
}
}
}
}
$artifact = Resolve-RpathArtifact
$baseUrl = Get-ReleaseBaseUrl
$archiveUrl = "$baseUrl/$($artifact.Archive)"
$checksumUrl = "$baseUrl/$($artifact.Checksum)"
$binaryPath = Join-Path $InstallDir $artifact.Binary
$installRoot = Get-InstallRoot -Directory $InstallDir
$metadataPath = Join-Path $installRoot "install.json"
Write-Host "rpath installer"
Write-Host "artifact: $($artifact.Archive)"
Write-Host "install directory: $InstallDir"
if ($DryRun) {
Write-Host "dry run: would download $archiveUrl"
Write-Host "dry run: would verify $checksumUrl"
Write-Host "dry run: would install $binaryPath"
if (-not $NoPath) {
Write-Host "dry run: would add $InstallDir to the user PATH if missing"
}
Maybe-InstallWrappers -BinaryPath $binaryPath
return
}
$tempRoot = Join-Path ([IO.Path]::GetTempPath()) "rpath-install-$PID"
Remove-Item -LiteralPath $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Force -Path $tempRoot | Out-Null
try {
$archivePath = Join-Path $tempRoot $artifact.Archive
$checksumPath = Join-Path $tempRoot $artifact.Checksum
$extractDir = Join-Path $tempRoot "extract"
New-Item -ItemType Directory -Force -Path $extractDir | Out-Null
Invoke-WebRequest -Uri $archiveUrl -OutFile $archivePath -UseBasicParsing
Invoke-WebRequest -Uri $checksumUrl -OutFile $checksumPath -UseBasicParsing
$checksumText = Get-Content -Raw -LiteralPath $checksumPath
$expected = [regex]::Match($checksumText, "(?i)\b[0-9a-f]{64}\b").Value.ToLowerInvariant()
if ([string]::IsNullOrWhiteSpace($expected)) {
throw "sha256 file did not contain a valid checksum"
}
$actual = (Get-FileHash -LiteralPath $archivePath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($actual -ne $expected) {
throw "checksum mismatch for $($artifact.Archive): expected $expected, got $actual"
}
Expand-Archive -LiteralPath $archivePath -DestinationPath $extractDir -Force
$foundBinary = Get-ChildItem -LiteralPath $extractDir -Recurse -File -Filter $artifact.Binary | Select-Object -First 1
if (-not $foundBinary) {
throw "archive did not contain $($artifact.Binary)"
}
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
Copy-Item -LiteralPath $foundBinary.FullName -Destination $binaryPath -Force
$pathAdded = $false
if (-not $NoPath) {
$pathAdded = Add-UserPathEntry -Entry $InstallDir
if ($pathAdded -and -not $DryRun) {
Write-Host "added $InstallDir to the user PATH"
}
}
New-Item -ItemType Directory -Force -Path $installRoot | Out-Null
[pscustomobject]@{
installDir = $InstallDir
binary = $binaryPath
pathAdded = $pathAdded
} | ConvertTo-Json | Set-Content -LiteralPath $metadataPath -Encoding UTF8
Write-Host "installed rpath to $binaryPath"
Maybe-InstallWrappers -BinaryPath $binaryPath
Write-Host "open a new terminal, then run: rpath --version"
} finally {
Remove-Item -LiteralPath $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
}