Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
340 lines (310 loc) · 12.4 KB
/
Copy pathinstall.ps1
File metadata and controls
340 lines (310 loc) · 12.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
# Install ndx.
# The script is published on GitHub Releases. The binary comes from the
# nuget.org RID package; the blob feed is used when nuget.org is unreachable.
# irm https://github.com/devlooped/ndx/releases/latest/download/install.ps1 | iex
# Env / flags: NDX_VERSION, NDX_PREFIX, NDX_ARCHIVE, NDX_RID, NDX_REPO, NDX_SKIP_PATH
# NDX_NUGET_FLAT, NDX_NUGET_REG, NDX_BLOB_FLAT
# Also accepts --version --prefix --archive --rid --repo --skip-path
$ErrorActionPreference = 'Stop'
$Repo = if ($env:NDX_REPO) { $env:NDX_REPO } else { 'devlooped/ndx' }
$Version = $env:NDX_VERSION
$Prefix = $env:NDX_PREFIX
$Archive = $env:NDX_ARCHIVE
$Rid = $env:NDX_RID
$SkipPath = $env:NDX_SKIP_PATH -eq '1'
$NugetFlat = if ($env:NDX_NUGET_FLAT) { $env:NDX_NUGET_FLAT } else { 'https://api.nuget.org/v3-flatcontainer' }
$NugetReg = if ($env:NDX_NUGET_REG) { $env:NDX_NUGET_REG } else { 'https://api.nuget.org/v3/registration5-gz-semver2' }
$BlobFlat = if ($env:NDX_BLOB_FLAT) { $env:NDX_BLOB_FLAT } else { 'https://kzu.blob.core.windows.net/nuget/flatcontainer' }
function Get-NdxRuntimeIdentifier {
# PSReadLine 2.0, shipped with Windows PowerShell 5.1, defines its own
# RuntimeInformation that has no OSArchitecture. Qualifying mscorlib
# selects the real type there, and PowerShell 7 still type-forwards it.
$arch = [System.Runtime.InteropServices.RuntimeInformation,mscorlib]::OSArchitecture
$archName = switch ($arch) {
'X64' { 'x64' }
'Arm64' { 'arm64' }
default {
throw "ndx: unsupported architecture '$arch'"
}
}
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
return "win-$archName"
}
if (Get-Variable IsMacOS -ErrorAction SilentlyContinue) {
if ($IsMacOS) { return "osx-$archName" }
if ($IsLinux) {
# Same signals as install.sh: Alpine, or musl's loader. gcompat does not hide it.
$musl = (Test-Path -LiteralPath '/etc/alpine-release') -or
(@(Get-ChildItem -Path '/lib' -Filter 'ld-musl-*.so*' -ErrorAction SilentlyContinue).Count -gt 0)
if (-not $musl -and (Get-Command ldd -ErrorAction SilentlyContinue)) {
$musl = ((& ldd --version 2>&1 | Out-String) -match 'musl')
}
if ($musl) { return "linux-musl-$archName" }
return "linux-$archName"
}
}
throw "ndx: unsupported OS"
}
function Send-EnvironmentChange {
if (-not ($IsWindows -or $env:OS -eq 'Windows_NT')) {
return
}
if (-not ('Win32.NativeBroadcast' -as [type])) {
Add-Type -Namespace Win32 -Name NativeBroadcast -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
}
$result = [UIntPtr]::Zero
[void][Win32.NativeBroadcast]::SendMessageTimeout(
[IntPtr]0xffff,
0x1a,
[UIntPtr]::Zero,
'Environment',
2,
5000,
[ref]$result)
}
function Get-NdxHttp {
if (-not $script:NdxClient) {
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
} catch {}
Add-Type -AssemblyName System.Net.Http
$handler = [System.Net.Http.HttpClientHandler]::new()
$handler.AutomaticDecompression = [System.Net.DecompressionMethods]::GZip -bor [System.Net.DecompressionMethods]::Deflate
$script:NdxClient = [System.Net.Http.HttpClient]::new($handler)
$script:NdxClient.Timeout = [TimeSpan]::FromMinutes(5)
$script:NdxClient.DefaultRequestHeaders.UserAgent.ParseAdd('ndx')
}
return $script:NdxClient
}
function Save-NdxUrl([string]$url, [string]$dest) {
$client = Get-NdxHttp
try {
$resp = $client.GetAsync($url).GetAwaiter().GetResult()
} catch {
return $false
}
try {
if (-not $resp.IsSuccessStatusCode) { return $false }
$fs = [IO.File]::Create($dest)
try {
$stream = $resp.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
try { $stream.CopyTo($fs) } finally { $stream.Dispose() }
} finally { $fs.Dispose() }
return $true
} finally {
$resp.Dispose()
}
}
function Get-NdxText([string]$url) {
$client = Get-NdxHttp
try {
$resp = $client.GetAsync($url).GetAwaiter().GetResult()
} catch {
return $null
}
try {
if (-not $resp.IsSuccessStatusCode) { return $null }
return $resp.Content.ReadAsStringAsync().GetAwaiter().GetResult()
} finally {
$resp.Dispose()
}
}
function Get-NdxLatestStable([string]$flat, [string]$id) {
$text = Get-NdxText ("{0}/{1}/index.json" -f $flat.TrimEnd('/'), $id.ToLowerInvariant())
if (-not $text) { return $null }
try { $obj = $text | ConvertFrom-Json } catch { return $null }
$best = $null
$bestText = $null
foreach ($v in @($obj.versions)) {
if (-not $v -or "$v".Contains('-')) { continue }
try { $parsed = [version]$v } catch { continue }
if ($null -eq $best -or $parsed -gt $best) {
$best = $parsed
$bestText = [string]$v
}
}
return $bestText
}
function Get-NdxCatalogHash([string]$reg, [string]$id, [string]$ver) {
$leafText = Get-NdxText ("{0}/{1}/{2}.json" -f $reg.TrimEnd('/'), $id, $ver)
if (-not $leafText) { return $null }
try { $leaf = $leafText | ConvertFrom-Json } catch { return $null }
$catalog = $leaf.catalogEntry
if ($catalog -isnot [string]) {
if ($null -eq $catalog) { return $null }
$catalog = $catalog.'@id'
}
if (-not $catalog) { return $null }
$entryText = Get-NdxText ([string]$catalog)
if (-not $entryText) { return $null }
try { $entry = $entryText | ConvertFrom-Json } catch { return $null }
$algo = [string]$entry.packageHashAlgorithm
if ($algo -and $algo -ne 'SHA512') { return $null }
$hash = [string]$entry.packageHash
if (-not $hash) { return $null }
return $hash.Trim()
}
function Get-NdxSha512([string]$path) {
$sha = [Security.Cryptography.SHA512]::Create()
try {
$fs = [IO.File]::OpenRead($path)
try { return [Convert]::ToBase64String($sha.ComputeHash($fs)) }
finally { $fs.Dispose() }
} finally {
$sha.Dispose()
}
}
function Save-NdxRidPackage([string]$id, [string]$ver, [string]$dest) {
$idL = $id.ToLowerInvariant()
$verL = $ver.ToLowerInvariant()
$rel = "$idL/$verL/$idL.$verL.nupkg"
if (Save-NdxUrl ("{0}/{1}" -f $NugetFlat.TrimEnd('/'), $rel) $dest) {
$expected = Get-NdxCatalogHash $NugetReg $idL $verL
if (-not $expected) {
Remove-Item -LiteralPath $dest -Force -ErrorAction SilentlyContinue
} else {
$actual = Get-NdxSha512 $dest
if ($actual -ne $expected) {
throw "ndx: SHA512 mismatch for $idL.$verL.nupkg`n expected: $expected`n actual: $actual"
}
return $true
}
}
return (Save-NdxUrl ("{0}/{1}" -f $BlobFlat.TrimEnd('/'), $rel) $dest)
}
function Expand-NdxPackage([string]$nupkg, [string]$destFile, [string]$entryName) {
if (-not ('System.IO.Compression.ZipFile' -as [type])) {
Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction SilentlyContinue
}
if (-not ('System.IO.Compression.ZipFile' -as [type])) {
Add-Type -AssemblyName System.IO.Compression
}
$zip = [IO.Compression.ZipFile]::OpenRead($nupkg)
try {
$entry = $null
foreach ($item in $zip.Entries) {
if ($item.FullName.Replace('\', '/') -eq $entryName) {
$entry = $item
break
}
}
if (-not $entry) { throw "ndx: package did not contain $entryName" }
$out = [IO.File]::Create($destFile)
try {
$input = $entry.Open()
try { $input.CopyTo($out) } finally { $input.Dispose() }
} finally { $out.Dispose() }
} finally {
$zip.Dispose()
}
}
function Add-NdxToUserPath([string]$dir) {
$parts = [Environment]::GetEnvironmentVariable('Path', 'User')
if (-not $parts) { $parts = '' }
$entries = $parts.Split([char]';', [StringSplitOptions]::RemoveEmptyEntries)
$env:Path = "$dir;$env:Path"
if ($entries -contains $dir) {
return
}
$updated = if ($parts) { "$parts;$dir" } else { $dir }
[Environment]::SetEnvironmentVariable('Path', $updated, 'User')
Send-EnvironmentChange
Write-Host "added $dir to the user PATH"
}
for ($i = 0; $i -lt $args.Count; $i++) {
switch -Regex ($args[$i]) {
'^--version$|^-Version$' { $Version = $args[++$i]; continue }
'^--prefix$|^-Prefix$' { $Prefix = $args[++$i]; continue }
'^--archive$|^-Archive$' { $Archive = $args[++$i]; continue }
'^--rid$|^-Rid$' { $Rid = $args[++$i]; continue }
'^--repo$|^-Repo$' { $Repo = $args[++$i]; continue }
'^--skip-path$|^-SkipPath$' { $SkipPath = $true; continue }
default { throw "ndx: unrecognized argument '$($args[$i])'" }
}
}
if (-not $Rid) {
$Rid = Get-NdxRuntimeIdentifier
}
$Rid = $Rid.ToLowerInvariant()
$windows = $Rid.StartsWith('win', [StringComparison]::OrdinalIgnoreCase)
$binary = if ($windows) { 'ndx.exe' } else { 'ndx' }
$ext = if ($windows) { 'zip' } else { 'tar.gz' }
if (-not $Prefix) {
$Prefix = if ($windows) {
Join-Path $env:LOCALAPPDATA 'ndx'
} else {
Join-Path $HOME '.local/bin'
}
}
$tmp = Join-Path ([IO.Path]::GetTempPath()) ("ndx-install-" + [guid]::NewGuid().ToString('n'))
New-Item -ItemType Directory -Path $tmp | Out-Null
try {
$extract = Join-Path $tmp 'extract'
New-Item -ItemType Directory -Path $extract | Out-Null
$fromPackage = $false
if (-not $Archive) {
$pkg = "ndx.$Rid"
if ($Version -and $Version.ToLowerInvariant() -eq 'ci') {
$tag = 'ci'
$resolved = 'ci'
$name = "ndx-$resolved-$Rid.$ext"
$base = "https://github.com/$Repo/releases/download/$tag"
$Archive = Join-Path $tmp $name
Invoke-WebRequest -Uri "$base/$name" -OutFile $Archive
Invoke-WebRequest -Uri "$base/$name.sha256" -OutFile "$Archive.sha256"
} else {
# GitHub's unauthenticated releases API returns 403 once the hourly
# quota is spent. The RID package on nuget.org is the same binary.
if ($Version) {
$resolved = $Version
if ($resolved.StartsWith('v') -or $resolved.StartsWith('V')) {
$resolved = $resolved.Substring(1)
}
} else {
$resolved = Get-NdxLatestStable $NugetFlat $pkg
if (-not $resolved) { $resolved = Get-NdxLatestStable $BlobFlat $pkg }
if (-not $resolved) { throw "ndx: could not resolve the latest stable version of $pkg" }
}
$nupkg = Join-Path $tmp "$pkg.$resolved.nupkg"
if (-not (Save-NdxRidPackage $pkg $resolved $nupkg)) {
throw "ndx: could not download $pkg $resolved"
}
Expand-NdxPackage $nupkg (Join-Path $extract $binary) "tools/any/$Rid/$binary"
$fromPackage = $true
}
}
if (-not $fromPackage) {
if (Test-Path "$Archive.sha256") {
$expected = ((Get-Content -Raw "$Archive.sha256").Trim() -split '\s+')[0].ToLowerInvariant()
$actual = (Get-FileHash -Algorithm SHA256 -Path $Archive).Hash.ToLowerInvariant()
if ($actual -ne $expected) {
throw "ndx: SHA256 mismatch for $(Split-Path $Archive -Leaf)`n expected: $expected`n actual: $actual"
}
}
if ($windows) {
Expand-Archive -Path $Archive -DestinationPath $extract -Force
} else {
tar -xzf $Archive -C $extract
}
}
$source = Join-Path $extract $binary
if (-not (Test-Path $source)) {
throw "ndx: archive did not contain $binary"
}
New-Item -ItemType Directory -Force -Path $Prefix | Out-Null
$dest = Join-Path $Prefix $binary
Copy-Item -Force -Path $source -Destination $dest
Write-Host "installed $dest"
if (-not $SkipPath) {
Add-NdxToUserPath $Prefix
}
}
finally {
if ($script:NdxClient) { $script:NdxClient.Dispose() }
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
}