-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathupdate
More file actions
executable file
·685 lines (538 loc) · 23.2 KB
/
update
File metadata and controls
executable file
·685 lines (538 loc) · 23.2 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#!/usr/bin/env bash
set -euo pipefail
# Symlinks and copies files from the ~/.dotfiles directory into their
# correct locations: $HOME, $HOME/.config/fish, $HOME/.config/templates,
# etc.
# Show usage information
function show_help {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Symlinks and copies dotfiles from ~/.dotfiles to their correct locations.
OPTIONS:
-h, --help Show this help message and exit
EXAMPLES:
$(basename "$0") # Install dotfiles
EOF
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
show_help
exit 0
;;
-*)
echo "Error: Unknown option $1" >&2
show_help
exit 1
;;
*)
echo "Error: No arguments expected" >&2
show_help
exit 1
;;
esac
done
# Error handling wrapper
function safe_execute {
local description="$1"
shift
if ! "$@"; then
echo "Error: Failed to $description" >&2
echo "Command: $*" >&2
exit 1
fi
}
# Echoes, then runs the command.
#
# Combines/is inspired by:
#
# http://stackoverflow.com/q/12231792/11543
# https://dl.google.com/dl/cloudsdk/release/install_google_cloud_sdk.bash
# http://tim.theenchanter.com/2009/02/how-to-escape-arguments-in-bash.html?showComment=1321001720427#c4751431215627296974
function x {
#echo "$" "$@"
eval "$(printf '%q ' "$@")"
}
# Ensure directory exists
function xmkdir {
if [ ! -d "$1" ]; then
x mkdir -p "$1"
fi
}
# Returns success if found
function exists {
type -P "$1" >/dev/null
}
function heading {
printf '# %s\n' "$@"
}
# SRCDIR is the root of the git repo
# From http://stackoverflow.com/a/246128/11543
SRCDIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# DSTDIR is $HOME (parent of the git repo)
DSTDIR="$(cd -P "$(dirname "${SRCDIR}")" && pwd)"
# "dotfiles" that will end up in $HOME
for f in "$SRCDIR"/home/.*; do
if [ -d "$f" ]; then
continue
fi
if [ "$(basename "$f")" = ".DS_Store" ]; then
continue
fi
if [ ! -L "$DSTDIR/$(basename "$f")" ]; then
x ln -sf "$f" "$DSTDIR"
fi
done
# Remove dangling symlinks
for f in "$DSTDIR"/.*; do
if readlink "$f" >/dev/null; then
if [ ! -f "$(readlink "$f")" ]; then
x rm "$f"
fi
fi
done
# Set PLATFORM after $HOME/.platform has been symlinked
if [[ ! -x "$HOME/.platform" ]]; then
echo "Error: $HOME/.platform script not found or not executable" >&2
echo "Make sure dotfiles are properly symlinked first" >&2
exit 1
fi
PLATFORM=$("$HOME"/.platform)
if [[ -z "$PLATFORM" ]]; then
echo "Error: Failed to detect platform" >&2
exit 1
fi
# Ask for sudo upfront if we're likely to need it
if [ "$PLATFORM" = "linux" ]; then
export DEBIAN_FRONTEND="noninteractive"
# Check if sudo requires a password first
if sudo -n true 2>/dev/null; then
# Passwordless sudo is configured, no need to prompt
SUDO=0
else
echo "This script requires sudo access for package management."
echo "You may be prompted for your password."
# Good for 5 mins; for more see https://github.com/mathiasbynens/dotfiles/blob/master/.macos#L13
if ! sudo -v; then
echo "Error: Failed to obtain sudo access" >&2
exit 1
fi
SUDO=0
fi
fi
LOCAL="$HOME/.local"
xmkdir "$LOCAL"
BINDIR="$LOCAL/bin"
xmkdir "$BINDIR"
case "$PLATFORM" in
darwin)
heading "macos"
# For some reason *some* applications (like TextEdit) won't read
# DefaultKeyBinding.dict if it's symlinked, or is in a symlinked directory,
# so rsync instead of symlink... http://apple.stackexchange.com/a/53110/890
# rdar://12429092
x rsync -a --delete "$SRCDIR/etc/KeyBindings" "$DSTDIR/Library"
# https://github.com/mathiasbynens/dotfiles/blob/master/.macos
# https://github.com/hjuutilainen/dotfiles/blob/master/bin/osx-user-defaults.sh
# https://github.com/ymendel/dotfiles/tree/master/osx
# https://github.com/drduh/OS-X-Security-and-Privacy-Guide
# How to figure out the bundle identifier of an app:
#
# osascript -e 'id of app "Safari"'
# => com.apple.Safari
# How to figure out what setting to change (for fish shell):
#
# vimdiff (defaults read -g | psub) (read -p "echo 'Make change and press enter (:qa to exit vimdiff!) > '" ; and defaults read -g | psub)
# vimdiff (defaults read pro.writer.mac | psub) (read -p "echo 'Make change and press enter (:qa to exit vimdiff!) > '" ; and defaults read pro.writer.mac | psub)
# System: disable smart quotes
x defaults write -g NSAutomaticQuoteSubstitutionEnabled -bool false
# System: disable smart dashes
x defaults write -g NSAutomaticDashSubstitutionEnabled -bool false
# System: don't correct spelling automatically
x defaults write -g NSAutomaticSpellingCorrectionEnabled -bool false
x defaults write -g WebAutomaticSpellingCorrectionEnabled -bool false
# System: don't capitalize words automatically
x defaults write -g NSAutomaticCapitalizationEnabled -bool false
# System: don't convert double space to period + double space
x defaults write -g NSAutomaticPeriodSubstitutionEnabled -bool false
# System: don't show suggestions on the Touch Bar!
x defaults write -g NSAutomaticTextCompletionEnabled -bool false
# System: don't default to iCloud
x defaults write -g NSDocumentSaveNewDocumentsToCloud -bool false
# System: speed up mouse (requires logout to take effect?)
x defaults write -g com.apple.mouse.scaling -float 6
# System: enable right click
x defaults write com.apple.driver.AppleBluetoothMultitouch.mouse MouseButtonMode -string TwoButton
# System: show all extensions (really Finder?)
x defaults write -g AppleShowAllExtensions -int 1
# Finder: keep folders on top when sorting by name
x defaults write com.apple.finder _FXSortFoldersFirst -int 1
# Finder: show status bar
x defaults write com.apple.finder ShowStatusBar -bool true
# Finder: disable the warning when changing a file extension
x defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false
# Finder: new finder window shows home
x defaults write com.apple.finder NewWindowTarget -string PfHm
# Terminal: disable "marks" http://apple.stackexchange.com/a/209907/890
x defaults write com.apple.Terminal AutoMarkPromptLines -int 0
# Safari: show the full URL in the address bar (note: this still hides the scheme)
#x defaults write com.apple.Safari ShowFullURLInSmartSearchField -bool true
# Safari: enable debug menu
#x defaults write com.apple.Safari IncludeInternalDebugMenu -bool true
# Safari: enable develop menu
#x defaults write com.apple.Safari IncludeDevelopMenu -bool true
# Safari: show status bar
#x defaults write com.apple.Safari ShowStatusBar -bool true
# Photos: don't open automatically when external media is connected
x defaults -currentHost write com.apple.ImageCapture disableHotPlug -bool true
# Messages: no automatic emoji
x defaults write com.apple.messageshelper.MessageController SOInputLineSettings -dict-add "automaticEmojiSubstitutionEnablediMessage" -bool false
# Messages: no smart quotes
x defaults write com.apple.messageshelper.MessageController SOInputLineSettings -dict-add "automaticQuoteSubstitutionEnabled" -bool false
# Messages: no smart dashes
x defaults write com.apple.messageshelper.MessageController SOInputLineSettings -dict-add "automaticDashSubstitutionEnabled" -bool false
# Messages: no automatic spell correction
x defaults write com.apple.messageshelper.MessageController SOInputLineSettings -dict-add "automaticSpellingCorrectionEnabled" -bool false
# Spotlight: configure categories
# defaults read com.apple.Spotlight orderedItems | sed -E -e 's/^[[:space:]]+//g' | tr -d '()\n' | tr ',' '\n' | awk -F "\n" '{ print "\'" $1 "\' \\\" }'
x defaults write com.apple.Spotlight orderedItems -array \
'{enabled = 1;name = APPLICATIONS;}' \
'{enabled = 0;name = "MENU_SPOTLIGHT_SUGGESTIONS";}' \
'{enabled = 1;name = "MENU_CONVERSION";}' \
'{enabled = 1;name = "MENU_EXPRESSION";}' \
'{enabled = 1;name = "MENU_DEFINITION";}' \
'{enabled = 1;name = "SYSTEM_PREFS";}' \
'{enabled = 0;name = DOCUMENTS;}' \
'{enabled = 0;name = DIRECTORIES;}' \
'{enabled = 0;name = PRESENTATIONS;}' \
'{enabled = 0;name = SPREADSHEETS;}' \
'{enabled = 0;name = PDF;}' \
'{enabled = 0;name = MESSAGES;}' \
'{enabled = 1;name = CONTACT;}' \
'{enabled = 0;name = "EVENT_TODO";}' \
'{enabled = 0;name = IMAGES;}' \
'{enabled = 0;name = BOOKMARKS;}' \
'{enabled = 0;name = MUSIC;}' \
'{enabled = 0;name = MOVIES;}' \
'{enabled = 0;name = FONTS;}' \
'{enabled = 0;name = "MENU_OTHER";}' \
'{enabled = 0;name = "MENU_WEBSEARCH";}' \
'{enabled = 0;name = SOURCE;}'
# Adjust keybindings
# NSUserKeyEquivalent modifiers:
#
# Command: @
# Control: ^
# Option: ~
# Shift: $
# Tab: \U21e5 (Unicode code point for ⇥ character)
# System/Global: try to default to "Paste and Match Style" on cmd-v http://apple.stackexchange.com/a/167649/890
x defaults write -g NSUserKeyEquivalents -dict-add "Paste and Match Style" -string "@v"
# iA Writer: bind cmd-n to new file, instead of new file in library
x defaults write pro.writer.mac NSUserKeyEquivalents -dict-add "New" -string "@n"
# iA Writer: change default extension
x defaults write pro.writer.mac "Document Path Extension" -string "md"
# iA Writer: disable night mode
x defaults write pro.writer.mac nightMode -int 0
# ia Writer: indent using spaces
x defaults write pro.writer.mac "Editor Indent Using Spaces" -int 1
# iA Writer: use "Quattro" font
x defaults write pro.writer.mac "Editor Typeface" -int 2
# iA Writer: in preview, don't center headings
x defaults write pro.writer.mac "Preview Center Headings" -int 0
# iA Writer: in preview, apply smart punctuation
x defaults write pro.writer.mac "Markdown Smart Punctuation Enabled" -int 1
# iA Writer: show filename extensions
x defaults write pro.writer.mac "Library Show Extension" -int 1
# iA Writer: configure sidebar
x defaults write pro.writer.mac "Library Shows Search Bar" -int 0
x defaults write pro.writer.mac "Organizer Shows Favorites" -int 0
x defaults write pro.writer.mac "Organizer Shows Hashtags" -int 0
x defaults write pro.writer.mac "Organizer Shows Smart Folders" -int 0
# Firefox, Safari: bind cmd-opt-j to "Web Console", to match Chrome (and muscle memory...)
x defaults write org.mozilla.firefox NSUserKeyEquivalents -dict-add "Web Console" -string "@~j"
#x defaults write com.apple.Safari NSUserKeyEquivalents -dict-add "Show Error Console" -string "@~j"
if [ ! -e "$HOME/iCloud" ] && [ -d "$HOME/Library/Mobile Documents/com~apple~CloudDocs" ]; then
x ln -s "$HOME/Library/Mobile Documents/com~apple~CloudDocs" "$HOME/iCloud"
fi
# https://github.com/altercation/ethanschoonover.com/tree/master/projects/solarized/apple-colorpalette-solarized
if [ ! -f "$DSTDIR/Library/Colors/Solarized.clr" ]; then
x cp "$SRCDIR/etc/Solarized.clr" "$DSTDIR/Library/Colors"
fi
# https://plus.google.com/+RomanNurik/posts/4VwE8RrXsGd
if [ ! -f "$DSTDIR/Library/Colors/Material-Design.clr" ]; then
x cp "$SRCDIR/etc/Material-Design.clr" "$DSTDIR/Library/Colors"
fi
# Let's just assume Services is special and copy instead of symlink...
x rsync -a --delete "$SRCDIR/etc/Services/" "$DSTDIR/Library/Services/"
if [[ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport ]]; then
x ln -sf /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport "$BINDIR"
fi
if [[ -x "$(which networkQuality)" ]]; then
x ln -sf "$(which networkQuality)" "$BINDIR/speedtest"
fi
# if [[ -x /Applications/Tailscale.app/Contents/MacOS/Tailscale ]]; then
# x ln -sf /Applications/Tailscale.app/Contents/MacOS/Tailscale "$BINDIR/tailscale"
# fi
if [[ -x "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" ]]; then
x ln -sf "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" "$BINDIR/code"
fi
;;
esac
# Fonts
if [ ! -L "$HOME/.config/fontconfig" ]; then
heading "fonts"
if [ -d "$HOME/.config/fontconfig" ]; then
x rm -rf "$HOME/.config/fontconfig"
fi
xmkdir "$HOME/.config"
x ln -s "$SRCDIR/etc/fontconfig" "$HOME/.config/fontconfig"
fi
# fish
heading "fish"
# Fish configuration needs to be in ~/.config/fish
if [ ! -L "$HOME/.config/fish" ]; then
if [ -d "$HOME/.config/fish" ]; then
x rm -rf "$HOME/.config/fish"
fi
xmkdir "$HOME/.config"
x ln -s "$SRCDIR/fish" "$HOME/.config/fish"
fi
# Update completions if completions more than 7 days old
if exists fish; then
# shellcheck disable=SC2016 # Variable expands in fish shell, not bash
fish_completions_dir=$(fish -c 'echo $__fish_cache_dir/generated_completions' 2>/dev/null)
if [[ -n "$fish_completions_dir" ]] && ! find "$fish_completions_dir" -maxdepth 0 -mtime -7 2>/dev/null | grep -q .; then
echo "Updating fish completions..."
safe_execute "update fish completions" fish -c fish_update_completions
fi
fi
# shpool
if exists shpool; then
heading "shpool"
if [ ! -L "$HOME/.config/shpool/config.toml" ]; then
xmkdir "$HOME/.config/shpool"
x rm -rf "$HOME/.config/shpool/config.toml"
x ln -s "$SRCDIR/etc/shpool/config.toml" "$HOME/.config/shpool/config.toml"
fi
fi
# starship
if exists starship; then
heading "starship"
if [ ! -L "$HOME/.config/starship.toml" ]; then
x rm -rf "$HOME/.config/starship.toml"
x ln -s "$SRCDIR/etc/starship/starship.toml" "$HOME/.config/starship.toml"
fi
fi
# ghostty
if exists ghostty; then
heading "ghostty"
if [ ! -L "$HOME/.config/ghostty/config" ]; then
xmkdir "$HOME/.config/ghostty"
x rm -rf "$HOME/.config/ghostty/config"
x ln -s "$SRCDIR/etc/ghostty/config" "$HOME/.config/ghostty/config"
fi
fi
# Scripts for $LOCAL/bin
if exists brew; then
heading "brew"
safe_execute "disable brew analytics" brew analytics off
echo "Updating brew (this may prompt for Xcode license agreement)..."
if ! brew update; then
echo "Error: brew update failed. You may need to agree to Xcode license." >&2
echo "Try running 'sudo xcodebuild -license accept' if needed." >&2
exit 1
fi
# The non-HEAD version is years old...
if ! brew leaves | grep -q jed; then
x brew install jed --HEAD
fi
expected="fish coreutils dict tig wget direnv entr jq pv prettyping exiftool mtr htop pwgen pidcat shellcheck mosh shfmt yazi fzf sevenzip ripgrep viu"
# These packages have non-standard installation mechanisms (see above)
custom="jed"
# These packages are optional (don't remove if present)
optional="imagemagick-full yt-dlp go ffmpeg apktool git composer protobuf bundletool scrcpy git-lfs llm firebase-cli uv mosquitto codex gh"
# These packages are versioned packages that need to be force-linked
# (typically used when a specific version is needed instead of the default)
# To completely remove a versioned package: brew remove --force node@24
versioned="node@24" # e.g. node@24
comm -13 <(brew leaves | sort) <(echo "$expected" | tr ' ' '\n' | sort) | xargs -n 1 brew install
# Install versioned packages (specific versions for compatibility/requirements)
for pkg in $versioned; do
if ! brew list "$pkg" >/dev/null 2>&1; then
x brew install "$pkg"
fi
# Force link versioned packages to make them available in PATH
# This may override the default version of the package
if ! brew link --dry-run "$pkg" 2>&1 | grep -q "Already linked"; then
x brew link --force --overwrite "$pkg"
fi
done
# Special handling for imagemagick-full (needs manual linking due to conflicts)
if brew list imagemagick-full >/dev/null 2>&1; then
if ! brew link --dry-run imagemagick-full 2>&1 | grep -q "Already linked"; then
x brew link --overwrite imagemagick-full
fi
fi
# Upgrade all packages
brew upgrade
comm -23 <(brew leaves | sort) <(echo "$expected" "$custom" "$optional" "$versioned" | tr ' ' '\n' | sort) | xargs -n 1 brew remove
# Final cleanup of unused dependencies and cache
brew autoremove
brew cleanup
# Install Zulu JDK 17 (needed for some projects, such as https://github.com/android/snippets)
# brew install --cask zulu@17
# Usage:
# JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/ ./gradlew ...
fi
# apt-get
if exists apt-get && [ "$SUDO" -ne 1 ]; then
heading "apt-get"
x sudo apt-get -y update # refresh package lists
expected="apt-file direnv command-not-found dnsutils apache2-utils htop iftop iotop lsof mosh traceroute mtr-tiny whois sysstat dstat hdparm psmisc locate wget pv zip unzip libxml2-utils jed sqlite3 jq entr ripgrep nodejs"
comm -13 <(dpkg-query -f '${binary:Package}\n' -W | sort) <(echo "$expected" | tr ' ' '\n' | sort) | xargs sudo apt-get -y install
# Can't remove any packages because not possible to determine which were
# user-installed. Use `apt-get remove` to remove manually.
x sudo apt-get -y autoremove --purge # remove unnecessary dependencies and configs
x sudo apt-get -y autoclean # remove old (unneeded) cached .deb files
# x sudo apt-get -y clean # remove current .deb files
# x sudo apt-get -y upgrade # update packages, if no new dependencies needed
x sudo apt-get -y full-upgrade # update packages, including ones that require new dependencies
if [ -f /var/run/reboot-required ]; then
echo "Warning: A reboot is required to complete updates."
if [ -f /var/run/reboot-required.pkgs ]; then
echo "Packages requiring reboot:"
sed 's/^/ /' /var/run/reboot-required.pkgs
fi
fi
fi
heading "git"
if [ "$PLATFORM" = "darwin" ]; then
git config --file ~/.gitconfig.local credential.helper osxkeychain
fi
heading "vscode"
# Don't use system tools to update VS Code
if [ -e /etc/apt/sources.list.d/vscode.list ] && [ "$SUDO" -ne 1 ]; then
x sudo rm -f /etc/apt/sources.list.d/vscode.list
fi
if ! exists code; then
if [ -d "/Applications/Visual Studio Code.app" ]; then
x ln -sf "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" "$BINDIR/code"
fi
fi
if exists code; then
if [ "$PLATFORM" = "linux" ]; then
DST="$DSTDIR/.config/Code/User"
else
DST="$DSTDIR/Library/Application Support/Code/User"
fi
xmkdir "$DST"
for f in "$SRCDIR"/etc/code/*; do
if [ ! -L "$DST/$(basename "$f")" ]; then
x rm -rf "$DST/$(basename "$f")"
x ln -sf "$f" "$DST"
fi
done
# Let's try Settings Sync for now https://code.visualstudio.com/docs/editor/settings-sync
# expected=$(echo "ms-vscode.vscode-typescript-tslint-plugin golang.go stkb.rewrap esbenp.prettier-vscode eg2.vscode-npm-script GitHub.vscode-pull-request-github DavidAnson.vscode-markdownlint github.github-vscode-theme" | tr '[:upper:]' '[:lower:]')
# actual=$(code --list-extensions | perl -pe '$_ = lc; chomp if eof' | tr '\n' ' ') # perl to remove trailing newline
# # can't use xargs because GNU xargs need -r; macOS xargs does -r by default, but rejects the switch
# for ext in $(comm -23 <(echo "$actual" | tr ' ' '\n' | sort) <(echo "$expected" | tr ' ' '\n' | sort)); do
# code --uninstall-extension "$ext"
# done
# for ext in $(comm -13 <(echo "$actual" | tr ' ' '\n' | sort) <(echo "$expected" | tr ' ' '\n' | sort)); do
# code --install-extension "$ext"
# done
fi
if exists npm; then
heading "npm"
x npm config set prefix "$HOME/.local"
# Packages are installed into $(npm config get prefix)
# Install expected packages if missing
expected="@google/gemini-cli markdownlint-cli2 prettier repomix"
# Update existing packages (skipping symlinked local packages)
if exists jq; then
# We use jq to filter out packages where 'resolved' starts with 'file:' (symlinks).
# We ignore errors from npm list (e.g. peer dep issues) so the pipe doesn't fail.
npm_list_json=$(npm list -g --depth=0 --json 2>/dev/null || echo "{}")
# Check if dependencies key exists and is not null
if echo "$npm_list_json" | jq -e '.dependencies // false' >/dev/null 2>&1; then
pkgs=$(echo "$npm_list_json" | jq -r '.dependencies | to_entries[] | select((.value.resolved // "") | startswith("file:") | not) | .key')
if [ -n "$pkgs" ]; then
# shellcheck disable=SC2086
x npm update -g --no-audit --no-fund --no-update-notifier $pkgs
fi
else
echo "No globally installed npm packages to update."
fi
else
# Fallback: update everything if jq is missing
echo "Warning: jq not found, updating all global npm packages." >&2
x npm update -g --no-audit --no-fund --no-update-notifier
fi
for pkg in $expected; do
if ! npm list -g --depth=0 "$pkg" >/dev/null 2>&1; then
x npm install -g --no-audit --no-fund --no-update-notifier "$pkg@latest"
fi
done
heading "tools"
# Build and install local npm tools to $HOME/.local/bin.
# We copy the built binaries rather than using "npm link" so that tools/
# remains a source directory with no global side effects.
for tool_dir in "$SRCDIR"/tools/*/; do
if [ -f "$tool_dir/package.json" ]; then
tool_name=$(basename "$tool_dir")
# Extract the binary name and path from package.json.
# Assumption: "bin" is an object {"name": "path"}. We take the first entry.
read -r bin_name bin_source <<<"$(jq -r '.bin | to_entries[0] | "\(.key) \(.value)"' "$tool_dir/package.json" 2>/dev/null)"
if [ -z "$bin_name" ] || [ -z "$bin_source" ] || [ "$bin_name" = "null" ]; then
continue
fi
# 1. Build if local binary is missing or stale
should_build=0
if [ ! -f "$tool_dir/$bin_source" ]; then
should_build=1
# Check if package.json is newer than binary
elif [ "$tool_dir/package.json" -nt "$tool_dir/$bin_source" ]; then
should_build=1
# Check if src directory exists and has newer files
# Use find to check if any file in src/ is newer than the binary
# Use -print -quit for efficiency (stop after first match)
elif [ -d "$tool_dir/src" ] && [ -n "$(find "$tool_dir/src" -type f -newer "$tool_dir/$bin_source" -print -quit 2>/dev/null)" ]; then
should_build=1
fi
if [ "$should_build" -eq 1 ]; then
echo "Building $tool_name..."
(
cd "$tool_dir"
# Assumption: package-lock.json exists
npm ci --no-audit --no-fund
)
fi
# 2. Install (unconditionally copy)
target="$HOME/.local/bin/$bin_name"
local_binary="$tool_dir/$bin_source"
rm -f "$target"
cp "$local_binary" "$target"
fi
done
fi
heading "agents"
if [ -d "$SRCDIR/skills" ]; then
# Each skills directory must be real so manual skills can be added alongside.
for skills_parent in "$HOME/.agents" "$HOME/.claude"; do
xmkdir "$skills_parent"
if [ -L "$skills_parent/skills" ] || [ -f "$skills_parent/skills" ]; then
x rm -rf "$skills_parent/skills"
fi
xmkdir "$skills_parent/skills"
for skill_dir in "$SRCDIR"/skills/*; do
if [ -d "$skill_dir" ]; then
skill_name=$(basename "$skill_dir")
x rm -rf "$skills_parent/skills/$skill_name"
x ln -sf "$skill_dir" "$skills_parent/skills/$skill_name"
fi
done
done
fi