-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
105 lines (94 loc) · 3.97 KB
/
Copy pathinstall.ps1
File metadata and controls
105 lines (94 loc) · 3.97 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
# Install a verified processkit-cli GitHub Release binary.
[CmdletBinding()]
param(
[ValidatePattern('^\d+\.\d+\.\d+$')]
[string] $Version,
[string] $InstallDir = (Join-Path ([Environment]::GetFolderPath('UserProfile')) '.local\bin'),
[string] $Target
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$repository = 'ZelAnton/ProcessKit-CLI'
$staged = $null
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
throw 'Install directory cannot be empty'
}
if (-not $Version) {
$release = Invoke-RestMethod `
-Uri "https://api.github.com/repos/$repository/releases/latest" `
-Headers @{ 'User-Agent' = 'processkit-cli-installer' }
if ($release.tag_name -notmatch '^v(\d+\.\d+\.\d+)$') {
throw "Latest release returned an invalid tag: $($release.tag_name)"
}
$Version = $Matches[1]
}
if (-not $Target) {
$Target = switch ($env:PROCESSOR_ARCHITECTURE) {
'AMD64' { 'x86_64-pc-windows-msvc' }
'ARM64' { 'aarch64-pc-windows-msvc' }
default { throw "No prebuilt Windows archive for architecture '$env:PROCESSOR_ARCHITECTURE'" }
}
}
if ($Target -notmatch '^[A-Za-z0-9_-]+$') {
throw "Invalid target triple: $Target"
}
$archive = "processkit-cli-v$Version-$Target.zip"
$releaseBase = if ($env:PROCESSKIT_CLI_RELEASE_BASE) {
$env:PROCESSKIT_CLI_RELEASE_BASE.TrimEnd('/')
} else {
"https://github.com/$repository/releases/download"
}
$base = "$releaseBase/v$Version"
$scratch = Join-Path ([IO.Path]::GetTempPath()) ("processkit-cli-install-" + [guid]::NewGuid())
try {
New-Item -ItemType Directory -Path $scratch | Out-Null
$archivePath = Join-Path $scratch $archive
$checksumPath = "$archivePath.sha256"
Write-Host "processkit-cli installer: downloading $archive"
Invoke-WebRequest -Uri "$base/$archive" -OutFile $archivePath
Invoke-WebRequest -Uri "$base/$archive.sha256" -OutFile $checksumPath
$checksumLine = (Get-Content -LiteralPath $checksumPath -Raw).Trim()
$expected = ($checksumLine -split '\s+')[0].ToLowerInvariant()
if ($expected -notmatch '^[0-9a-f]{64}$') {
throw 'Published checksum sidecar is malformed'
}
$actual = (Get-FileHash -LiteralPath $archivePath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($actual -ne $expected) {
throw "SHA-256 mismatch for $archive"
}
$expanded = Join-Path $scratch 'expanded'
Expand-Archive -LiteralPath $archivePath -DestinationPath $expanded
$source = Join-Path $expanded 'processkit-cli.exe'
if (-not (Test-Path -LiteralPath $source -PathType Leaf)) {
throw 'Verified archive does not contain processkit-cli.exe at its root'
}
$downloadedVersion = try { (& $source --version 2>$null) } catch { '' }
if ($downloadedVersion -ne "processkit-cli $Version") {
throw "Verified archive contains '$downloadedVersion', expected processkit-cli $Version"
}
$destination = Join-Path $InstallDir 'processkit-cli.exe'
if (Test-Path -LiteralPath $destination) {
$installedVersion = try { (& $destination --version 2>$null) } catch { '' }
if ($installedVersion -notmatch '^processkit-cli \d+\.\d+\.\d+$') {
throw "$destination exists but is not processkit-cli; refusing to overwrite"
}
}
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
$staged = "$destination.tmp.$PID"
Copy-Item -LiteralPath $source -Destination $staged
Move-Item -LiteralPath $staged -Destination $destination -Force
$staged = $null
& $destination --version
Write-Host "processkit-cli installer: installed to $destination"
$pathEntries = $env:PATH -split [IO.Path]::PathSeparator
if ($InstallDir -notin $pathEntries) {
Write-Host "processkit-cli installer: add $InstallDir to PATH"
}
} finally {
if ($staged -and (Test-Path -LiteralPath $staged)) {
Remove-Item -LiteralPath $staged -Force
}
if (Test-Path -LiteralPath $scratch) {
Remove-Item -LiteralPath $scratch -Recurse -Force
}
}