-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·247 lines (218 loc) · 6.61 KB
/
Copy pathbootstrap
File metadata and controls
executable file
·247 lines (218 loc) · 6.61 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
#!/usr/bin/env bash
#
# bootstrap — install a mac dev environment.
#
# Usage:
# bootstrap Interactive: ask machine type, then optional extras.
# bootstrap --all Non-interactive: run every core step (in scope).
# bootstrap --only S1,S2 Run only the named steps (comma-separated).
# bootstrap --doctor Read-only: report current state. Installs nothing.
# bootstrap --force Re-run steps even if marked completed.
# bootstrap --help Show this help.
#
# Interactive flow:
# Asks "work or personal?" — both run every core step (no per-step prompts).
# A personal machine also gets Brewfile.home plus y/n prompts for music
# production, games (Dungeon Crawl), and media apps.
#
# Core steps (run on every machine; also the --all set):
# xcode homebrew brewfile claude chezmoi marta vim_anywhere bindings osx
#
# Extra steps (personal interactive flow, or via --only):
# brewfile_home brewfile_music brewfile_media crawl
#
# Logs every run to ~/.local/state/setup/bootstrap-<date>.log
# State file at ~/.local/state/setup/completed tracks finished steps.
set -uo pipefail
# Resolve script root (follow symlinks).
TARGET_FILE=$0
cd "$(dirname "$TARGET_FILE")" || exit 1
TARGET_FILE=$(basename "$TARGET_FILE")
while [[ -L "$TARGET_FILE" ]]; do
TARGET_FILE=$(readlink "$TARGET_FILE")
cd "$(dirname "$TARGET_FILE")" || exit 1
TARGET_FILE=$(basename "$TARGET_FILE")
done
export ROOT
ROOT=$(pwd -P)
cd - >/dev/null || exit 1
# shellcheck source=lib.sh
source "$ROOT/lib.sh"
###
# Defaults / config
###
# Steps included in --all and default-interactive. Order matters: each step
# may depend on earlier ones (e.g. brewfile needs homebrew).
ALL_STEPS=(xcode homebrew brewfile claude chezmoi marta vim_anywhere bindings osx)
# Extra steps not in --all (personal interactive flow, or opt-in via --only).
EXTRA_STEPS=(brewfile_home brewfile_music brewfile_media crawl)
MODE=interactive # interactive | all | only | doctor
ONLY_STEPS=()
FORCE=false
###
# Arg parsing
###
usage() {
sed -n '3,25p' "$0" | sed 's/^# \{0,1\}//'
}
while (( $# > 0 )); do
case $1 in
--all) MODE=all ;;
--only)
MODE=only
# Reject "--only" with nothing after, an empty arg, or another flag.
# pick_steps runs in a process-substitution subshell so `exit 2` from
# there doesn't reach the parent — validate here at parse time.
if [[ -z "${2:-}" ]] || [[ "${2:0:2}" == "--" ]]; then
log_error "--only requires a comma-separated step list"
exit 2
fi
shift
IFS=, read -r -a ONLY_STEPS <<< "$1"
# Strip empty fragments from leading / trailing / doubled commas.
_filtered=()
for _s in "${ONLY_STEPS[@]}"; do
[[ -n "$_s" ]] && _filtered+=("$_s")
done
ONLY_STEPS=("${_filtered[@]+"${_filtered[@]}"}")
unset _filtered _s
if (( ${#ONLY_STEPS[@]} == 0 )); then
log_error "--only requires a non-empty step list"
exit 2
fi
;;
--doctor) MODE=doctor ;;
--force) FORCE=true ;;
--help|-h) usage; exit 0 ;;
*) log_error "unknown flag: $1"; usage; exit 2 ;;
esac
shift
done
###
# Doctor mode (read-only short-circuit — must come BEFORE any state
# creation or logging side effects).
###
if [[ "$MODE" == "doctor" ]]; then
doctor_report
exit 0
fi
###
# Logging + state setup (modes that actually install things)
###
state_init
BOOTSTRAP_LOG="$(state_dir)/bootstrap-$(date +%Y%m%d-%H%M%S).log"
export BOOTSTRAP_LOG
touch "$BOOTSTRAP_LOG"
log_info "logging to $BOOTSTRAP_LOG"
###
# Pick the steps to run
###
# Yes/no prompt, default No. The prompt goes to stderr (via read -p) so it
# doesn't pollute the step list pick_steps prints to stdout. Returns 0 for yes.
_ask_yn() {
local reply
read -r -p "$1 [y/N] " reply
case ${reply:-N} in
[Yy]*) return 0 ;;
*) return 1 ;;
esac
}
pick_steps() {
case $MODE in
all)
printf '%s\n' "${ALL_STEPS[@]}"
;;
only)
# ONLY_STEPS is validated at parse time (see --only case above).
printf '%s\n' "${ONLY_STEPS[@]}"
;;
interactive)
local reply machine=""
while [[ "$machine" == "" ]]; do
read -r -p "What kind of machine are you setting up? [work/personal] " reply
case ${reply:-} in
[Ww]*) machine=work ;;
[Pp]*) machine=personal ;;
*) echo " please answer 'work' or 'personal'" >&2 ;;
esac
done
# Core steps run on every machine, no per-step prompting.
printf '%s\n' "${ALL_STEPS[@]}"
# Personal machines also get the home baseline plus optional extras.
if [[ "$machine" == "personal" ]]; then
printf '%s\n' brewfile_home
_ask_yn "Install music production apps?" && printf '%s\n' brewfile_music
_ask_yn "Install games (Dungeon Crawl)?" && printf '%s\n' crawl
_ask_yn "Install media apps (Plex, YACReader)?" && printf '%s\n' brewfile_media
fi
;;
esac
}
# bash 3.2 compatible (no mapfile/readarray, which are bash 4+)
STEPS_TO_RUN=()
while IFS= read -r _line; do
STEPS_TO_RUN+=("$_line")
done < <(pick_steps)
unset _line
if (( ${#STEPS_TO_RUN[@]} == 0 )); then
log_warn "no steps selected; nothing to do"
exit 0
fi
###
# Validate step names before running anything
###
valid_step() {
local s=$1
local known=("${ALL_STEPS[@]}" "${EXTRA_STEPS[@]}")
local k
for k in "${known[@]}"; do
[[ "$k" == "$s" ]] && return 0
done
return 1
}
invalid=()
for s in "${STEPS_TO_RUN[@]}"; do
valid_step "$s" || invalid+=("$s")
done
if (( ${#invalid[@]} > 0 )); then
log_error "unknown step(s): ${invalid[*]}"
log_info "valid steps: ${ALL_STEPS[*]} ${EXTRA_STEPS[*]}"
exit 2
fi
###
# Run steps
###
run_step() {
local name=$1
# State-skip applies only to the default-interactive and --all flows,
# where the user is doing a fresh setup. --only and --force are
# explicit re-run requests; respect them.
if [[ "$FORCE" != "true" ]] && [[ "$MODE" != "only" ]] && state_is_done "$name"; then
log_skip "$name (already completed; --only or --force to re-run)"
return 0
fi
log_step "$name"
if "step_$name"; then
state_mark_done "$name"
return 0
else
local rc=$?
log_error "step '$name' failed (exit $rc)"
return "$rc"
fi
}
failures=()
for s in "${STEPS_TO_RUN[@]}"; do
run_step "$s" || failures+=("$s")
done
###
# Summary
###
echo
if (( ${#failures[@]} == 0 )); then
log_ok "bootstrap complete: ${#STEPS_TO_RUN[@]} step(s) ran successfully"
else
log_warn "bootstrap finished with failures: ${failures[*]}"
log_info "see $BOOTSTRAP_LOG for details; re-run with --only ${failures[*]// /,} after fixing"
exit 1
fi