Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
165 lines (146 loc) · 7.35 KB
/
Copy pathinstall.ps1
File metadata and controls
165 lines (146 loc) · 7.35 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
# Install a Hanzo native binary on Windows (PowerShell).
#
# irm https://raw.githubusercontent.com/hanzoai/cli/main/install.ps1 | iex
#
# Downloads <BIN>-windows-<arch>.tar.gz from GitHub Releases, verifies its
# .sha256 checksum, extracts <BIN>.exe into $HOME\.local\bin (or
# $env:HANZO_INSTALL_PREFIX), installs the alias copy, and ensures the directory
# is on the User PATH.
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
if (Get-Variable -Name PSNativeCommandUseErrorActionPreference -ErrorAction SilentlyContinue) {
$PSNativeCommandUseErrorActionPreference = $false
}
function Invoke-HanzoInstall {
param(
[string]$Repo = $(if ($env:HANZO_INSTALL_REPO) { $env:HANZO_INSTALL_REPO } else { 'hanzoai/cli' }),
[string]$Bin = $(if ($env:HANZO_INSTALL_BIN) { $env:HANZO_INSTALL_BIN } else { 'hanzo' }),
[string]$Alias = $(if (Test-Path env:HANZO_INSTALL_ALIAS) { $env:HANZO_INSTALL_ALIAS } else { 'hanzo-cli' }),
[string]$Prefix = $(if ($env:HANZO_INSTALL_PREFIX) { $env:HANZO_INSTALL_PREFIX } else { Join-Path $HOME '.local\bin' }),
[string]$Tag = $(if ($env:HANZO_VERSION) { $env:HANZO_VERSION } else { '' })
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13
$arch = switch ($env:PROCESSOR_ARCHITECTURE) {
'ARM64' { 'amd64' } # fallback if WoW64/x64 emulation; native amd64 asset
default { 'amd64' }
}
if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64' -and [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq 'Arm64') {
# Keep windows-amd64 as primary published target (x64 emulation supported on Windows 11 ARM64).
$arch = 'amd64'
}
$target = "windows-$arch"
$asset = "$Bin-$target.tar.gz"
$token = $env:HANZO_INSTALL_TOKEN
if (-not $token) { $token = $env:GH_TOKEN }
if (-not $token) { $token = $env:GITHUB_TOKEN }
$dlHeaders = @{ 'User-Agent' = 'hanzo-installer/1.0' }
$apiHeaders = @{ 'User-Agent' = 'hanzo-installer/1.0' }
if ($token) { $apiHeaders['Authorization'] = "Bearer $token" }
function Get-TagCandidates {
param([string]$RepoName, [hashtable]$Hdr)
try {
$rel = Invoke-RestMethod -Uri "https://api.github.com/repos/$RepoName/releases?per_page=30" -Headers $Hdr
return @($rel | Where-Object { -not $_.draft } | ForEach-Object { $_.tag_name })
} catch {
# Fallback to git ls-remote when API rate limit hits anonymously.
if (Get-Command git -ErrorAction SilentlyContinue) {
$lines = git ls-remote --tags "https://github.com/$RepoName.git" 2>$null
$items = foreach ($line in $lines) {
if ($line -match 'refs/tags/([^\^]+?)(?:\^\{\})?$') {
$tag = $Matches[1]
if ($tag -match '^(?:.*-)?v(\d+\.\d+\.\d+)$') {
[PSCustomObject]@{ Tag = $tag; Ver = [version]$Matches[1] }
}
}
}
return @($items | Sort-Object Ver -Descending | Select-Object -ExpandProperty Tag -Unique)
}
throw
}
}
function Test-ReleaseAsset {
param([string]$RepoName, [string]$ReleaseTag, [string]$AssetName, [hashtable]$Hdr)
$shaUrl = "https://github.com/$RepoName/releases/download/$ReleaseTag/$AssetName.sha256"
try {
$resp = Invoke-WebRequest -UseBasicParsing -Method Head -Uri $shaUrl -Headers $Hdr -TimeoutSec 10
return ($resp.StatusCode -ge 200 -and $resp.StatusCode -lt 400)
} catch {
return $false
}
}
if (-not $Tag) {
$candidates = Get-TagCandidates -RepoName $Repo -Hdr $apiHeaders
foreach ($cand in $candidates) {
if (Test-ReleaseAsset -RepoName $Repo -ReleaseTag $cand -AssetName $asset -Hdr $dlHeaders) {
$Tag = $cand
break
}
}
if (-not $Tag) {
throw "${Bin}: no stable release of $Repo has a published $target build."
}
}
$tmp = Join-Path ([IO.Path]::GetTempPath()) ("hanzo-inst-" + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Force -Path $tmp | Out-Null
try {
$base = "https://github.com/$Repo/releases/download/$Tag"
$tgzPath = Join-Path $tmp $asset
$shaPath = Join-Path $tmp "$asset.sha256"
Write-Host "${Bin}: $Tag $target"
Invoke-WebRequest -UseBasicParsing -Uri "$base/$asset" -Headers $dlHeaders -OutFile $tgzPath
Invoke-WebRequest -UseBasicParsing -Uri "$base/$asset.sha256" -Headers $dlHeaders -OutFile $shaPath
$want = ((Get-Content -Raw $shaPath) -split '\s+')[0].Trim().ToLowerInvariant()
if ($want.Length -ne 64) {
throw "${Bin}: invalid checksum file for $asset"
}
$got = (Get-FileHash -Algorithm SHA256 $tgzPath).Hash.ToLowerInvariant()
if ($got -ne $want) {
throw "${Bin}: checksum MISMATCH for $asset (want $want, got $got)"
}
$tarExe = (Get-Command tar.exe -ErrorAction SilentlyContinue).Source
if (-not $tarExe) { $tarExe = (Get-Command tar -ErrorAction Stop).Source }
& $tarExe -xzf $tgzPath -C $tmp
if ($LASTEXITCODE -ne 0) {
throw "${Bin}: failed extracting $asset"
}
$extracted = Get-ChildItem -Path $tmp -Filter "$Bin.exe" -Recurse | Select-Object -First 1
if (-not $extracted) {
throw "${Bin}: archive $asset did not contain $Bin.exe"
}
New-Item -ItemType Directory -Force -Path $Prefix | Out-Null
$dest = Join-Path $Prefix "$Bin.exe"
$old = "$dest.old"
if (Test-Path $old) { Remove-Item -Force $old -ErrorAction SilentlyContinue }
if (Test-Path $dest) { Move-Item -Force $dest $old -ErrorAction SilentlyContinue }
Move-Item -Force $extracted.FullName $dest
if (Test-Path $old) { Remove-Item -Force $old -ErrorAction SilentlyContinue }
Write-Host "${Bin}: installed $dest"
if ($Alias -and $Alias -ne $Bin) {
$aliasDest = Join-Path $Prefix "$Alias.exe"
$aliasOld = "$aliasDest.old"
if (Test-Path $aliasOld) { Remove-Item -Force $aliasOld -ErrorAction SilentlyContinue }
if (Test-Path $aliasDest) { Move-Item -Force $aliasDest $aliasOld -ErrorAction SilentlyContinue }
Copy-Item -Force $dest $aliasDest
if (Test-Path $aliasOld) { Remove-Item -Force $aliasOld -ErrorAction SilentlyContinue }
Write-Host "${Bin}: installed $aliasDest (alias)"
}
} finally {
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
}
# Ensure $Prefix is on User PATH and current session PATH.
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
$parts = @()
if ($userPath) { $parts = $userPath -split ';' | Where-Object { $_ } }
if ($parts -notcontains $Prefix) {
$newPath = (@($Prefix) + $parts) -join ';'
[Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
Write-Host "${Bin}: added $Prefix to User PATH"
}
if (($env:Path -split ';') -notcontains $Prefix) {
$env:Path = "$Prefix;$env:Path"
}
if ($Bin -eq 'hanzo') {
Write-Host "hanzo: next -> hanzo auth login"
}
}
Invoke-HanzoInstall