forked from okx/onchainos-skills
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
330 lines (279 loc) · 13 KB
/
Copy pathinstall.ps1
File metadata and controls
330 lines (279 loc) · 13 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
# ──────────────────────────────────────────────────────────────
# onchainos installer / updater (Windows)
#
# Usage (stable):
# irm https://raw.githubusercontent.com/okx/onchainos-skills/main/install.ps1 | iex
#
# Usage (beta):
# $env:ONCHAINOS_BETA=1; irm https://raw.githubusercontent.com/okx/onchainos-skills/main/install.ps1 | iex
# # or
# & ([scriptblock]::Create((irm https://raw.githubusercontent.com/okx/onchainos-skills/main/install.ps1))) --beta
#
# Behavior:
# - Default (stable): fetches latest stable release from GitHub,
# compares with local version, installs/upgrades if needed.
# - Beta: fetches all tags, finds the latest version (including
# pre-releases) by semver, and installs it.
# - Caches the last check timestamp. Skips GitHub API calls if
# checked within the last 12 hours.
#
# Supported platforms:
# Windows: x86_64, i686, ARM64
# ──────────────────────────────────────────────────────────────
param(
[switch]$beta
)
$ErrorActionPreference = "Stop"
# Support --beta via remaining args (PowerShell treats -- as param terminator)
if ($args -contains "beta" -or $args -contains "--beta") {
$beta = [switch]::new($true)
}
# Support ONCHAINOS_BETA env var (for irm | iex which cannot pass args)
if ($env:ONCHAINOS_BETA) {
$beta = [switch]::new($true)
}
$REPO = "okx/onchainos-skills"
$BINARY = "onchainos"
$INSTALL_DIR = Join-Path $env:USERPROFILE ".local\bin"
$CACHE_DIR = Join-Path $env:USERPROFILE ".onchainos"
$CACHE_FILE = Join-Path $CACHE_DIR "last_check"
$CACHE_TTL = 43200 # 12 hours in seconds
function Get-Target {
$arch = $env:PROCESSOR_ARCHITECTURE
switch ($arch) {
"AMD64" { return "x86_64-pc-windows-msvc" }
"x86" { return "i686-pc-windows-msvc" }
"ARM64" { return "aarch64-pc-windows-msvc" }
default { throw "Unsupported architecture: $arch" }
}
}
# ── Cache helpers ────────────────────────────────────────────
function Test-CacheFresh {
if (-not (Test-Path $CACHE_FILE)) { return $false }
$cachedTs = (Get-Content $CACHE_FILE -ErrorAction SilentlyContinue | Select-Object -First 1).Trim()
if (-not $cachedTs) { return $false }
$now = [int][double]::Parse((Get-Date -UFormat %s))
$elapsed = $now - [int]$cachedTs
return ($elapsed -lt $CACHE_TTL)
}
function Write-Cache {
if (-not (Test-Path $CACHE_DIR)) { New-Item -ItemType Directory -Path $CACHE_DIR -Force | Out-Null }
[int][double]::Parse((Get-Date -UFormat %s)) | Out-File -FilePath $CACHE_FILE -Encoding ascii -NoNewline
}
# ── Version helpers ──────────────────────────────────────────
function Get-LocalVersion {
$binaryPath = Join-Path $INSTALL_DIR "$BINARY.exe"
if (Test-Path $binaryPath) {
$output = & $binaryPath --version 2>$null
if ($output -match "\S+\s+(\S+)") { return $Matches[1] }
}
return $null
}
function Get-BaseVersion([string]$ver) {
return ($ver -split '-')[0]
}
function Get-PreRelease([string]$ver) {
if ($ver -match '-(.+)$') { return $Matches[1] }
return $null
}
# Semver greater-than: returns $true if $v1 > $v2
function Test-SemverGt([string]$v1, [string]$v2) {
$base1 = Get-BaseVersion $v1
$base2 = Get-BaseVersion $v2
$pre1 = Get-PreRelease $v1
$pre2 = Get-PreRelease $v2
$parts1 = $base1 -split '\.'
$parts2 = $base2 -split '\.'
for ($i = 0; $i -lt 3; $i++) {
$f1 = if ($parts1[$i]) { [int]$parts1[$i] } else { 0 }
$f2 = if ($parts2[$i]) { [int]$parts2[$i] } else { 0 }
if ($f1 -gt $f2) { return $true }
if ($f1 -lt $f2) { return $false }
}
# Base versions equal — compare pre-release
if (-not $pre1 -and -not $pre2) { return $false } # equal
if (-not $pre1) { return $true } # stable > any pre-release
if (-not $pre2) { return $false } # pre-release < stable
# Both have pre-release (e.g., beta.0 vs beta.1)
$num1 = if ($pre1 -match '(\d+)$') { [int]$Matches[1] } else { 0 }
$num2 = if ($pre2 -match '(\d+)$') { [int]$Matches[1] } else { 0 }
return ($num1 -gt $num2)
}
# ── GitHub API helpers ───────────────────────────────────────
function Get-LatestStableVersion {
try {
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/${REPO}/releases/latest" -TimeoutSec 10 -UseBasicParsing
$ver = $response.tag_name -replace '^v', ''
if ($ver) { return $ver }
} catch {}
throw "Could not fetch latest version from GitHub. Check your network connection or install manually from https://github.com/${REPO}"
}
function Get-LatestVersionWithBeta {
try {
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/${REPO}/tags?per_page=100" -TimeoutSec 10 -UseBasicParsing
$best = $null
foreach ($tag in $response) {
$v = $tag.name -replace '^v', ''
if (-not $v) { continue }
if (-not $best -or (Test-SemverGt $v $best)) {
$best = $v
}
}
if ($best) { return $best }
} catch {}
throw "Could not fetch tags from GitHub. Check your network connection or install manually from https://github.com/${REPO}"
}
# ── Binary installer ─────────────────────────────────────────
function Install-Binary {
param([string]$Tag)
$target = Get-Target
$binaryName = "${BINARY}-${target}.exe"
$url = "https://github.com/${REPO}/releases/download/${Tag}/${binaryName}"
$checksumsUrl = "https://github.com/${REPO}/releases/download/${Tag}/checksums.txt"
Write-Host "Installing ${BINARY} ${Tag} (${target})..."
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
try {
$binaryPath = Join-Path $tmpDir $binaryName
$checksumsPath = Join-Path $tmpDir "checksums.txt"
Invoke-WebRequest -Uri $url -OutFile $binaryPath -UseBasicParsing
Invoke-WebRequest -Uri $checksumsUrl -OutFile $checksumsPath -UseBasicParsing
$expectedLine = Get-Content $checksumsPath | Where-Object { $_ -match $binaryName } | Select-Object -First 1
if (-not $expectedLine) { throw "No checksum found for $binaryName" }
$expectedHash = ($expectedLine -split "\s+")[0]
$actualHash = (Get-FileHash -Path $binaryPath -Algorithm SHA256).Hash.ToLower()
if ($actualHash -ne $expectedHash) {
throw @"
Checksum mismatch!
Expected: $expectedHash
Got: $actualHash
The downloaded file may have been tampered with. Aborting.
"@
}
Write-Host "Checksum verified."
if (-not (Test-Path $INSTALL_DIR)) { New-Item -ItemType Directory -Path $INSTALL_DIR -Force | Out-Null }
$destPath = Join-Path $INSTALL_DIR "$BINARY.exe"
Move-Item -Path $binaryPath -Destination $destPath -Force
Write-Host "Installed ${BINARY} ${Tag} to ${destPath}"
}
finally {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
# ── Workflow sync ────────────────────────────────────────────
function Sync-Workflows {
param([string]$Tag)
$workflowsDir = Join-Path $CACHE_DIR "workflows"
$workflowsUrl = "https://github.com/${REPO}/releases/download/${Tag}/workflows.tar.gz"
$checksumsUrl = "https://github.com/${REPO}/releases/download/${Tag}/workflows-checksums.txt"
Write-Host "Syncing workflows (${Tag})..."
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
try {
$archivePath = Join-Path $tmpDir "workflows.tar.gz"
Invoke-WebRequest -Uri $workflowsUrl -OutFile $archivePath -UseBasicParsing -TimeoutSec 30
# Verify checksum — fail closed: skip install if verification cannot complete
$checksumsPath = Join-Path $tmpDir "workflows-checksums.txt"
try {
Invoke-WebRequest -Uri $checksumsUrl -OutFile $checksumsPath -UseBasicParsing -TimeoutSec 10
} catch {
Write-Host "Warning: could not download workflows checksum — skipping (non-fatal)" -ForegroundColor Yellow
return
}
$expectedLine = Get-Content $checksumsPath | Where-Object { $_ -match "workflows.tar.gz" } | Select-Object -First 1
if (-not $expectedLine) {
Write-Host "Warning: no checksum found for workflows.tar.gz — skipping (non-fatal)" -ForegroundColor Yellow
return
}
$expectedHash = ($expectedLine -split "\s+")[0]
$actualHash = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash.ToLower()
if ($actualHash -ne $expectedHash) {
Write-Host "Warning: workflows checksum mismatch — skipping (non-fatal)" -ForegroundColor Yellow
return
}
tar -xzf "$archivePath" -C "$tmpDir" 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Host "Warning: could not extract workflows (non-fatal)" -ForegroundColor Yellow
return
}
$srcWorkflows = Join-Path $tmpDir "workflows"
if (Test-Path $srcWorkflows) {
if (Test-Path $workflowsDir) { Remove-Item -Path $workflowsDir -Recurse -Force }
if (-not (Test-Path $CACHE_DIR)) { New-Item -ItemType Directory -Path $CACHE_DIR -Force | Out-Null }
Move-Item -Path $srcWorkflows -Destination $workflowsDir -Force
Write-Host "Workflows synced to ${workflowsDir}"
}
}
catch {
Write-Host "Warning: could not sync workflows ($($_.Exception.Message)) (non-fatal)" -ForegroundColor Yellow
}
finally {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
# ── PATH setup ───────────────────────────────────────────────
function Add-ToPath {
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -split ";" | Where-Object { $_ -eq $INSTALL_DIR }) { return }
$newPath = "${INSTALL_DIR};${userPath}"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
$env:Path = "${INSTALL_DIR};${env:Path}"
Write-Host ""
Write-Host "Added $INSTALL_DIR to your user PATH."
Write-Host "Restart your terminal or run the following to use '${BINARY}' now:"
Write-Host ""
Write-Host " `$env:Path = `"${INSTALL_DIR};`$env:Path`""
Write-Host ""
}
# ── Main ─────────────────────────────────────────────────────
function Main {
$localVer = Get-LocalVersion
if ($beta) {
# ── Beta mode: find latest version including pre-releases ──
$targetVer = Get-LatestVersionWithBeta
if ($localVer -eq $targetVer) {
$wfDir = Join-Path $CACHE_DIR "workflows"
if (-not (Test-Path $wfDir)) { Sync-Workflows -Tag "v${localVer}" }
Write-Cache
return
}
} else {
# ── Stable mode ──
# Fast path: binary exists and was checked recently — skip API call
if ($localVer -and (Test-CacheFresh)) {
$wfDir = Join-Path $CACHE_DIR "workflows"
if (-not (Test-Path $wfDir)) { Sync-Workflows -Tag "v${localVer}" }
return
}
$latestStable = Get-LatestStableVersion
if (-not $localVer) {
# Not installed — install latest stable
$targetVer = $latestStable
} elseif ($localVer -eq $latestStable) {
# Already on exact latest stable
$wfDir = Join-Path $CACHE_DIR "workflows"
if (-not (Test-Path $wfDir)) { Sync-Workflows -Tag "v${localVer}" }
Write-Cache
return
} else {
if (Test-SemverGt $latestStable $localVer) {
# Latest stable is newer than local (handles beta→stable upgrade too)
$targetVer = $latestStable
} else {
# Local is same or newer (e.g., on a beta ahead of stable)
$wfDir = Join-Path $CACHE_DIR "workflows"
if (-not (Test-Path $wfDir)) { Sync-Workflows -Tag "v${localVer}" }
Write-Cache
return
}
}
}
if ($localVer) {
Write-Host "Updating ${BINARY} from ${localVer} to ${targetVer}..."
}
Install-Binary -Tag "v${targetVer}"
Sync-Workflows -Tag "v${targetVer}"
Write-Cache
Add-ToPath
}
Main