-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
566 lines (482 loc) · 20.5 KB
/
install.ps1
File metadata and controls
566 lines (482 loc) · 20.5 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# PersistenceAI Web Installer (PowerShell)
# This script can be downloaded and executed via:
# iwr -useb https://persistence-ai.github.io/Landing/install.ps1 | iex
# curl -fsSL https://persistence-ai.github.io/Landing/install.ps1 | powershell -ExecutionPolicy Bypass -Command -
$ErrorActionPreference = "Stop"
# ============================================================================
# Output Functions (Simple, like OpenCode)
# ============================================================================
function Write-Info {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[i]" -ForegroundColor Cyan -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Success {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[+]" -ForegroundColor Green -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Error {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[x]" -ForegroundColor Red -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Warning {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[!]" -ForegroundColor Yellow -NoNewline
Write-Host " $msg" -ForegroundColor Gray
}
function Write-Step {
param([string]$msg)
Write-Host " " -NoNewline
Write-Host "[>]" -ForegroundColor Cyan -NoNewline
Write-Host " $msg" -ForegroundColor White
}
# ============================================================================
# Banner
# ============================================================================
Write-Host ""
Write-Host " " -NoNewline; Write-Host "========================================" -ForegroundColor Magenta
Write-Host " " -NoNewline; Write-Host "|" -ForegroundColor Magenta -NoNewline
Write-Host " " -NoNewline; Write-Host "PersistenceAI" -ForegroundColor Magenta -NoNewline; Write-Host " Installer" -ForegroundColor White -NoNewline; Write-Host " " -NoNewline; Write-Host "|" -ForegroundColor Magenta
Write-Host " " -NoNewline; Write-Host "========================================" -ForegroundColor Magenta
Write-Host ""
# ============================================================================
# Configuration
# ============================================================================
$BASE_URL = "https://persistence-ai.github.io/Landing"
$APP_NAME = "persistenceai"
$INSTALL_DIR = "$env:USERPROFILE\.persistenceai\bin"
$TEMP_DIR = "$env:TEMP\persistenceai-install"
# Detect platform
$os = "windows"
$arch = if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" }
if ($arch -ne "x64") {
Write-Error "Unsupported architecture: $arch. PersistenceAI requires x64."
exit 1
}
$platform = "$os-$arch"
$zipName = "$APP_NAME-$platform.zip"
# ============================================================================
# Version Detection
# ============================================================================
function Get-LatestVersion {
try {
$response = Invoke-RestMethod -Uri "$BASE_URL/api/latest.json" -ErrorAction SilentlyContinue
if ($response.version) {
return $response.version
}
} catch {
# Fallback to GitHub API
try {
$ghResponse = Invoke-RestMethod -Uri "https://api.github.com/repos/Persistence-AI/Landing/releases/latest" -ErrorAction SilentlyContinue
if ($ghResponse.tag_name) {
return $ghResponse.tag_name -replace '^v', ''
}
} catch {
return $null
}
}
return $null
}
# Determine version
$Version = $null
if ($args.Count -gt 0) {
$Version = $args[0]
Write-Info "Installing version: $Version"
} else {
Write-Step "Fetching latest version"
$Version = Get-LatestVersion
if (-not $Version) {
$Version = "latest"
Write-Warning "Could not fetch latest version, using 'latest'"
} else {
Write-Info "Latest version: $Version"
}
}
# ============================================================================
# Download URL Resolution
# ============================================================================
function Get-DownloadUrl {
param([string]$Version)
try {
if ($Version -eq "latest") {
$releaseUrl = "https://api.github.com/repos/Persistence-AI/Landing/releases/latest"
} else {
$releaseUrl = "https://api.github.com/repos/Persistence-AI/Landing/releases/tags/v$Version"
}
$release = Invoke-RestMethod -Uri $releaseUrl -ErrorAction Stop
# Update version from release tag if needed
if ($Version -eq "latest") {
$script:Version = $release.tag_name -replace '^v', ''
}
# Find Windows x64 zip asset
$windowsAsset = $release.assets | Where-Object {
($_.name -like "*windows*x64*.zip") -or
($_.name -like "*win*x64*.zip")
} | Select-Object -First 1
if ($windowsAsset) {
return $windowsAsset.browser_download_url
}
} catch {
Write-Warning "Could not query GitHub API: $_"
}
# Fallback to standard URL pattern
if ($Version -eq "latest") {
return "https://github.com/Persistence-AI/Landing/releases/latest/download/persistenceai-windows-x64.zip"
} else {
return "https://github.com/Persistence-AI/Landing/releases/download/v$Version/persistenceai-windows-x64-v$Version.zip"
}
}
Write-Step "Getting download URL"
$downloadUrl = Get-DownloadUrl -Version $Version
Write-Info "Download URL: $downloadUrl"
# ============================================================================
# Installation Check
# ============================================================================
$existingPath = Get-Command -Name $APP_NAME -ErrorAction SilentlyContinue
if ($existingPath) {
Write-Info "PersistenceAI is already installed at: $($existingPath.Source)"
$currentVersion = & $existingPath.Source --version 2>&1 | Select-Object -First 1
Write-Info "Current version: $currentVersion"
# Detect if running non-interactively (piped from web via iwr | iex)
$isNonInteractive = [Console]::IsInputRedirected
if ($currentVersion -ne $Version) {
Write-Info "Upgrading from $currentVersion to $Version..."
} elseif ($isNonInteractive) {
# Same version but non-interactive - always reinstall (like OpenCode)
Write-Info "Reinstalling version $Version (applying latest changes)..."
} else {
Write-Info "Version $Version is already installed."
$reinstall = Read-Host "Reinstall anyway? (y/N)"
if ($reinstall -ne "y" -and $reinstall -ne "Y") {
Write-Success "Installation skipped."
exit 0
}
Write-Info "Reinstalling version $Version..."
}
}
# ============================================================================
# Download Function (Simplified)
# ============================================================================
function Get-Binary {
param(
[string]$Url,
[string]$OutputPath
)
Write-Step "Downloading PersistenceAI..."
Write-Info "Source: $Url"
$ProgressPreference = 'SilentlyContinue'
try {
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($Url, $OutputPath)
$webClient.Dispose()
if (-not (Test-Path $OutputPath)) {
throw "Downloaded file not found"
}
$fileSize = (Get-Item $OutputPath).Length
if ($fileSize -eq 0) {
throw "Downloaded file is empty"
}
Write-Success "Download completed ($([math]::Round($fileSize/1MB, 2)) MB)"
return $true
} catch {
Write-Error "Download failed: $_"
return $false
}
}
# ============================================================================
# Installation Function (Simple, like OpenCode's mv)
# ============================================================================
function Install-Binary {
param(
[string]$SourcePath,
[string]$TargetPath
)
# Debug: Show source file modification time
if (Test-Path $SourcePath) {
$sourceFile = Get-Item $SourcePath
$sourceModTime = $sourceFile.LastWriteTime
$timeSinceSource = (Get-Date) - $sourceModTime
Write-Info "Source file modified: $sourceModTime ($([math]::Round($timeSinceSource.TotalMinutes, 1)) minutes ago)"
}
# Stop running processes (simple, like OpenCode)
$processes = Get-Process | Where-Object {
($_.ProcessName -eq "pai") -or
($_.ProcessName -eq "persistenceai")
} -ErrorAction SilentlyContinue
if ($processes) {
Write-Info "Stopping running processes..."
foreach ($proc in $processes) {
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
}
Start-Sleep -Seconds 2
}
# Debug: Show existing target file modification time (if it exists)
if (Test-Path $TargetPath) {
$oldFile = Get-Item $TargetPath
$oldModTime = $oldFile.LastWriteTime
$timeSinceOld = (Get-Date) - $oldModTime
Write-Info "Existing file modified: $oldModTime ($([math]::Round($timeSinceOld.TotalMinutes, 1)) minutes ago)"
Write-Info "Removing existing file..."
try {
Remove-Item -Path $TargetPath -Force -ErrorAction Stop
} catch {
Write-Warning "Could not remove existing file: $_"
Write-Info "Attempting to continue anyway..."
}
}
# Atomic move (like OpenCode's mv command)
Write-Info "Installing binary..."
try {
Move-Item -Path $SourcePath -Destination $TargetPath -Force -ErrorAction Stop
# Debug: Verify installed file modification time
Start-Sleep -Milliseconds 500 # Give Windows time to update metadata
if (Test-Path $TargetPath) {
$installedFile = Get-Item $TargetPath
$installedModTime = $installedFile.LastWriteTime
$timeSinceInstalled = (Get-Date) - $installedModTime
Write-Info "Installed file modified: $installedModTime ($([math]::Round($timeSinceInstalled.TotalMinutes, 2)) minutes ago)"
# Warn if modification time is old (more than 2 minutes)
if ($timeSinceInstalled.TotalMinutes -gt 2) {
Write-Warning "WARNING: Installed file modification time is $([math]::Round($timeSinceInstalled.TotalMinutes, 1)) minutes old!"
Write-Warning "This may indicate the file was not properly updated."
} else {
Write-Success "Binary installed successfully (file timestamp verified)"
}
}
return $true
} catch {
Write-Error "Installation failed: $_"
return $false
}
}
# ============================================================================
# PATH Management
# ============================================================================
function Update-Path {
param([string]$InstallDir)
$currentPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($currentPath -notlike "*$InstallDir*") {
Write-Step "Adding PersistenceAI to PATH..."
# Prepend to PATH to ensure our executables take precedence
$newPath = "$InstallDir;$currentPath"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
$env:Path = "$InstallDir;$env:Path"
Write-Success "PATH updated successfully"
} else {
Write-Info "Already in PATH"
# Ensure it's at the beginning
$pathParts = $currentPath -split ';'
if ($pathParts[0] -ne $InstallDir) {
$pathParts = $pathParts | Where-Object { $_ -ne $InstallDir }
$newPath = "$InstallDir;" + ($pathParts -join ';')
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
$env:Path = "$InstallDir;$env:Path"
Write-Info "Moved PersistenceAI to beginning of PATH"
}
}
}
# ============================================================================
# Uninstall Function (Enhanced with File Lock Handling)
# ============================================================================
function Uninstall-PersistenceAI {
Write-Step "Uninstalling PersistenceAI..."
# Stop processes aggressively (including child processes)
$processes = Get-Process | Where-Object {
($_.ProcessName -eq "pai") -or
($_.ProcessName -eq "persistenceai")
} -ErrorAction SilentlyContinue
if ($processes) {
Write-Info "Stopping running processes (including child processes)..."
foreach ($proc in $processes) {
try {
# Use taskkill with /T to kill process tree
Start-Process -FilePath "taskkill" -ArgumentList "/F", "/T", "/PID", $proc.Id -Wait -NoNewWindow -ErrorAction SilentlyContinue | Out-Null
} catch {
# Fallback to Stop-Process
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
}
}
Start-Sleep -Seconds 3
}
# Remove directories with retry logic
$dirsToRemove = @(
"$env:USERPROFILE\.persistenceai",
"$env:USERPROFILE\.pai",
"$env:USERPROFILE\.config\persistenceai",
"$env:USERPROFILE\.config\pai",
"$env:LOCALAPPDATA\persistenceai",
"$env:LOCALAPPDATA\pai"
)
# Also find installation directories from PATH
$paiCmd = Get-Command -Name "pai" -ErrorAction SilentlyContinue
$persistenceaiCmd = Get-Command -Name "persistenceai" -ErrorAction SilentlyContinue
if ($paiCmd) {
$binDir = Split-Path $paiCmd.Source -Parent
$installDir = Split-Path $binDir -Parent
if ($installDir -and $installDir -notin $dirsToRemove) {
$dirsToRemove += $installDir
}
}
if ($persistenceaiCmd) {
$binDir = Split-Path $persistenceaiCmd.Source -Parent
$installDir = Split-Path $binDir -Parent
if ($installDir -and $installDir -notin $dirsToRemove) {
$dirsToRemove += $installDir
}
}
foreach ($dir in $dirsToRemove) {
if (Test-Path $dir) {
Write-Info "Removing $dir..."
$maxRetries = 3
for ($i = 1; $i -le $maxRetries; $i++) {
try {
Remove-Item -Path $dir -Recurse -Force -ErrorAction Stop
break
} catch {
if ($i -lt $maxRetries) {
Write-Warning "Attempt $i failed, retrying in 2 seconds..."
Start-Sleep -Seconds 2
} else {
Write-Warning "Could not remove $dir after $maxRetries attempts"
Write-Info "Files may be locked. Try running the standalone uninstall.ps1 script or restart your computer."
}
}
}
}
}
# Clean PATH
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath) {
$newUserPath = ($userPath -split ";" | Where-Object {
$_ -and $_ -notmatch "\.persistenceai" -and $_ -notmatch "\.pai"
}) -join ";"
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
}
# Update current session PATH
$env:Path = ($env:Path -split ";" | Where-Object {
$_ -and $_ -notmatch "\.persistenceai" -and $_ -notmatch "\.pai"
}) -join ";"
Write-Success "PersistenceAI uninstalled successfully"
}
# ============================================================================
# Installation Method Detection (for uninstall)
# ============================================================================
function Get-InstallMethod {
$binaryPath = Get-Command -Name $APP_NAME -ErrorAction SilentlyContinue
if ($binaryPath) {
$installDir = Split-Path $binaryPath.Source -Parent
if ($installDir -like "*\.persistenceai\bin*") {
return "curl" # Installed via this script
}
}
return "unknown"
}
# ============================================================================
# Main Installation Flow
# ============================================================================
# Create directories
Write-Step "Preparing installation directories..."
New-Item -ItemType Directory -Force -Path $INSTALL_DIR | Out-Null
if (Test-Path $TEMP_DIR) {
Remove-Item $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
}
New-Item -ItemType Directory -Force -Path $TEMP_DIR | Out-Null
# Download
$zipPath = Join-Path $TEMP_DIR $zipName
if (-not (Get-Binary -Url $downloadUrl -OutputPath $zipPath)) {
Remove-Item -Path $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
exit 1
}
# Extract
Write-Step "Extracting archive..."
try {
Expand-Archive -Path $zipPath -DestinationPath $TEMP_DIR -Force
# Find executables
$exePath = Join-Path $TEMP_DIR "bin\$APP_NAME.exe"
if (-not (Test-Path $exePath)) {
$exePath = Join-Path $TEMP_DIR "$APP_NAME.exe"
}
$paiExePath = Join-Path $TEMP_DIR "bin\pai.exe"
if (-not (Test-Path $paiExePath)) {
$paiExePath = Join-Path $TEMP_DIR "pai.exe"
}
if (-not (Test-Path $exePath) -and -not (Test-Path $paiExePath)) {
throw "Executable not found in archive"
}
} catch {
Write-Error "Extraction failed: $_"
Remove-Item -Path $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
exit 1
}
# Install binaries
if (Test-Path $exePath) {
$targetPath = Join-Path $INSTALL_DIR "$APP_NAME.exe"
if (-not (Install-Binary -SourcePath $exePath -TargetPath $targetPath)) {
Remove-Item -Path $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
exit 1
}
}
if (Test-Path $paiExePath) {
$paiTargetPath = Join-Path $INSTALL_DIR "pai.exe"
if (-not (Install-Binary -SourcePath $paiExePath -TargetPath $paiTargetPath)) {
Remove-Item -Path $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
exit 1
}
} elseif (Test-Path $exePath) {
# Create pai.exe as a copy if it doesn't exist
$paiTargetPath = Join-Path $INSTALL_DIR "pai.exe"
Copy-Item -Path $exePath -Destination $paiTargetPath -Force
Write-Success "Created 'pai' command (alias)"
}
# Verify installation
Write-Step "Verifying installation..."
$exeFullPath = Join-Path $INSTALL_DIR "$APP_NAME.exe"
$paiExeFullPath = Join-Path $INSTALL_DIR "pai.exe"
if (-not (Test-Path $exeFullPath) -or -not (Test-Path $paiExeFullPath)) {
Write-Error "Installation verification failed"
Remove-Item -Path $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
exit 1
}
try {
$versionOutput = & $exeFullPath --version 2>&1 | Select-Object -First 1
Write-Success "Installed version: $versionOutput"
# Debug: Show final file modification times
Write-Host ""
Write-Host " " -NoNewline; Write-Host "Debug Information:" -ForegroundColor Cyan
if (Test-Path $exeFullPath) {
$finalFile = Get-Item $exeFullPath
$finalModTime = $finalFile.LastWriteTime
$timeSinceFinal = (Get-Date) - $finalModTime
Write-Info "persistenceai.exe last modified: $finalModTime ($([math]::Round($timeSinceFinal.TotalMinutes, 2)) minutes ago)"
}
if (Test-Path $paiExeFullPath) {
$finalPaiFile = Get-Item $paiExeFullPath
$finalPaiModTime = $finalPaiFile.LastWriteTime
$timeSinceFinalPai = (Get-Date) - $finalPaiModTime
Write-Info "pai.exe last modified: $finalPaiModTime ($([math]::Round($timeSinceFinalPai.TotalMinutes, 2)) minutes ago)"
}
Write-Host ""
} catch {
Write-Warning "Could not verify version"
}
# Update PATH
Update-Path -InstallDir $INSTALL_DIR
# Cleanup
Remove-Item -Path $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
# Success message
Write-Host ""
Write-Host " " -NoNewline; Write-Host "================================" -ForegroundColor DarkGray
Write-Host " " -NoNewline; Write-Host "Installation Complete!" -ForegroundColor Green
Write-Host " " -NoNewline; Write-Host "================================" -ForegroundColor DarkGray
Write-Host ""
Write-Host " " -NoNewline; Write-Host "Location:" -ForegroundColor Cyan -NoNewline; Write-Host " $INSTALL_DIR" -ForegroundColor Gray
Write-Host " " -NoNewline; Write-Host "Commands:" -ForegroundColor Cyan -NoNewline; Write-Host " persistenceai, pai" -ForegroundColor Gray
Write-Host ""
Write-Host " " -NoNewline; Write-Host "Note:" -ForegroundColor Yellow -NoNewline; Write-Host " Restart PowerShell for PATH changes to take effect" -ForegroundColor Gray
Write-Host ""