-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
224 lines (193 loc) · 6.66 KB
/
Copy pathinstall.ps1
File metadata and controls
224 lines (193 loc) · 6.66 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
param(
[string]$Version,
[string]$InstallPath
)
$Owner = "aiomayo"
$Repo = "hdf"
$BinaryName = "hdf"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function Get-LatestRelease {
$apiUrl = "https://api.github.com/repos/$Owner/$Repo/releases/latest"
try {
$response = Invoke-RestMethod -Uri $apiUrl -Method Get -ErrorAction Stop
return $response.tag_name
}
catch {
Write-Error "Failed to fetch latest release: $_"
Write-Error "Please check your internet connection or visit https://github.com/$Owner/$Repo/releases"
exit 1
}
}
function Get-Architecture {
$arch = $env:PROCESSOR_ARCHITECTURE
switch ($arch) {
"AMD64" { return "x86_64" }
"ARM64" { return "arm64" }
"x86" { return "i386" }
default {
Write-Error "Unsupported architecture: $arch"
Write-Error "Supported architectures: AMD64, ARM64, x86"
exit 1
}
}
}
function Test-CommandExists {
param($command)
$null = Get-Command $command -ErrorAction SilentlyContinue
return $?
}
function Get-InstalledVersion {
if (Test-CommandExists $BinaryName) {
try {
$output = & $BinaryName --version 2>&1 | Out-String
if ($output -match 'v?(\d+\.\d+\.\d+)') {
return $matches[1]
}
}
catch {
return $null
}
}
return $null
}
function Compare-Versions {
param(
[string]$TargetVersion,
[string]$InstalledVersion
)
$targetClean = $TargetVersion -replace '^v', ''
$installedClean = $InstalledVersion -replace '^v', ''
return $targetClean -eq $installedClean
}
function Verify-Checksum {
param(
[string]$FilePath,
[string]$ChecksumFile
)
$fileName = Split-Path $FilePath -Leaf
$content = Get-Content $ChecksumFile
$checksumLine = $content | Where-Object { $_ -like "*$fileName*" }
if (-not $checksumLine) {
Write-Warning "Checksum not found for $fileName, skipping verification"
return $true
}
$expectedChecksum = ($checksumLine -split '\s+')[0]
$fileHash = (Get-FileHash -Path $FilePath -Algorithm SHA256).Hash.ToLower()
if ($fileHash -eq $expectedChecksum.ToLower()) {
Write-Host "Checksum verified" -ForegroundColor Green
return $true
}
else {
Write-Error "Checksum verification failed"
Write-Error "Expected: $expectedChecksum"
Write-Error "Got: $fileHash"
return $false
}
}
if (-not $Version) {
Write-Host "Fetching latest release..." -ForegroundColor Cyan
$Version = Get-LatestRelease
}
$installedVersion = Get-InstalledVersion
if ($installedVersion) {
Write-Host "Currently installed: $installedVersion" -ForegroundColor Cyan
if (Compare-Versions -TargetVersion $Version -InstalledVersion $installedVersion) {
Write-Host "$BinaryName $Version is already installed and up to date" -ForegroundColor Green
exit 0
}
else {
Write-Host "Updating $BinaryName from $installedVersion to $Version" -ForegroundColor Yellow
}
}
else {
Write-Host "`nInstalling $BinaryName $Version" -ForegroundColor Green
}
$arch = Get-Architecture
$os = "Windows"
$versionNumber = $Version -replace '^v', ''
$fileName = "${BinaryName}_${versionNumber}_${os}_${arch}.zip"
$downloadUrl = "https://github.com/$Owner/$Repo/releases/download/$Version/$fileName"
$checksumUrl = "https://github.com/$Owner/$Repo/releases/download/$Version/checksums.txt"
if (-not $InstallPath) {
$InstallPath = "$env:LOCALAPPDATA\$BinaryName\bin"
}
$tempDir = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName())
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
try {
$zipPath = Join-Path $tempDir $fileName
Write-Host "Downloading from GitHub releases..." -ForegroundColor Cyan
try {
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing -ErrorAction Stop
}
catch {
Write-Error "Failed to download $fileName"
Write-Error "URL: $downloadUrl"
Write-Error "Error: $_"
exit 1
}
$checksumPath = Join-Path $tempDir "checksums.txt"
Write-Host "Verifying checksum..." -ForegroundColor Cyan
try {
Invoke-WebRequest -Uri $checksumUrl -OutFile $checksumPath -UseBasicParsing -ErrorAction Stop
if (-not (Verify-Checksum -FilePath $zipPath -ChecksumFile $checksumPath)) {
exit 1
}
}
catch {
Write-Warning "Could not verify checksum: $_"
}
Write-Host "Extracting..." -ForegroundColor Cyan
Expand-Archive -Path $zipPath -DestinationPath $tempDir -Force
if (-not (Test-Path $InstallPath)) {
New-Item -ItemType Directory -Path $InstallPath -Force | Out-Null
}
$binaryPath = Join-Path $tempDir "$BinaryName.exe"
$destPath = Join-Path $InstallPath "$BinaryName.exe"
if (-not (Test-Path $binaryPath)) {
Write-Error "Binary not found in archive: $binaryPath"
Write-Error "Archive contents:"
Get-ChildItem $tempDir | ForEach-Object { Write-Error " $_" }
exit 1
}
Copy-Item -Path $binaryPath -Destination $destPath -Force
Write-Host "Installed to: $destPath" -ForegroundColor Green
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$InstallPath*") {
Write-Host "Adding to PATH..." -ForegroundColor Cyan
[Environment]::SetEnvironmentVariable(
"Path",
"$userPath;$InstallPath",
"User"
)
$env:Path = "$env:Path;$InstallPath"
Write-Host "Added $InstallPath to PATH" -ForegroundColor Green
Write-Host "`nRestart your terminal for PATH changes to take effect" -ForegroundColor Yellow
}
Write-Host "`n$BinaryName installed successfully!" -ForegroundColor Green
$env:Path = "$env:Path;$InstallPath"
if (Test-CommandExists $BinaryName) {
Write-Host "`nVersion:" -ForegroundColor Cyan
try {
& $BinaryName --version
}
catch {
Write-Host "Run '$BinaryName --version' to verify" -ForegroundColor Yellow
}
}
else {
Write-Host "`nRun 'refreshenv' or restart your terminal" -ForegroundColor Yellow
}
Write-Host "`nQuick start:" -ForegroundColor Cyan
Write-Host " hdf <query> Kill processes matching query"
Write-Host " hdf -l <query> List matching processes"
Write-Host " hdf --help Show all options"
}
catch {
Write-Error "Installation failed: $_"
exit 1
}
finally {
if (Test-Path $tempDir) {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}