forked from gastownhall/beads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
445 lines (384 loc) · 12.4 KB
/
install.sh
File metadata and controls
445 lines (384 loc) · 12.4 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
#!/usr/bin/env bash
#
# Beads (bd) installation script
# Usage: curl -fsSL https://raw.githubusercontent.com/steveyegge/beads/main/scripts/install.sh | bash
#
# ⚠️ IMPORTANT: This script must be EXECUTED, never SOURCED
# ❌ WRONG: source install.sh (will exit your shell on errors)
# ✅ CORRECT: bash install.sh
# ✅ CORRECT: curl -fsSL ... | bash
#
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${BLUE}==>${NC} $1"
}
log_success() {
echo -e "${GREEN}==>${NC} $1"
}
log_warning() {
echo -e "${YELLOW}==>${NC} $1"
}
log_error() {
echo -e "${RED}Error:${NC} $1" >&2
}
# Detect OS and architecture
detect_platform() {
local os arch
case "$(uname -s)" in
Darwin)
os="darwin"
;;
Linux)
os="linux"
;;
*)
log_error "Unsupported operating system: $(uname -s)"
exit 1
;;
esac
case "$(uname -m)" in
x86_64|amd64)
arch="amd64"
;;
aarch64|arm64)
arch="arm64"
;;
*)
log_error "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
echo "${os}_${arch}"
}
# Download and install from GitHub releases
install_from_release() {
log_info "Installing bd from GitHub releases..."
local platform=$1
local tmp_dir
tmp_dir=$(mktemp -d)
# Get latest release version
log_info "Fetching latest release..."
local latest_url="https://api.github.com/repos/steveyegge/beads/releases/latest"
local version
if command -v curl &> /dev/null; then
version=$(curl -fsSL "$latest_url" | grep '"tag_name"' | sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
elif command -v wget &> /dev/null; then
version=$(wget -qO- "$latest_url" | grep '"tag_name"' | sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
else
log_error "Neither curl nor wget found. Please install one of them."
return 1
fi
if [ -z "$version" ]; then
log_error "Failed to fetch latest version"
return 1
fi
log_info "Latest version: $version"
# Download URL
local archive_name="beads_${version#v}_${platform}.tar.gz"
local download_url="https://github.com/steveyegge/beads/releases/download/${version}/${archive_name}"
log_info "Downloading $archive_name..."
cd "$tmp_dir"
if command -v curl &> /dev/null; then
if ! curl -fsSL -o "$archive_name" "$download_url"; then
log_error "Download failed"
cd - > /dev/null || cd "$HOME"
rm -rf "$tmp_dir"
return 1
fi
elif command -v wget &> /dev/null; then
if ! wget -q -O "$archive_name" "$download_url"; then
log_error "Download failed"
cd - > /dev/null || cd "$HOME"
rm -rf "$tmp_dir"
return 1
fi
fi
# Extract archive
log_info "Extracting archive..."
if ! tar -xzf "$archive_name"; then
log_error "Failed to extract archive"
rm -rf "$tmp_dir"
return 1
fi
# Determine install location
local install_dir
if [[ -w /usr/local/bin ]]; then
install_dir="/usr/local/bin"
else
install_dir="$HOME/.local/bin"
mkdir -p "$install_dir"
fi
# Install binary
log_info "Installing to $install_dir..."
if [[ -w "$install_dir" ]]; then
mv bd "$install_dir/"
else
sudo mv bd "$install_dir/"
fi
log_success "bd installed to $install_dir/bd"
# Check if install_dir is in PATH
if [[ ":$PATH:" != *":$install_dir:"* ]]; then
log_warning "$install_dir is not in your PATH"
echo ""
echo "Add this to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
echo " export PATH=\"\$PATH:$install_dir\""
echo ""
fi
cd - > /dev/null || cd "$HOME"
rm -rf "$tmp_dir"
return 0
}
# Check if Go is installed and meets minimum version
check_go() {
if command -v go &> /dev/null; then
local go_version=$(go version | awk '{print $3}' | sed 's/go//')
log_info "Go detected: $(go version)"
# Extract major and minor version numbers
local major=$(echo "$go_version" | cut -d. -f1)
local minor=$(echo "$go_version" | cut -d. -f2)
# Check if Go version is 1.24 or later
if [ "$major" -eq 1 ] && [ "$minor" -lt 24 ]; then
log_error "Go 1.24 or later is required (found: $go_version)"
echo ""
echo "Please upgrade Go:"
echo " - Download from https://go.dev/dl/"
echo " - Or use your package manager to update"
echo ""
return 1
fi
return 0
else
return 1
fi
}
# Install using go install (fallback)
install_with_go() {
log_info "Installing bd using 'go install'..."
if go install github.com/steveyegge/beads/cmd/bd@latest; then
log_success "bd installed successfully via go install"
# Record where we expect the binary to have been installed
# Prefer GOBIN if set, otherwise GOPATH/bin
local gobin
gobin=$(go env GOBIN 2>/dev/null || true)
if [ -n "$gobin" ]; then
bin_dir="$gobin"
else
bin_dir="$(go env GOPATH)/bin"
fi
LAST_INSTALL_PATH="$bin_dir/bd"
# Check if GOPATH/bin (or GOBIN) is in PATH
if [[ ":$PATH:" != *":$bin_dir:"* ]]; then
log_warning "$bin_dir is not in your PATH"
echo ""
echo "Add this to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
echo " export PATH=\"\$PATH:$bin_dir\""
echo ""
fi
return 0
else
log_error "go install failed"
return 1
fi
}
# Build from source (last resort)
build_from_source() {
log_info "Building bd from source..."
local tmp_dir
tmp_dir=$(mktemp -d)
cd "$tmp_dir"
log_info "Cloning repository..."
if git clone --depth 1 https://github.com/steveyegge/beads.git; then
cd beads
log_info "Building binary..."
if go build -o bd ./cmd/bd; then
# Determine install location
local install_dir
if [[ -w /usr/local/bin ]]; then
install_dir="/usr/local/bin"
else
install_dir="$HOME/.local/bin"
mkdir -p "$install_dir"
fi
log_info "Installing to $install_dir..."
if [[ -w "$install_dir" ]]; then
mv bd "$install_dir/"
else
sudo mv bd "$install_dir/"
fi
log_success "bd installed to $install_dir/bd"
# Record where we installed the binary when building from source
LAST_INSTALL_PATH="$install_dir/bd"
# Check if install_dir is in PATH
if [[ ":$PATH:" != *":$install_dir:"* ]]; then
log_warning "$install_dir is not in your PATH"
echo ""
echo "Add this to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
echo " export PATH=\"\$PATH:$install_dir\""
echo ""
fi
cd - > /dev/null || cd "$HOME"
rm -rf "$tmp_dir"
return 0
else
log_error "Build failed"
cd - > /dev/null || cd "$HOME"
cd - > /dev/null
rm -rf "$tmp_dir"
return 1
fi
else
log_error "Failed to clone repository"
rm -rf "$tmp_dir"
return 1
fi
}
# Verify installation
verify_installation() {
# If multiple 'bd' binaries exist on PATH, warn the user before verification
warn_if_multiple_bd || true
if command -v bd &> /dev/null; then
log_success "bd is installed and ready!"
echo ""
bd version 2>/dev/null || echo "bd (development build)"
echo ""
echo "Get started:"
echo " cd your-project"
echo " bd init"
echo " bd quickstart"
echo ""
return 0
else
log_error "bd was installed but is not in PATH"
return 1
fi
}
# Returns a list of full paths to 'bd' found in PATH (earlier entries first)
get_bd_paths_in_path() {
local IFS=':'
local -a entries
read -ra entries <<< "$PATH"
local -a found
local p
for p in "${entries[@]}"; do
[ -z "$p" ] && continue
if [ -x "$p/bd" ]; then
# Resolve symlink if possible
if command -v readlink >/dev/null 2>&1; then
resolved=$(readlink -f "$p/bd" 2>/dev/null || printf '%s' "$p/bd")
else
resolved="$p/bd"
fi
# avoid duplicates
skip=0
for existing in "${found[@]:-}"; do
if [ "$existing" = "$resolved" ]; then skip=1; break; fi
done
if [ $skip -eq 0 ]; then
found+=("$resolved")
fi
fi
done
# print results, one per line
for item in "${found[@]:-}"; do
printf '%s\n' "$item"
done
}
warn_if_multiple_bd() {
# Use bash 3.2-compatible approach instead of mapfile (bash 4.0+)
bd_paths=()
while IFS= read -r line; do
bd_paths+=("$line")
done < <(get_bd_paths_in_path)
if [ "${#bd_paths[@]}" -le 1 ]; then
return 0
fi
log_warning "Multiple 'bd' executables found on your PATH. An older copy may be executed instead of the one we installed."
echo "Found the following 'bd' executables (entries earlier in PATH take precedence):"
local i=1
for p in "${bd_paths[@]}"; do
local ver
if [ -x "$p" ]; then
ver=$("$p" version 2>/dev/null || true)
fi
if [ -z "$ver" ]; then ver="<unknown version>"; fi
echo " $i. $p -> $ver"
i=$((i+1))
done
if [ -n "$LAST_INSTALL_PATH" ]; then
echo ""
echo "We installed to: $LAST_INSTALL_PATH"
# Compare first PATH entry vs installed path
first="${bd_paths[0]}"
if [ "$first" != "$LAST_INSTALL_PATH" ]; then
log_warning "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:"
echo " - Remove or rename the older $first from your PATH, or"
echo " - Reorder your PATH so that $(dirname "$LAST_INSTALL_PATH") appears before $(dirname "$first")"
echo "After updating PATH, restart your shell and run 'bd version' to confirm."
else
echo "The installed 'bd' is first in your PATH.";
fi
else
log_warning "We couldn't determine where we installed 'bd' during this run.";
fi
}
# Main installation flow
main() {
echo ""
echo "🔗 Beads (bd) Installer"
echo ""
log_info "Detecting platform..."
local platform
platform=$(detect_platform)
log_info "Platform: $platform"
# Try downloading from GitHub releases first
if install_from_release "$platform"; then
verify_installation
exit 0
fi
log_warning "Failed to install from releases, trying alternative methods..."
# Try go install as fallback
if check_go; then
if install_with_go; then
verify_installation
exit 0
fi
fi
# Try building from source as last resort
log_warning "Falling back to building from source..."
if ! check_go; then
log_warning "Go is not installed"
echo ""
echo "bd requires Go 1.24 or later to build from source. You can:"
echo " 1. Install Go from https://go.dev/dl/"
echo " 2. Use your package manager:"
echo " - macOS: brew install go"
echo " - Ubuntu/Debian: sudo apt install golang"
echo " - Other Linux: Check your distro's package manager"
echo ""
echo "After installing Go, run this script again."
exit 1
fi
if build_from_source; then
verify_installation
exit 0
fi
# All methods failed
log_error "Installation failed"
echo ""
echo "Manual installation:"
echo " 1. Download from https://github.com/steveyegge/beads/releases/latest"
echo " 2. Extract and move 'bd' to your PATH"
echo ""
echo "Or install from source:"
echo " 1. Install Go from https://go.dev/dl/"
echo " 2. Run: go install github.com/steveyegge/beads/cmd/bd@latest"
echo ""
exit 1
}
main "$@"