-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotctl
More file actions
executable file
·418 lines (350 loc) · 10.7 KB
/
dotctl
File metadata and controls
executable file
·418 lines (350 loc) · 10.7 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
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
readonly REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
readonly LAUNCH_AGENTS_SOURCE="$HOME/.config/launch-agents"
readonly CRONS_DIR="$REPO_ROOT/.shell/scripts/crons"
readonly MACOS_SCRIPT="$REPO_ROOT/.shell/scripts/macos/defaults.sh"
readonly REQUIRED_TOOLS=("brew" "stow")
# ─── Help ──────────────────────────────────────────────────────────────── {{{
usage() {
cat <<EOF
Usage: ./dotctl <command> [options]
Running without a command prints this help. Every action is explicit.
Commands:
all Run stow, load launch agents, install crons,
apply macOS defaults
stow [--adopt] Symlink dotfiles into \$HOME via stow
agents <load|unload|list>
Manage launch agents under ~/.config/launch-agents
crons <install|list>
Orchestrate per-cron install.sh hooks under
.shell/scripts/crons/*/install.sh
macos <apply> Apply macOS \`defaults write\` tweaks
(.shell/scripts/macos/defaults.sh)
Global:
-h, --help Show this help
Per-command help:
./dotctl <command> -h
Examples:
./dotctl all # stow + agents load + crons install + macos apply
./dotctl stow --adopt # adopt existing files as stow-managed
./dotctl agents unload # unload all launch agents
./dotctl crons list # list discovered cron install hooks
./dotctl macos apply # apply macOS defaults tweaks
EOF
}
usage_stow() {
cat <<EOF
Usage: ./dotctl stow [--adopt]
Symlinks this repo into \$HOME via GNU stow. Cleans stale symlinks first so
renamed/removed files don't linger.
Options:
--adopt Run \`stow --adopt\`: files already in \$HOME that shadow repo files
get moved into the repo and replaced with symlinks. Use with care
— review \`git status\` afterwards.
EOF
}
usage_agents() {
cat <<EOF
Usage: ./dotctl agents <load|unload|list>
Manages user LaunchAgents. Each *.plist in ~/.config/launch-agents/ is
symlinked into ~/Library/LaunchAgents/ and bootstrapped via launchctl.
Subcommands:
load Symlink plists and bootstrap (no-op if already loaded)
unload Bootout and remove the symlinks
list Show each plist + whether it's currently loaded
EOF
}
usage_crons() {
cat <<EOF
Usage: ./dotctl crons <install|list>
Orchestrates per-cron install hooks. Any .shell/scripts/crons/*/install.sh
is auto-discovered; drop a new cron folder with its own install.sh and it
will be picked up on the next run.
Subcommands:
install Run every discovered install.sh (may prompt for sudo)
list Show discovered cron dirs and whether they have install.sh
Notes:
- install.sh is invoked with cwd set to its own directory.
- Failures in one script do not abort the others; exit code reflects
whether any failed.
EOF
}
usage_macos() {
cat <<EOF
Usage: ./dotctl macos <apply>
Applies personal macOS \`defaults write\` tweaks (Finder, Dock, trackpad,
keyboard, Safari, Mail, …) defined in:
.shell/scripts/macos/defaults.sh
Subcommands:
apply Run the defaults script. Idempotent. Relaunches Dock, Finder,
SystemUIServer, and cfprefsd so changes take effect immediately.
Notes:
- No-op on non-Darwin systems.
- Edit the script directly to add/remove tweaks; it IS the documentation.
EOF
}
# }}}
# ─── Helpers ───────────────────────────────────────────────────────────── {{{
_command_installed() {
command -v "$1" &>/dev/null
}
_install_brew() {
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew update
}
_require_tools() {
for tool in "${REQUIRED_TOOLS[@]}"; do
if ! _command_installed "$tool" && [[ "$tool" == "brew" ]]; then
_install_brew
elif ! _command_installed "$tool"; then
brew install "$tool"
fi
done
}
_clean_stale_symlinks() {
local pkg resolved rel home_path
pkg="$REPO_ROOT"
while IFS= read -r -d '' entry; do
rel="${entry#$pkg/}"
home_path="$HOME/$rel"
[[ -L "$home_path" ]] || continue
resolved=$(readlink -f "$home_path" 2>/dev/null || true)
if [[ "$resolved" != "$entry" ]]; then
rm -v "$home_path"
fi
done < <(find "$pkg" -mindepth 1 \
\( -path "$pkg/.git" -o -path "$pkg/.git/*" \) -prune -o \
\( -type f -o -type d \) -print0)
}
_agent_is_loaded() {
local label="$1" uid
uid=$(id -u)
launchctl print "gui/$uid/$label" &>/dev/null
}
# }}}
# ─── stow ──────────────────────────────────────────────────────────────── {{{
cmd_stow() {
local adopt=0
while [[ $# -gt 0 ]]; do
case "$1" in
--adopt)
adopt=1
shift
;;
-h | --help)
usage_stow
return 0
;;
*)
echo "stow: unknown option: $1" >&2
usage_stow >&2
return 1
;;
esac
done
_require_tools
_clean_stale_symlinks
if ((adopt)); then
stow -v -t "$HOME" -d "$REPO_ROOT" . --adopt
else
stow -v -t "$HOME" -d "$REPO_ROOT" .
fi
}
# }}}
# ─── agents ────────────────────────────────────────────────────────────── {{{
_agents_load() {
[[ -d "$LAUNCH_AGENTS_SOURCE" ]] || {
echo "No launch-agents dir; skipping."
return 0
}
local uid filename label
uid=$(id -u)
for file in "$LAUNCH_AGENTS_SOURCE"/*.plist; do
filename=$(basename "$file")
label="${filename%.plist}"
ln -sfv "$file" "$HOME/Library/LaunchAgents/$filename"
launchctl bootstrap "gui/$uid" "$HOME/Library/LaunchAgents/$filename" ||
echo " note: $label may already be loaded"
done
}
_agents_unload() {
[[ -d "$LAUNCH_AGENTS_SOURCE" ]] || {
echo "No launch-agents dir; skipping."
return 0
}
local uid filename label
uid=$(id -u)
for file in "$LAUNCH_AGENTS_SOURCE"/*.plist; do
filename=$(basename "$file")
label="${filename%.plist}"
if [[ -e "$HOME/Library/LaunchAgents/$filename" || -L "$HOME/Library/LaunchAgents/$filename" ]]; then
launchctl bootout "gui/$uid/$label" ||
echo " note: $label may already be unloaded"
rm -fv "$HOME/Library/LaunchAgents/$filename"
fi
done
}
_agents_list() {
[[ -d "$LAUNCH_AGENTS_SOURCE" ]] || {
echo "No launch-agents dir."
return 0
}
local filename label status
printf '%-40s %s\n' "LABEL" "STATUS"
printf '%-40s %s\n' "-----" "------"
for file in "$LAUNCH_AGENTS_SOURCE"/*.plist; do
filename=$(basename "$file")
label="${filename%.plist}"
if _agent_is_loaded "$label"; then
status="loaded"
else
status="unloaded"
fi
printf '%-40s %s\n' "$label" "$status"
done
}
cmd_agents() {
local sub="${1:-}"
[[ -n "$sub" ]] || {
usage_agents >&2
return 1
}
shift
case "$sub" in
load) _agents_load ;;
unload) _agents_unload ;;
list) _agents_list ;;
-h | --help) usage_agents ;;
*)
echo "agents: unknown subcommand: $sub" >&2
usage_agents >&2
return 1
;;
esac
}
# }}}
# ─── crons ─────────────────────────────────────────────────────────────── {{{
_discover_crons() {
[[ -d "$CRONS_DIR" ]] || return 0
for dir in "$CRONS_DIR"/*/; do
[[ -d "$dir" ]] && printf '%s\n' "${dir%/}"
done
}
_crons_install() {
local dir name install_path failed=0 ran=0
while IFS= read -r dir; do
name=$(basename "$dir")
install_path="$dir/install.sh"
if [[ -x "$install_path" || -f "$install_path" ]]; then
echo "── installing cron: $name ──"
if (cd "$dir" && bash "$install_path"); then
ran=$((ran + 1))
else
echo " ✗ $name/install.sh failed" >&2
failed=$((failed + 1))
fi
fi
done < <(_discover_crons)
echo
echo "Cron install summary: $ran succeeded, $failed failed."
return "$failed"
}
_crons_list() {
local dir name
printf '%-30s %s\n' "CRON" "HAS install.sh"
printf '%-30s %s\n' "----" "--------------"
while IFS= read -r dir; do
name=$(basename "$dir")
if [[ -f "$dir/install.sh" ]]; then
printf '%-30s %s\n' "$name" "yes"
else
printf '%-30s %s\n' "$name" "no"
fi
done < <(_discover_crons)
}
cmd_crons() {
local sub="${1:-}"
[[ -n "$sub" ]] || {
usage_crons >&2
return 1
}
shift
case "$sub" in
install) _crons_install ;;
list) _crons_list ;;
-h | --help) usage_crons ;;
*)
echo "crons: unknown subcommand: $sub" >&2
usage_crons >&2
return 1
;;
esac
}
# }}}
# ─── macos ─────────────────────────────────────────────────────────────── {{{
_macos_apply() {
[[ "$(uname -s)" == "Darwin" ]] || {
echo "macos: skipping — not on Darwin"
return 0
}
[[ -f "$MACOS_SCRIPT" ]] || {
echo "macos: $MACOS_SCRIPT not found" >&2
return 1
}
bash "$MACOS_SCRIPT"
}
cmd_macos() {
local sub="${1:-}"
[[ -n "$sub" ]] || {
usage_macos >&2
return 1
}
shift
case "$sub" in
apply) _macos_apply ;;
-h | --help) usage_macos ;;
*)
echo "macos: unknown subcommand: $sub" >&2
usage_macos >&2
return 1
;;
esac
}
# }}}
# ─── all ───────────────────────────────────────────────────────────────── {{{
cmd_all() {
cmd_stow "$@"
echo
_agents_load
echo
_crons_install || echo " (some cron installs failed — see above)"
echo
_macos_apply || echo " (macos apply failed — see above)"
}
# }}}
# ─── dispatch ──────────────────────────────────────────────────────────── {{{
main() {
if [[ $# -eq 0 ]]; then
usage
exit 0
fi
local cmd="$1"
shift
case "$cmd" in
all) cmd_all "$@" ;;
stow) cmd_stow "$@" ;;
agents) cmd_agents "$@" ;;
crons) cmd_crons "$@" ;;
macos) cmd_macos "$@" ;;
-h | --help) usage ;;
*)
echo "Unknown command: $cmd" >&2
usage >&2
exit 1
;;
esac
}
main "$@"
# }}}