Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·403 lines (364 loc) · 12.5 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·403 lines (364 loc) · 12.5 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
#!/usr/bin/env bash
# scripts/linux/setup.sh -- Core Linux/macOS/WSL installer (#468 flags-first)
#
# Called by: setup.sh (root entry point)
#
# Usage (direct):
# bash scripts/linux/setup.sh [--list] [--help] [--only=a,b] [--skip=a,b]
# [--interactive | --non-interactive]
#
# Flags:
# --list Print available tools (alphabetical), exit 0. No install.
# --help Print usage, exit 0.
# --only=a,b,c Install ONLY the listed tools (comma-separated).
# --skip=a,b,c Install all default tools EXCEPT the listed ones.
# --interactive Show the tool picker when a TTY is available.
# --non-interactive
# Never show the tool picker.
# --only and --skip are mutually exclusive.
#
# Hidden test seam (not in --help):
# --tools-dir=<path> Override the tools directory (test use only).
# --selection-file=<path>
# Read selected tools from a file (test use only).
set -euo pipefail
exec 2>&1 # Merge stderr into stdout for ordered output in piped/Devcontainer environments
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TOOLS_DIR="${SCRIPT_DIR}/tools"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh"
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")/lib/tui.sh"
# ---------------------------------------------------------------------------
# DEFAULT_TOOLS -- single ordered source of truth for a no-arg default run.
# Adding a .sh file to tools/ makes it AvailableTools (selectable via --only)
# but does NOT add it here. Promotion to default requires editing this array.
# ---------------------------------------------------------------------------
DEFAULT_TOOLS=(
"prereqs"
"zsh"
"nvm"
"gh"
"auth"
"copilot-cli"
"dotfiles"
"git-hook"
)
# ---------------------------------------------------------------------------
# Arg parsing
# ---------------------------------------------------------------------------
ARG_ONLY=""
ARG_SKIP=""
ARG_LIST=0
ARG_HELP=0
ARG_TOOLS_DIR="" # hidden test seam
ARG_SELECTION_FILE="" # hidden test seam
ARG_ONLY_SET=0 # tracks whether --only was explicitly provided
ARG_SKIP_SET=0 # tracks whether --skip was explicitly provided
ARG_INTERACTIVE_SET=0
ARG_NON_INTERACTIVE_SET=0
ARG_SELECTION_FILE_SET=0
for arg in "$@"; do
case "$arg" in
--only=*) ARG_ONLY="${arg#--only=}"; ARG_ONLY_SET=1 ;;
--skip=*) ARG_SKIP="${arg#--skip=}"; ARG_SKIP_SET=1 ;;
--list) ARG_LIST=1 ;;
--help) ARG_HELP=1 ;;
--interactive) ARG_INTERACTIVE_SET=1 ;;
--non-interactive) ARG_NON_INTERACTIVE_SET=1 ;;
--tools-dir=*) ARG_TOOLS_DIR="${arg#--tools-dir=}" ;;
--selection-file=*)
ARG_SELECTION_FILE="${arg#--selection-file=}"
ARG_SELECTION_FILE_SET=1
;;
*)
log_error "Unknown argument: $arg"
log_error "Run with --help for usage."
exit 1
;;
esac
done
# Override TOOLS_DIR from test seam if provided
if [[ -n "$ARG_TOOLS_DIR" ]]; then
TOOLS_DIR="$ARG_TOOLS_DIR"
# Load DEFAULT_TOOLS from defaults.txt in the stub dir (test self-contained)
# Use while-read instead of mapfile -- bash 3.2 (macOS) does not have mapfile
if [[ -f "${TOOLS_DIR}/defaults.txt" ]]; then
DEFAULT_TOOLS=()
while IFS= read -r _line; do
[[ -n "$_line" ]] && DEFAULT_TOOLS+=("$_line")
done < "${TOOLS_DIR}/defaults.txt"
fi
fi
# ---------------------------------------------------------------------------
# Dispatcher
# ---------------------------------------------------------------------------
run_tool() {
local tool_name="$1"
local tool_script="${TOOLS_DIR}/${tool_name}.sh"
if [[ ! -f "$tool_script" ]]; then
log_warn "Tool script not found, skipping: ${tool_script}"
return 0
fi
log_info "Installing: ${tool_name}"
# shellcheck source=/dev/null
if bash "${tool_script}"; then
log_ok "${tool_name} -- done"
else
log_error "${tool_name} -- FAILED (see above)"
return 1
fi
}
# Build AvailableTools: basenames of all *.sh in TOOLS_DIR
get_available_tools() {
local tool
for tool in "${TOOLS_DIR}"/*.sh; do
[[ -f "$tool" ]] && basename "$tool" .sh
done | sort
}
# Validate a CSV list: no blanks, no empty tokens, each name in AvailableTools
validate_csv_shape() {
local input="$1"
if [[ -z "$input" ]]; then
log_error "Flag requires at least one tool name."
exit 1
fi
if [[ "$input" == *,,* || "$input" == ,* || "$input" == *, ]]; then
log_error "Empty tool name in list (check commas)."
exit 1
fi
}
print_help() {
cat <<'USAGE'
Usage: setup.sh [OPTIONS]
Install developer tools on Linux/macOS/WSL.
Options:
--list Print available tools (alphabetical order), exit 0.
--help Print this help message, exit 0.
--only=a,b,c Install ONLY the listed tools (comma-separated).
--skip=a,b,c Install all default tools EXCEPT the listed ones.
--interactive Show the tool picker when a TTY is available.
--non-interactive Never show the tool picker.
Notes:
--only and --skip are mutually exclusive.
Unknown tool names exit with an error and print the available tool list.
A no-arg run installs all default tools in the defined order.
USAGE
}
# ---------------------------------------------------------------------------
# --help / --list handling (exit before any install logic)
# ---------------------------------------------------------------------------
if [[ $ARG_HELP -eq 1 ]]; then
print_help
exit 0
fi
if [[ $ARG_LIST -eq 1 ]]; then
get_available_tools
exit 0
fi
# ---------------------------------------------------------------------------
# Mutual exclusion
# ---------------------------------------------------------------------------
if [[ $ARG_ONLY_SET -eq 1 && $ARG_SKIP_SET -eq 1 ]]; then
log_error "--only and --skip are mutually exclusive."
exit 1
fi
if [[ $ARG_INTERACTIVE_SET -eq 1 && $ARG_NON_INTERACTIVE_SET -eq 1 ]]; then
log_error "--interactive and --non-interactive are mutually exclusive."
exit 1
fi
if [[ $ARG_NON_INTERACTIVE_SET -eq 1 && $ARG_SELECTION_FILE_SET -eq 1 ]]; then
log_error "--non-interactive and --selection-file are mutually exclusive."
exit 1
fi
# (Note: ARG_ONLY_SET / ARG_SKIP_SET handle empty-value sentinels; build_final_toolset validates.)
# ---------------------------------------------------------------------------
# Interactive guard. Returns 0 (true) when the menu should run.
# ---------------------------------------------------------------------------
is_interactive() {
if [[ $ARG_NON_INTERACTIVE_SET -eq 1 || $ARG_ONLY_SET -eq 1 || $ARG_SKIP_SET -eq 1 ]]; then
return 1
fi
# --interactive + --selection-file: bypass CI/TTY detection so CI can test the menu path.
if [[ $ARG_INTERACTIVE_SET -eq 1 && $ARG_SELECTION_FILE_SET -eq 1 ]]; then
return 0
fi
if [[ "${SETUP_NON_INTERACTIVE:-}" == "1" || -n "${CI:-}" || -n "${GITHUB_ACTIONS:-}" ]]; then
return 1
fi
if [[ ! -t 0 || ! -t 1 ]]; then
return 1
fi
return 0
}
# ---------------------------------------------------------------------------
# Build FinalToolSet -- populates global FINAL_TOOLS (bash 3.2 safe: no
# local -n namerefs, no mapfile; use plain global array + while-read).
# ---------------------------------------------------------------------------
FINAL_TOOLS=()
build_final_toolset() {
FINAL_TOOLS=()
if [[ $ARG_SELECTION_FILE_SET -eq 1 && $ARG_ONLY_SET -eq 0 && $ARG_SKIP_SET -eq 0 ]]; then
if [[ -z "$ARG_SELECTION_FILE" || ! -f "$ARG_SELECTION_FILE" ]]; then
log_error "Selection file not found: ${ARG_SELECTION_FILE}"
exit 1
fi
local _selection
while IFS= read -r _selection || [[ -n "$_selection" ]]; do
_selection="${_selection%$'\r'}"
[[ -z "$_selection" ]] && continue
if [[ -n "$ARG_ONLY" ]]; then
ARG_ONLY="${ARG_ONLY},${_selection}"
else
ARG_ONLY="$_selection"
fi
done < "$ARG_SELECTION_FILE"
ARG_ONLY_SET=1
fi
# Build available-tools list into a local array (while-read, not mapfile)
local available=()
local _t
while IFS= read -r _t; do
[[ -n "$_t" ]] && available+=("$_t")
done < <(get_available_tools)
if [[ $ARG_ONLY_SET -eq 1 ]]; then
validate_csv_shape "$ARG_ONLY"
local only_list=()
IFS=',' read -ra only_list <<< "$ARG_ONLY"
# Validate each name against AvailableTools
local name avail found
for name in "${only_list[@]}"; do
found=0
for avail in "${available[@]}"; do
[[ "$avail" == "$name" ]] && found=1 && break
done
if [[ $found -eq 0 ]]; then
log_error "Unknown tool: ${name}"
log_error "Available tools: $(get_available_tools | tr '\n' ' ')"
log_error "Use --list to see all available tools."
exit 1
fi
done
# ORDER PRESERVATION: iterate DEFAULT_TOOLS, include those requested.
# Do NOT use input order -- dependencies require the default sequence.
local tool _in_only
for tool in "${DEFAULT_TOOLS[@]}"; do
_in_only=0
for name in "${only_list[@]}"; do
[[ "$name" == "$tool" ]] && _in_only=1 && break
done
[[ $_in_only -eq 1 ]] && FINAL_TOOLS+=("$tool")
done
# Opt-in tools (requested but NOT in DEFAULT_TOOLS): append alphabetically.
# These have no defined default position, so alphabetical is deterministic.
local _in_default
local _optin_names
_optin_names=()
for name in "${only_list[@]}"; do
_in_default=0
for tool in "${DEFAULT_TOOLS[@]}"; do
[[ "$tool" == "$name" ]] && _in_default=1 && break
done
[[ $_in_default -eq 0 ]] && _optin_names+=("$name")
done
if [[ ${#_optin_names[@]} -gt 0 ]]; then
local _sorted_optin
_sorted_optin=()
while IFS= read -r _t; do
[[ -n "$_t" ]] && _sorted_optin+=("$_t")
done < <(printf '%s\n' "${_optin_names[@]}" | sort)
FINAL_TOOLS+=("${_sorted_optin[@]}")
fi
elif [[ $ARG_SKIP_SET -eq 1 ]]; then
validate_csv_shape "$ARG_SKIP"
local skip_list=()
IFS=',' read -ra skip_list <<< "$ARG_SKIP"
# Validate each skip name against AvailableTools
local name avail found
for name in "${skip_list[@]}"; do
found=0
for avail in "${available[@]}"; do
[[ "$avail" == "$name" ]] && found=1 && break
done
if [[ $found -eq 0 ]]; then
log_error "Unknown tool: ${name}"
log_error "Available tools: $(get_available_tools | tr '\n' ' ')"
exit 1
fi
done
# Filter DEFAULT_TOOLS, preserving order
local tool skip s
for tool in "${DEFAULT_TOOLS[@]}"; do
skip=0
for s in "${skip_list[@]}"; do
[[ "$s" == "$tool" ]] && skip=1 && break
done
if [[ $skip -eq 0 ]]; then
FINAL_TOOLS+=("$tool")
fi
done
else
FINAL_TOOLS=("${DEFAULT_TOOLS[@]}")
fi
}
# ---------------------------------------------------------------------------
# Platform detection (kept for logging; prereqs.sh re-detects internally)
# ---------------------------------------------------------------------------
detect_platform() {
local os
os="$(uname -s)"
if [[ "$os" == "Darwin" ]]; then
echo "macos"
elif grep -qi microsoft /proc/version 2>/dev/null; then
echo "wsl"
else
echo "linux"
fi
}
# ---------------------------------------------------------------------------
# Main dispatch
# ---------------------------------------------------------------------------
main() {
local platform
platform="$(detect_platform)"
log_info "Starting Linux/macOS setup"
log_info "Platform: ${platform}"
log_info "Repo root: ${REPO_ROOT}"
if is_interactive; then
if [[ $ARG_SELECTION_FILE_SET -eq 1 ]]; then
# CI/test seam: read newline-delimited selection from file
_MENU_SELECTION=""
_MENU_CANCELLED=0
local _sel_line
while IFS= read -r _sel_line || [[ -n "$_sel_line" ]]; do
_sel_line="${_sel_line%$'\r'}"
[[ -z "$_sel_line" ]] && continue
_MENU_SELECTION="${_MENU_SELECTION:+${_MENU_SELECTION},}${_sel_line}"
done < "$ARG_SELECTION_FILE"
else
local _avail_for_menu=()
local _t
while IFS= read -r _t; do
[[ -n "$_t" ]] && _avail_for_menu+=("$_t")
done < <(get_available_tools)
show_tool_menu DEFAULT_TOOLS _avail_for_menu
fi
if [[ "${_MENU_CANCELLED:-0}" -eq 1 ]]; then
exit 0
fi
if [[ -z "${_MENU_SELECTION:-}" ]]; then
log_info "Nothing selected, exiting."
exit 0
fi
ARG_ONLY="$_MENU_SELECTION"
ARG_ONLY_SET=1
fi
build_final_toolset
local tool
for tool in "${FINAL_TOOLS[@]}"; do
run_tool "$tool"
done
log_ok "Setup complete. Open a new shell to apply all changes."
}
main