-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinstall.ps1
More file actions
432 lines (370 loc) · 14 KB
/
install.ps1
File metadata and controls
432 lines (370 loc) · 14 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#Requires -Version 5.1
<#
.SYNOPSIS
Autohand CLI Installer for Windows
.DESCRIPTION
Downloads and installs the Autohand CLI for Windows.
.PARAMETER Alpha
Install the latest alpha (pre-release) build
.PARAMETER Clean
Remove existing installation before installing
.PARAMETER NoCache
Bypass CDN cache and fetch fresh release from GitHub
.PARAMETER Version
Install specific version (e.g., 0.7.3)
.PARAMETER InstallDir
Custom installation directory
.EXAMPLE
iwr -useb https://autohand.ai/install.ps1 | iex
.EXAMPLE
iwr -useb https://autohand.ai/install.ps1 -OutFile install.ps1; .\install.ps1 -Alpha
.EXAMPLE
iwr -useb https://autohand.ai/install.ps1 -OutFile install.ps1; .\install.ps1 -Clean
.EXAMPLE
$env:AUTOHAND_VERSION = "0.7.3"; iwr -useb https://autohand.ai/install.ps1 | iex
#>
param(
[switch]$Alpha,
[switch]$Clean,
[switch]$NoCache,
[string]$Version,
[string]$InstallDir,
[switch]$Help
)
$ErrorActionPreference = "Stop"
$REPO = "autohandai/code-cli"
$BINARY_NAME = "autohand.exe"
function Write-Logo {
$logo = @"
___ __ __ __
/ | __ __/ /_____ / /_ ____ _____ ____/ /
/ /| |/ / / / __/ __ \/ __ \/ __ `/ __ \/ __ /
/ ___ / /_/ / /_/ /_/ / / / / /_/ / / / / /_/ /
/_/ |_\__,_/\__/\____/_/ /_/\__,_/_/ /_/\__,_/
"@
Write-Host $logo -ForegroundColor Blue
}
function Write-Step {
param([string]$Message)
Write-Host "==> " -ForegroundColor Blue -NoNewline
Write-Host $Message
}
function Write-Success {
param([string]$Message)
Write-Host "OK " -ForegroundColor Green -NoNewline
Write-Host $Message
}
function Write-Error-Custom {
param([string]$Message)
Write-Host "Error: " -ForegroundColor Red -NoNewline
Write-Host $Message
}
function Show-Help {
@"
Autohand CLI Installer for Windows
Usage: iwr -useb https://autohand.ai/install.ps1 | iex
Or download and run with options:
iwr -useb https://autohand.ai/install.ps1 -OutFile install.ps1
.\install.ps1 [OPTIONS]
Options:
-Alpha Install the latest alpha (pre-release) build
-Clean Remove existing installation before installing
-NoCache Bypass CDN cache and fetch fresh release from GitHub
-Version Install specific version (e.g., 0.7.3)
-InstallDir Custom installation directory
-Help Show this help message
Environment variables:
AUTOHAND_VERSION Install specific version (e.g., 0.7.3)
AUTOHAND_INSTALL_DIR Custom installation directory
AUTOHAND_CHANNEL Set to "alpha" for pre-release builds
Examples:
iwr -useb https://autohand.ai/install.ps1 | iex
.\install.ps1 -Alpha
.\install.ps1 -Clean
.\install.ps1 -Version 0.7.3
`$env:AUTOHAND_CHANNEL = "alpha"; iwr -useb https://autohand.ai/install.ps1 | iex
"@
}
function Get-Architecture {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
switch ($arch) {
"X64" { return "windows-x64" }
"Arm64" { return "windows-arm64" }
default {
throw "Unsupported CPU architecture: $arch"
}
}
}
function Get-LatestVersion {
$apiUrl = "https://api.github.com/repos/$REPO/releases/latest"
try {
$headers = @{
"Accept" = "application/vnd.github.v3+json"
"Cache-Control" = "no-cache"
}
$response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -UseBasicParsing
$tagName = $response.tag_name
# Remove 'v' prefix if present
if ($tagName.StartsWith("v")) {
$tagName = $tagName.Substring(1)
}
return $tagName
}
catch {
throw "Failed to fetch latest version from GitHub API: $_"
}
}
function Get-LatestAlphaVersion {
$apiUrl = "https://api.github.com/repos/$REPO/releases?per_page=100"
try {
$headers = @{
"Accept" = "application/vnd.github.v3+json"
"Cache-Control" = "no-cache"
}
$releases = Invoke-RestMethod -Uri $apiUrl -Headers $headers -UseBasicParsing
$alphaReleases = $releases | Where-Object { $_.prerelease -eq $true -and $_.tag_name }
if (-not $alphaReleases) {
throw "No alpha (pre-release) builds found"
}
# GitHub releases API order is not guaranteed chronological for prereleases.
# Choose newest by published_at (fallback to created_at).
$latestAlpha = $alphaReleases |
Sort-Object -Property @{
Expression = {
if ($_.published_at) {
[DateTime]$_.published_at
} elseif ($_.created_at) {
[DateTime]$_.created_at
} else {
[DateTime]::MinValue
}
}
Descending = $true
} |
Select-Object -First 1
$tagName = $latestAlpha.tag_name
if ($tagName.StartsWith("v")) {
$tagName = $tagName.Substring(1)
}
return $tagName
}
catch {
throw "Failed to fetch latest alpha version from GitHub API: $_"
}
}
function Get-ArchiveAssetName {
param([string]$Architecture)
switch ($Architecture) {
"windows-x64" { return "autohand-windows-x64.zip" }
default { throw "Unsupported installer architecture: $Architecture" }
}
}
function Remove-ExistingInstallation {
Write-Step "Cleaning up existing installation..."
# Common installation locations
$locations = @(
"$env:LOCALAPPDATA\autohand\autohand.exe",
"$env:LOCALAPPDATA\Programs\autohand\autohand.exe",
"$env:ProgramFiles\autohand\autohand.exe",
"$env:USERPROFILE\.local\bin\autohand.exe"
)
foreach ($loc in $locations) {
if (Test-Path $loc) {
Write-Host " Removing: $loc"
Remove-Item -Path $loc -Force -ErrorAction SilentlyContinue
}
}
# Remove autohand cache
$cacheDir = "$env:LOCALAPPDATA\autohand"
if (Test-Path "$cacheDir\version-check-stable.json") {
Write-Host " Removing: $cacheDir\version-check-stable.json"
Remove-Item -Path "$cacheDir\version-check-stable.json" -Force -ErrorAction SilentlyContinue
}
if (Test-Path "$cacheDir\version-check-alpha.json") {
Write-Host " Removing: $cacheDir\version-check-alpha.json"
Remove-Item -Path "$cacheDir\version-check-alpha.json" -Force -ErrorAction SilentlyContinue
}
# Clean up legacy cache file
if (Test-Path "$cacheDir\version-check.json") {
Write-Host " Removing: $cacheDir\version-check.json"
Remove-Item -Path "$cacheDir\version-check.json" -Force -ErrorAction SilentlyContinue
}
if (Test-Path "$cacheDir\cache") {
Write-Host " Removing: $cacheDir\cache\"
Remove-Item -Path "$cacheDir\cache" -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Success "Cleanup complete."
Write-Host ""
}
function Install-Autohand {
Write-Logo
if ($Help) {
Show-Help
return
}
# Clean existing installation if requested
if ($Clean) {
Remove-ExistingInstallation
}
# Determine channel
$channel = "stable"
if ($Alpha) {
$channel = "alpha"
}
if ($env:AUTOHAND_CHANNEL) {
$channel = $env:AUTOHAND_CHANNEL
}
# Detect architecture
Write-Step "Detecting system architecture..."
$arch = Get-Architecture
Write-Success "Detected $arch"
Write-Host ""
# Determine version
$targetVersion = $Version
if ([string]::IsNullOrEmpty($targetVersion)) {
$targetVersion = $env:AUTOHAND_VERSION
}
if ([string]::IsNullOrEmpty($targetVersion)) {
if ($channel -eq "alpha") {
Write-Step "Fetching latest alpha version..."
$targetVersion = Get-LatestAlphaVersion
Write-Success "Latest alpha version: v$targetVersion"
} else {
Write-Step "Fetching latest version..."
$targetVersion = Get-LatestVersion
Write-Success "Latest version: v$targetVersion"
}
Write-Host ""
}
# Construct bundle download URL
$archiveName = Get-ArchiveAssetName -Architecture $arch
$downloadUrl = "https://github.com/$REPO/releases/download/v$targetVersion/$archiveName"
$checksumUrl = "$downloadUrl.sha256"
# Determine installation directory
$installPath = $InstallDir
if ([string]::IsNullOrEmpty($installPath)) {
$installPath = $env:AUTOHAND_INSTALL_DIR
}
if ([string]::IsNullOrEmpty($installPath)) {
$installPath = "$env:LOCALAPPDATA\autohand"
}
# Create installation directory
if (-not (Test-Path $installPath)) {
New-Item -ItemType Directory -Path $installPath -Force | Out-Null
}
$binaryPath = Join-Path $installPath $BINARY_NAME
$rgPath = Join-Path $installPath "rg.exe"
$tempRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("autohand-install-" + [System.Guid]::NewGuid().ToString("N"))
$archivePath = Join-Path $tempRoot $archiveName
$checksumPath = "$archivePath.sha256"
$extractPath = Join-Path $tempRoot "extract"
Write-Step "Downloading Autohand CLI..."
Write-Host " Channel: $channel"
Write-Host " Platform: $arch"
Write-Host " Version: $targetVersion"
Write-Host " Target: $binaryPath"
Write-Host ""
New-Item -ItemType Directory -Path $tempRoot -Force | Out-Null
New-Item -ItemType Directory -Path $extractPath -Force | Out-Null
try {
# Download archive + checksum
try {
$headers = @{}
if ($NoCache) {
$headers["Cache-Control"] = "no-cache, no-store"
$headers["Pragma"] = "no-cache"
}
Invoke-WebRequest -Uri $downloadUrl -OutFile $archivePath -Headers $headers -UseBasicParsing
Invoke-WebRequest -Uri $checksumUrl -OutFile $checksumPath -Headers $headers -UseBasicParsing
}
catch {
Write-Error-Custom "Failed to download from $downloadUrl"
Write-Host "Hint: Check if the version exists at https://github.com/$REPO/releases" -ForegroundColor Yellow
throw $_
}
if (-not (Test-Path $archivePath) -or (Get-Item $archivePath).Length -eq 0) {
throw "Downloaded archive is empty or missing"
}
$expectedHash = (Get-Content $checksumPath -TotalCount 1).Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries)[0]
if (-not $expectedHash) {
throw "Checksum file is empty"
}
$actualHash = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($expectedHash.ToLowerInvariant() -ne $actualHash) {
throw "Checksum verification failed"
}
Write-Success "Checksum verification passed"
Expand-Archive -Path $archivePath -DestinationPath $extractPath -Force
$extractedAutohand = Get-ChildItem -Path $extractPath -Filter "autohand.exe" -Recurse | Select-Object -First 1 -ExpandProperty FullName
if (-not $extractedAutohand) {
throw "Bundle does not contain autohand.exe"
}
Copy-Item -Path $extractedAutohand -Destination $binaryPath -Force
Write-Success "Installed to $binaryPath"
if ($env:AUTOHAND_SKIP_RIPGREP -eq "1") {
Write-Host "Skipping ripgrep install because AUTOHAND_SKIP_RIPGREP=1" -ForegroundColor Yellow
} elseif (Get-Command rg -ErrorAction SilentlyContinue) {
Write-Step "ripgrep already installed, skipping bundled install"
} else {
$extractedRipgrep = Get-ChildItem -Path $extractPath -Filter "rg.exe" -Recurse | Select-Object -First 1 -ExpandProperty FullName
if ($extractedRipgrep) {
Copy-Item -Path $extractedRipgrep -Destination $rgPath -Force
Write-Success "ripgrep installed to $rgPath"
} else {
Write-Host "Bundle did not contain ripgrep, skipping" -ForegroundColor Yellow
}
}
}
finally {
if (Test-Path $tempRoot) {
Remove-Item -Path $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
}
}
Write-Step "Installing to $installPath"
# Add to PATH if not already present
$currentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($currentPath -notlike "*$installPath*") {
Write-Host ""
Write-Host "Next steps" -ForegroundColor Yellow
Write-Host ""
Write-Host "1) Add to PATH (run in PowerShell as Administrator or for current user):"
Write-Host ""
Write-Host " # For current user only:" -ForegroundColor Gray
Write-Host " `$userPath = [Environment]::GetEnvironmentVariable('PATH', 'User')"
Write-Host " [Environment]::SetEnvironmentVariable('PATH', `"`$userPath;$installPath`", 'User')"
Write-Host ""
Write-Host " # Or add to current session only:" -ForegroundColor Gray
Write-Host " `$env:PATH += `";$installPath`""
Write-Host ""
Write-Host "2) Restart your terminal or run:"
Write-Host " refreshenv (if using Chocolatey)"
Write-Host ""
}
Write-Host ""
Write-Success "Autohand CLI installed successfully!"
Write-Host ""
# Try to get version (with timeout to guard against binary hangs)
try {
$job = Start-Job -ScriptBlock { param($p) & $p --version 2>$null } -ArgumentList $binaryPath
$completed = $job | Wait-Job -Timeout 5
if ($completed) {
$versionOutput = Receive-Job -Job $job
Write-Host "Version: $versionOutput"
} else {
Stop-Job -Job $job
Write-Host "Version: $targetVersion"
}
Remove-Job -Job $job -Force -ErrorAction SilentlyContinue
}
catch {
Write-Host "Version: $targetVersion"
}
Write-Host ""
Write-Host "Get started:"
Write-Host " autohand # Start interactive mode"
Write-Host " autohand --help # Show all options"
Write-Host " autohand login # Sign in to your account"
Write-Host ""
}
# Run installer
Install-Autohand