-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.ps1
More file actions
299 lines (247 loc) · 8.93 KB
/
install.ps1
File metadata and controls
299 lines (247 loc) · 8.93 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
# Abacus Installation Script for Windows PowerShell
# Usage: irm https://raw.githubusercontent.com/ChrisEdwards/abacus/main/install.ps1 | iex
$ErrorActionPreference = "Stop"
# Configuration
$Repo = "ChrisEdwards/abacus"
$BinaryName = "abacus"
$InstallDir = "$env:LOCALAPPDATA\Programs\abacus"
# Colors for output
function Write-Info($message) {
Write-Host "ℹ " -ForegroundColor Blue -NoNewline
Write-Host $message
}
function Write-Success($message) {
Write-Host "✓ " -ForegroundColor Green -NoNewline
Write-Host $message
}
function Write-Warning($message) {
Write-Host "⚠ " -ForegroundColor Yellow -NoNewline
Write-Host $message
}
function Write-Error-Custom($message) {
Write-Host "✗ " -ForegroundColor Red -NoNewline
Write-Host $message
}
# Detect platform
function Get-Platform {
Write-Info "Detecting platform..."
$os = "windows"
$arch = if ([Environment]::Is64BitOperatingSystem) { "amd64" } else { "386" }
Write-Info "Detected platform: $os/$arch"
return @{OS = $os; Arch = $arch}
}
# Get latest version from GitHub API
function Get-LatestVersion {
Write-Info "Fetching latest version..."
try {
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest"
$version = $release.tag_name
if ([string]::IsNullOrEmpty($version)) {
throw "Version not found in API response"
}
Write-Success "Latest version: $version"
return $version
}
catch {
Write-Error-Custom "Failed to fetch latest version: $_"
exit 1
}
}
# Try go install
function Install-ViaGo {
Write-Warning "Trying go install..."
try {
$goVersion = go version 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Error-Custom "Go is not installed"
return $false
}
Write-Info "Found $goVersion"
Write-Info "Running: go install github.com/$Repo/cmd/$BinaryName@latest"
go install "github.com/$Repo/cmd/$BinaryName@latest"
if ($LASTEXITCODE -eq 0) {
$goPath = go env GOPATH
$goBin = Join-Path $goPath "bin"
$binaryPath = Join-Path $goBin "$BinaryName.exe"
if (Test-Path $binaryPath) {
Write-Success "Installed via go install to $goBin"
# Add to PATH if not already there
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$goBin*") {
Write-Info "Adding $goBin to PATH..."
[Environment]::SetEnvironmentVariable("Path", "$userPath;$goBin", "User")
$env:Path = "$env:Path;$goBin"
Write-Success "Added to PATH (restart terminal for changes to take effect)"
}
return $true
}
}
return $false
}
catch {
Write-Error-Custom "go install failed: $_"
return $false
}
}
# Download and install binary
function Install-Binary {
param(
[string]$Version,
[hashtable]$Platform
)
$versionNumber = $Version -replace '^v', ''
$archiveName = "${BinaryName}_${versionNumber}_$($Platform.OS)_$($Platform.Arch).tar.gz"
$downloadUrl = "https://github.com/$Repo/releases/download/$Version/$archiveName"
Write-Info "Downloading $archiveName..."
$tempDir = Join-Path $env:TEMP "abacus-install-$(Get-Random)"
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
try {
$archivePath = Join-Path $tempDir $archiveName
# Download
Invoke-WebRequest -Uri $downloadUrl -OutFile $archivePath -ErrorAction Stop
Write-Success "Downloaded $archiveName"
Write-Info "Extracting binary..."
# Extract using tar (available in Windows 10+)
Push-Location $tempDir
try {
tar -xzf $archiveName 2>$null
if ($LASTEXITCODE -ne 0) {
throw "tar extraction failed"
}
}
finally {
Pop-Location
}
$binaryPath = Join-Path $tempDir "$BinaryName.exe"
if (-not (Test-Path $binaryPath)) {
# Try without .exe extension (the archive contains just "abacus")
$binaryPath = Join-Path $tempDir $BinaryName
if (-not (Test-Path $binaryPath)) {
throw "Binary not found in archive"
}
}
Write-Success "Extracted binary"
# Create install directory
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
# Backup existing installation
$targetPath = Join-Path $InstallDir "$BinaryName.exe"
if (Test-Path $targetPath) {
Write-Warning "Existing installation found, backing up..."
Move-Item $targetPath "$targetPath.backup" -Force
}
# Install binary
Write-Info "Installing to $InstallDir..."
Copy-Item $binaryPath $targetPath -Force
Write-Success "Installed $BinaryName to $InstallDir"
# Add to PATH if not already there
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$InstallDir*") {
Write-Info "Adding $InstallDir to PATH..."
[Environment]::SetEnvironmentVariable("Path", "$userPath;$InstallDir", "User")
$env:Path = "$env:Path;$InstallDir"
Write-Success "Added to PATH (restart terminal for changes to take effect)"
}
return $true
}
catch {
Write-Error-Custom "Installation failed: $_"
return $false
}
finally {
# Cleanup
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
# Verify installation
function Test-Installation {
Write-Info "Verifying installation..."
$command = Get-Command $BinaryName -ErrorAction SilentlyContinue
if (-not $command) {
Write-Warning "$BinaryName is not in PATH yet"
Write-Host ""
Write-Host "Please restart your terminal or run:"
Write-Host " `$env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'User')" -ForegroundColor Blue
Write-Host ""
Write-Host "Or run directly: $InstallDir\$BinaryName.exe"
return
}
try {
$versionOutput = & $BinaryName --version 2>&1
Write-Success "$BinaryName is installed and in PATH"
Write-Host ""
Write-Host $versionOutput
}
catch {
Write-Warning "Could not verify installation: $_"
}
}
# Check for existing installation
function Test-ExistingInstallation {
$command = Get-Command $BinaryName -ErrorAction SilentlyContinue
if ($command) {
Write-Warning "Found existing installation:"
Write-Host " Location: $($command.Source)"
try {
$version = & $BinaryName --version 2>&1 | Select-Object -First 1
Write-Host " Version: $version"
}
catch {
Write-Host " Version: unknown"
}
Write-Host ""
$response = Read-Host "Continue with installation? [y/N]"
if ($response -notmatch '^[Yy]$') {
Write-Host "Installation cancelled"
exit 0
}
}
}
# Main installation flow
function Main {
Write-Host ""
Write-Host "═══════════════════════════════════════"
Write-Host " Abacus Installation Script"
Write-Host "═══════════════════════════════════════"
Write-Host ""
Test-ExistingInstallation
$platform = Get-Platform
$version = Get-LatestVersion
Write-Host ""
# Try direct binary installation first
if (Install-Binary -Version $version -Platform $platform) {
Write-Host ""
Test-Installation
Write-Host ""
Write-Success "Installation complete!"
}
else {
# Fall back to go install
Write-Host ""
Write-Warning "Direct installation failed"
if (Install-ViaGo) {
Write-Host ""
Test-Installation
Write-Host ""
Write-Success "Installation complete via go install!"
}
else {
Write-Host ""
Write-Error-Custom "Installation failed"
Write-Host ""
Write-Host "Please try one of these alternatives:"
Write-Host " 1. Install Go and run: go install github.com/$Repo/cmd/$BinaryName@latest"
Write-Host " 2. Download manually from: https://github.com/$Repo/releases"
Write-Host " 3. Install via Scoop (if available): scoop install abacus"
exit 1
}
}
Write-Host ""
Write-Host "For more information:"
Write-Host " Documentation: https://github.com/$Repo"
Write-Host " Report issues: https://github.com/$Repo/issues"
Write-Host ""
}
# Run main function
Main