-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·487 lines (401 loc) · 13.9 KB
/
bootstrap.sh
File metadata and controls
executable file
·487 lines (401 loc) · 13.9 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
#!/usr/bin/env bash
# Base shell standards require explicit error handling instead of shell strict mode.
bootstrap_usage() {
cat <<'EOF'
Usage:
bootstrap.sh [options]
Options:
--source Install or update Base from a Git checkout.
--brew Install Base through Homebrew.
--install-dir <path> Source checkout path. Defaults to ~/work/base.
--repo-url <url> Git repository URL for source mode.
--branch <name> Clone a specific branch for a new source checkout.
--no-homebrew-install Fail instead of installing Homebrew when missing.
--dry-run Print planned actions without making changes.
-h, --help Show this help text.
Mode selection uses this precedence:
command-line flag, BASE_BOOTSTRAP_MODE, existing Homebrew install,
existing source checkout, then source mode.
EOF
}
bootstrap_log() {
printf '%s\n' "$*"
}
bootstrap_die() {
printf 'ERROR: %s\n' "$*" >&2
exit 1
}
bootstrap_expand_path() {
local path="$1"
case "$path" in
"~") printf '%s\n' "$HOME" ;;
"~/"*) printf '%s/%s\n' "$HOME" "${path#"~/"}" ;;
*) printf '%s\n' "$path" ;;
esac
}
bootstrap_parent_dir() {
local path="${1%/}"
case "$path" in
*/*) printf '%s\n' "${path%/*}" ;;
*) printf '.\n' ;;
esac
}
bootstrap_run() {
if [[ "${BASE_BOOTSTRAP_DRY_RUN:-false}" == "true" ]]; then
printf '[DRY-RUN] Would run:'
printf ' %q' "$@"
printf '\n'
return 0
fi
"$@"
}
bootstrap_uname() {
if [[ -n "${BASE_BOOTSTRAP_TEST_OS:-}" ]]; then
printf '%s\n' "$BASE_BOOTSTRAP_TEST_OS"
return 0
fi
uname -s
}
bootstrap_require_macos() {
local os_name
os_name="$(bootstrap_uname)"
[[ "$os_name" == "Darwin" ]] || bootstrap_die "bootstrap.sh currently supports macOS only."
}
BOOTSTRAP_BREW_BIN=""
bootstrap_find_brew() {
local candidate
local candidates="${BASE_BOOTSTRAP_BREW_CANDIDATES:-/opt/homebrew/bin/brew:/usr/local/bin/brew}"
local old_ifs
if [[ -n "${BASE_BOOTSTRAP_BREW_BIN:-}" && -x "${BASE_BOOTSTRAP_BREW_BIN:-}" ]]; then
printf '%s\n' "$BASE_BOOTSTRAP_BREW_BIN"
return 0
fi
if command -v brew >/dev/null 2>&1; then
command -v brew
return 0
fi
old_ifs="$IFS"
IFS=:
for candidate in $candidates; do
IFS="$old_ifs"
[[ -x "$candidate" ]] || continue
printf '%s\n' "$candidate"
return 0
done
IFS="$old_ifs"
return 1
}
bootstrap_refresh_brew() {
local brew_bin
local brew_dir
brew_bin="$(bootstrap_find_brew || true)"
[[ -n "$brew_bin" ]] || return 1
BOOTSTRAP_BREW_BIN="$brew_bin"
brew_dir="$(bootstrap_parent_dir "$brew_bin")"
case ":$PATH:" in
*":$brew_dir:"*) ;;
*) PATH="$brew_dir:$PATH"; export PATH ;;
esac
return 0
}
bootstrap_install_homebrew() {
local installer
local installer_url="${BASE_BOOTSTRAP_HOMEBREW_INSTALLER_URL:-https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh}"
bootstrap_log "Installing Homebrew."
if [[ "${BASE_BOOTSTRAP_DRY_RUN:-false}" == "true" ]]; then
bootstrap_log "[DRY-RUN] Would run: /bin/bash -c <Homebrew installer from $installer_url>"
BOOTSTRAP_BREW_BIN=brew
return 0
fi
command -v curl >/dev/null 2>&1 || bootstrap_die "curl is required to install Homebrew."
installer="$(curl -fsSL "$installer_url")" || bootstrap_die "Failed to download the Homebrew installer."
/bin/bash -c "$installer" || bootstrap_die "Homebrew installer failed."
}
bootstrap_ensure_homebrew() {
local allow_install="$1"
if bootstrap_refresh_brew; then
bootstrap_log "Homebrew is available at '$BOOTSTRAP_BREW_BIN'."
return 0
fi
[[ "$allow_install" == "true" ]] || bootstrap_die "Homebrew is required. Install Homebrew from https://brew.sh/ or rerun without --no-homebrew-install."
bootstrap_install_homebrew || bootstrap_die "Homebrew installation failed."
if [[ "${BASE_BOOTSTRAP_DRY_RUN:-false}" == "true" ]]; then
return 0
fi
bootstrap_refresh_brew || bootstrap_die "Homebrew installation completed, but 'brew' was not found."
bootstrap_log "Homebrew is available at '$BOOTSTRAP_BREW_BIN'."
}
bootstrap_git_usable() {
command -v git >/dev/null 2>&1 || return 1
git --version >/dev/null 2>&1
}
bootstrap_ensure_git() {
if bootstrap_git_usable; then
bootstrap_log "Git is available."
return 0
fi
bootstrap_log "Installing Git through Homebrew."
bootstrap_run "$BOOTSTRAP_BREW_BIN" install git || bootstrap_die "Failed to install Git through Homebrew."
if [[ "${BASE_BOOTSTRAP_DRY_RUN:-false}" == "true" ]]; then
return 0
fi
bootstrap_git_usable || bootstrap_die "Git was installed, but 'git --version' still does not work."
}
bootstrap_bash_version_number() {
printf '%s\n' "${BASE_BOOTSTRAP_TEST_BASH_VERSION:-${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}}"
}
bootstrap_find_supported_bash() {
local candidate
local candidates="${BASE_BOOTSTRAP_BASH_CANDIDATES:-/opt/homebrew/bin/bash:/usr/local/bin/bash}"
local current_version
local old_ifs
current_version="$(bootstrap_bash_version_number)"
if [[ "$current_version" -ge 42 ]]; then
printf '%s\n' "${BASH:-bash}"
return 0
fi
old_ifs="$IFS"
IFS=:
for candidate in $candidates; do
IFS="$old_ifs"
[[ -x "$candidate" ]] || continue
printf '%s\n' "$candidate"
return 0
done
IFS="$old_ifs"
return 1
}
bootstrap_ensure_supported_bash() {
if bootstrap_find_supported_bash >/dev/null 2>&1; then
bootstrap_log "Bash 4.2+ is available for Base."
return 0
fi
bootstrap_log "Installing Bash 4.2+ through Homebrew."
bootstrap_run "$BOOTSTRAP_BREW_BIN" install bash || bootstrap_die "Failed to install Bash through Homebrew."
if [[ "${BASE_BOOTSTRAP_DRY_RUN:-false}" == "true" ]]; then
return 0
fi
bootstrap_find_supported_bash >/dev/null 2>&1 || bootstrap_die "Bash was installed, but a supported Bash was not found."
}
bootstrap_source_checkout_present() {
local install_dir="$1"
[[ -d "$install_dir/.git" || -x "$install_dir/bin/basectl" ]]
}
bootstrap_brew_base_installed() {
local formula="$1"
[[ -n "$BOOTSTRAP_BREW_BIN" ]] || return 1
"$BOOTSTRAP_BREW_BIN" list --formula "$formula" >/dev/null 2>&1
}
bootstrap_validate_mode() {
local mode="$1"
case "$mode" in
""|source|brew) return 0 ;;
*) bootstrap_die "Invalid bootstrap mode '$mode'. Use --source, --brew, or BASE_BOOTSTRAP_MODE=source|brew." ;;
esac
}
bootstrap_select_mode() {
local requested_mode="$1"
local install_dir="$2"
local formula="$3"
if [[ -n "$requested_mode" ]]; then
printf '%s\n' "$requested_mode"
return 0
fi
if bootstrap_brew_base_installed "$formula"; then
printf 'brew\n'
return 0
fi
if bootstrap_source_checkout_present "$install_dir"; then
printf 'source\n'
return 0
fi
printf 'source\n'
}
bootstrap_install_source() {
local repo_url="$1"
local install_dir="$2"
local branch="$3"
local parent_dir
if [[ -d "$install_dir/.git" ]]; then
bootstrap_log "Updating existing Base source checkout at '$install_dir'."
bootstrap_run git -C "$install_dir" pull --ff-only || bootstrap_die "Failed to update Base source checkout."
return 0
fi
if [[ -e "$install_dir" ]]; then
bootstrap_die "Install path '$install_dir' exists but is not a Git checkout."
fi
bootstrap_log "Cloning Base into '$install_dir'."
parent_dir="$(bootstrap_parent_dir "$install_dir")"
bootstrap_run mkdir -p "$parent_dir" || bootstrap_die "Failed to create install parent directory '$parent_dir'."
if [[ -n "$branch" ]]; then
bootstrap_run git clone --branch "$branch" "$repo_url" "$install_dir" || bootstrap_die "Failed to clone Base repository."
else
bootstrap_run git clone "$repo_url" "$install_dir" || bootstrap_die "Failed to clone Base repository."
fi
}
bootstrap_install_brew_base() {
local formula="$1"
if bootstrap_brew_base_installed "$formula"; then
bootstrap_log "Base Homebrew formula '$formula' is already installed."
return 0
fi
bootstrap_log "Installing Base with Homebrew formula '$formula'."
bootstrap_run "$BOOTSTRAP_BREW_BIN" install "$formula" || bootstrap_die "Failed to install Base Homebrew formula '$formula'."
}
bootstrap_find_homebrew_basectl() {
local active_basectl
local candidate
local prefix
if [[ -n "$BOOTSTRAP_BREW_BIN" && "$BOOTSTRAP_BREW_BIN" != "brew" ]]; then
prefix="$("$BOOTSTRAP_BREW_BIN" --prefix 2>/dev/null || true)"
if [[ -n "$prefix" ]]; then
candidate="$prefix/bin/basectl"
if [[ -x "$candidate" ]]; then
printf '%s\n' "$candidate"
return 0
fi
fi
fi
active_basectl="$(command -v basectl 2>/dev/null || true)"
case "$active_basectl" in
/opt/homebrew/*|/usr/local/*)
printf '%s\n' "$active_basectl"
return 0
;;
esac
return 1
}
bootstrap_print_provenance() {
local active_basectl
local brew_basectl
local install_dir="$1"
local mode="$2"
brew_basectl="$(bootstrap_find_homebrew_basectl || true)"
active_basectl="$(command -v basectl 2>/dev/null || true)"
bootstrap_log ""
bootstrap_log "Base provenance:"
if [[ "$mode" == "source" || -e "$install_dir" ]]; then
bootstrap_log " source checkout: $install_dir"
else
bootstrap_log " source checkout: not found at $install_dir"
fi
if [[ -n "$brew_basectl" ]]; then
bootstrap_log " Homebrew basectl: $brew_basectl"
else
bootstrap_log " Homebrew basectl: not found on PATH"
fi
if [[ -n "$active_basectl" ]]; then
bootstrap_log " active basectl: $active_basectl"
else
bootstrap_log " active basectl: not found on PATH"
fi
}
bootstrap_brew_basectl_command() {
local brew_basectl
local prefix
brew_basectl="$(bootstrap_find_homebrew_basectl || true)"
if [[ -n "$brew_basectl" ]]; then
printf '%s\n' "$brew_basectl"
return 0
fi
if [[ -n "$BOOTSTRAP_BREW_BIN" && "$BOOTSTRAP_BREW_BIN" != "brew" ]]; then
prefix="$("$BOOTSTRAP_BREW_BIN" --prefix 2>/dev/null || true)"
if [[ -n "$prefix" ]]; then
printf '%s\n' "$prefix/bin/basectl"
return 0
fi
fi
printf 'basectl\n'
}
bootstrap_print_next_steps() {
local basectl_command
local install_dir="$1"
local mode="$2"
if [[ "$mode" == "source" ]]; then
basectl_command="$install_dir/bin/basectl"
else
basectl_command="$(bootstrap_brew_basectl_command)"
fi
bootstrap_log ""
bootstrap_log "Run these commands to finish Base setup and shell integration:"
bootstrap_log " $basectl_command setup"
bootstrap_log " $basectl_command update-profile"
bootstrap_log " exec \"\$SHELL\" -l"
}
bootstrap_main() {
local allow_homebrew_install="${BASE_BOOTSTRAP_HOMEBREW_INSTALL:-true}"
local branch="${BASE_BOOTSTRAP_BRANCH:-}"
local formula="${BASE_BOOTSTRAP_BREW_FORMULA:-codeforester/base/base}"
local install_dir="${BASE_BOOTSTRAP_INSTALL_DIR:-${BASE_HOME:-$HOME/work/base}}"
local mode="${BASE_BOOTSTRAP_MODE:-}"
local repo_url="${BASE_BOOTSTRAP_REPO_URL:-https://github.com/codeforester/base.git}"
BASE_BOOTSTRAP_DRY_RUN="${BASE_BOOTSTRAP_DRY_RUN:-false}"
while (($# > 0)); do
case "$1" in
-h|--help)
bootstrap_usage
return 0
;;
--source)
mode=source
shift
;;
--brew)
mode=brew
shift
;;
--install-dir|--dir)
[[ -n "${2:-}" ]] || bootstrap_die "Option '$1' requires an argument."
install_dir="$2"
shift 2
;;
--repo-url)
[[ -n "${2:-}" ]] || bootstrap_die "Option '--repo-url' requires an argument."
repo_url="$2"
shift 2
;;
--branch)
[[ -n "${2:-}" ]] || bootstrap_die "Option '--branch' requires an argument."
branch="$2"
shift 2
;;
--no-homebrew-install)
allow_homebrew_install=false
shift
;;
--dry-run)
BASE_BOOTSTRAP_DRY_RUN=true
shift
;;
*)
bootstrap_usage >&2
bootstrap_die "Unknown option '$1'."
;;
esac
done
bootstrap_validate_mode "$mode" || return $?
install_dir="$(bootstrap_expand_path "$install_dir")" || return $?
bootstrap_log "Base bootstrap"
bootstrap_require_macos || return $?
bootstrap_ensure_homebrew "$allow_homebrew_install" || return $?
bootstrap_ensure_git || return $?
bootstrap_ensure_supported_bash || return $?
mode="$(bootstrap_select_mode "$mode" "$install_dir" "$formula")" || return $?
bootstrap_log "Install mode: $mode"
case "$mode" in
source)
bootstrap_log "Repository: $repo_url"
bootstrap_log "Install path: $install_dir"
bootstrap_install_source "$repo_url" "$install_dir" "$branch" || return $?
;;
brew)
bootstrap_log "Formula: $formula"
bootstrap_install_brew_base "$formula" || return $?
;;
esac
bootstrap_print_provenance "$install_dir" "$mode" || return $?
bootstrap_print_next_steps "$install_dir" "$mode" || return $?
}
if [[ "${BASE_BOOTSTRAP_TESTING:-false}" != "true" ]]; then
bootstrap_main "$@"
fi