forked from gastownhall/beads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
303 lines (264 loc) · 11.1 KB
/
install.ps1
File metadata and controls
303 lines (264 loc) · 11.1 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
# Beads (bd) Windows installer
# Usage:
# irm https://raw.githubusercontent.com/steveyegge/beads/main/install.ps1 | iex
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$Script:SkipGoInstall = $env:BEADS_INSTALL_SKIP_GOINSTALL -eq "1"
$Script:SourceOverride = $env:BEADS_INSTALL_SOURCE
function Write-Info($Message) { Write-Host "==> $Message" -ForegroundColor Cyan }
function Write-Success($Message) { Write-Host "==> $Message" -ForegroundColor Green }
function Write-WarningMsg($Message) { Write-Warning $Message }
function Write-Err($Message) { Write-Host "Error: $Message" -ForegroundColor Red }
function Test-GoSupport {
$goCmd = Get-Command go -ErrorAction SilentlyContinue
if (-not $goCmd) {
return [pscustomobject]@{
Present = $false
MeetsRequirement = $false
RawVersion = $null
}
}
try {
$output = & go version
} catch {
return [pscustomobject]@{
Present = $false
MeetsRequirement = $false
RawVersion = $null
}
}
$match = [regex]::Match($output, 'go(?<major>\d+)\.(?<minor>\d+)')
if (-not $match.Success) {
return [pscustomobject]@{
Present = $true
MeetsRequirement = $true
RawVersion = $output
}
}
$major = [int]$match.Groups["major"].Value
$minor = [int]$match.Groups["minor"].Value
$meets = ($major -gt 1) -or ($major -eq 1 -and $minor -ge 24)
return [pscustomobject]@{
Present = $true
MeetsRequirement = $meets
RawVersion = $output.Trim()
}
}
function Install-WithGo {
if ($Script:SkipGoInstall) {
Write-Info "Skipping go install (BEADS_INSTALL_SKIP_GOINSTALL=1)."
return $false
}
Write-Info "Installing bd via go install..."
try {
& go install github.com/steveyegge/beads/cmd/bd@latest
if ($LASTEXITCODE -ne 0) {
Write-WarningMsg "go install exited with code $LASTEXITCODE"
return $false
}
} catch {
Write-WarningMsg "go install failed: $_"
return $false
}
# Prefer GOBIN if set, otherwise GOPATH/bin
$gobin = (& go env GOBIN) 2>$null
if ($gobin -and $gobin.Trim() -ne "") {
$binDir = $gobin.Trim()
} else {
$gopath = (& go env GOPATH)
if (-not $gopath) {
return $true
}
$binDir = Join-Path $gopath "bin"
}
$bdPath = Join-Path $binDir "bd.exe"
# Record where we expect the binary to have been installed in this run
$Script:LastInstallPath = $bdPath
if (-not (Test-Path $bdPath)) {
Write-WarningMsg "bd.exe not found in $binDir after install"
}
$pathEntries = [Environment]::GetEnvironmentVariable("PATH", "Process").Split([IO.Path]::PathSeparator) | ForEach-Object { $_.Trim() }
if (-not ($pathEntries -contains $binDir)) {
Write-WarningMsg "$binDir is not in your PATH. Add it with:`n setx PATH `"$Env:PATH;$binDir`""
}
return $true
}
function Install-FromSource {
Write-Info "Building bd from source..."
$tempRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("beads-install-" + [guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $tempRoot | Out-Null
try {
$repoPath = Join-Path $tempRoot "beads"
if ($Script:SourceOverride) {
Write-Info "Using source override: $Script:SourceOverride"
if (Test-Path $Script:SourceOverride) {
New-Item -ItemType Directory -Path $repoPath | Out-Null
Get-ChildItem -LiteralPath $Script:SourceOverride -Force | Where-Object { $_.Name -ne ".git" } | ForEach-Object {
$destination = Join-Path $repoPath $_.Name
if ($_.PSIsContainer) {
Copy-Item -LiteralPath $_.FullName -Destination $destination -Recurse -Force
} else {
Copy-Item -LiteralPath $_.FullName -Destination $repoPath -Force
}
}
} else {
Write-Info "Cloning override repository..."
& git clone $Script:SourceOverride $repoPath
if ($LASTEXITCODE -ne 0) {
throw "git clone failed with exit code $LASTEXITCODE"
}
}
} else {
Write-Info "Cloning repository..."
& git clone --depth 1 https://github.com/steveyegge/beads.git $repoPath
if ($LASTEXITCODE -ne 0) {
throw "git clone failed with exit code $LASTEXITCODE"
}
}
Push-Location $repoPath
try {
Write-Info "Compiling bd.exe..."
& go build -o bd.exe ./cmd/bd
if ($LASTEXITCODE -ne 0) {
throw "go build failed with exit code $LASTEXITCODE"
}
} finally {
Pop-Location
}
$installDir = Join-Path $env:LOCALAPPDATA "Programs\bd"
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
Copy-Item -Path (Join-Path $repoPath "bd.exe") -Destination (Join-Path $installDir "bd.exe") -Force
Write-Success "bd installed to $installDir\bd.exe"
# Record where we installed the binary when building from source
$Script:LastInstallPath = Join-Path $installDir "bd.exe"
$pathEntries = [Environment]::GetEnvironmentVariable("PATH", "Process").Split([IO.Path]::PathSeparator) | ForEach-Object { $_.Trim() }
if (-not ($pathEntries -contains $installDir)) {
Write-WarningMsg "$installDir is not in your PATH. Add it with:`n setx PATH `"$Env:PATH;$installDir`""
}
} finally {
Remove-Item -Path $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
}
return $true
}
function Get-BdPathsInPath {
$pathEntries = [Environment]::GetEnvironmentVariable("PATH", "Process").Split([IO.Path]::PathSeparator) | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
$found = @()
foreach ($entry in $pathEntries) {
try {
$candidate = Join-Path $entry "bd.exe"
} catch {
continue
}
if (Test-Path $candidate) {
try {
$resolved = (Resolve-Path $candidate -ErrorAction SilentlyContinue).ProviderPath
} catch {
$resolved = $candidate
}
if (-not ($found -contains $resolved)) { $found += $resolved }
}
}
return $found
}
function Warn-IfMultipleBd {
$paths = Get-BdPathsInPath
if ($paths.Count -le 1) { return }
Write-WarningMsg "Multiple 'bd' executables found on your PATH. This can cause an older version to be executed instead of the one we installed."
Write-Host "Found the following 'bd' executables (entries earlier in PATH take precedence):" -ForegroundColor Yellow
$i = 0
foreach ($p in $paths) {
$i++
$ver = $null
try {
$ver = & "$p" version 2>$null
if ($LASTEXITCODE -ne 0) { $ver = $null }
} catch { $ver = $null }
if (-not $ver) { $ver = "<unknown version>" }
Write-Host (" {0}. {1} -> {2}" -f $i, $p, $ver)
}
if ($Script:LastInstallPath) {
Write-Host "`nWe installed to: $($Script:LastInstallPath)" -ForegroundColor Cyan
$first = $paths[0]
if ($first -ne $Script:LastInstallPath) {
Write-WarningMsg "The 'bd' executable that appears first in your PATH is different from the one we installed. To make the newly installed 'bd' the one you get when running 'bd', either:"
Write-Host " - Remove the older $first from your PATH, or" -ForegroundColor Yellow
Write-Host " - Reorder your PATH so that $([System.IO.Path]::GetDirectoryName($Script:LastInstallPath)) appears before $([System.IO.Path]::GetDirectoryName($first))" -ForegroundColor Yellow
Write-Host "After updating PATH, restart your shell and run 'bd version' to confirm." -ForegroundColor Yellow
} else {
Write-Host "The installed 'bd' is first in your PATH." -ForegroundColor Green
}
} else {
Write-WarningMsg "We couldn't determine where we installed 'bd' during this run."
}
}
function Verify-Install {
Write-Info "Verifying installation..."
# If there are multiple bd binaries on PATH, warn the user before running the verification
try { Warn-IfMultipleBd } catch { }
try {
$versionOutput = & bd version 2>$null
if ($LASTEXITCODE -ne 0) {
Write-WarningMsg "bd version exited with code $LASTEXITCODE"
return $false
}
Write-Success "bd is installed: $versionOutput"
return $true
} catch {
Write-WarningMsg "bd is not on PATH yet. Add the install directory to PATH and re-open your shell."
return $false
}
}
$goSupport = Test-GoSupport
if ($goSupport.Present) {
Write-Info "Detected Go: $($goSupport.RawVersion)"
} else {
Write-WarningMsg "Go not found on PATH."
}
function Print-GoInstallInstructions {
Write-Host "\nTo install Go (required: 1.24+), run one of the following depending on what you have installed:" -ForegroundColor Cyan
$winget = Get-Command winget -ErrorAction SilentlyContinue
$choco = Get-Command choco -ErrorAction SilentlyContinue
$scoop = Get-Command scoop -ErrorAction SilentlyContinue
if ($winget) {
Write-Host " winget install --exact --id GoLang.Go" -ForegroundColor Yellow
Write-Host " (run as an elevated/admin terminal if required)\n" -ForegroundColor Cyan
return
}
if ($choco) {
Write-Host " choco install golang -y" -ForegroundColor Yellow
Write-Host " (requires an elevated/admin PowerShell)\n" -ForegroundColor Cyan
return
}
if ($scoop) {
Write-Host " scoop install go" -ForegroundColor Yellow
Write-Host " (scoop installs to your user profile; no admin required)\n" -ForegroundColor Cyan
return
}
Write-Host " Download and run the official installer from:" -ForegroundColor Cyan
Write-Host " https://go.dev/dl/" -ForegroundColor Yellow
Write-Host "\nAfter installing Go 1.24+, re-run this installer (the exact same command you used to invoke this script)." -ForegroundColor Cyan
}
$installed = $false
if ($goSupport.Present -and $goSupport.MeetsRequirement) {
$installed = Install-WithGo
if (-not $installed) {
Write-WarningMsg "go install failed; attempting to build from source..."
$installed = Install-FromSource
}
} elseif ($goSupport.Present -and -not $goSupport.MeetsRequirement) {
Write-Err "Go 1.24 or newer is required (found: $($goSupport.RawVersion)). Please upgrade Go or use your package manager."
Print-GoInstallInstructions
exit 1
} else {
# No Go present - do not attempt to auto-download or auto-install Go.
Write-Err "Go is not installed. bd requires Go 1.24+ to build from source."
Print-GoInstallInstructions
exit 1
}
if ($installed) {
Verify-Install | Out-Null
Write-Success "Installation complete. Run 'bd quickstart' inside a repo to begin."
} else {
Write-Err "Installation failed. Please install Go 1.24+ and try again."
exit 1
}