-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup
More file actions
executable file
·313 lines (279 loc) · 8.25 KB
/
setup
File metadata and controls
executable file
·313 lines (279 loc) · 8.25 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
#!/usr/bin/env zsh
set -e
DOTFILES=$HOME/.dotfiles
source "$DOTFILES/zsh/tool_packages.zsh"
# --- argument parsing ---
DRY_RUN=0
for arg in "$@"; do
case $arg in
--dry-run) DRY_RUN=1 ;;
*)
echo "Usage: setup [--dry-run]" >&2
exit 1
;;
esac
done
# --- dry-run helpers ---
dry_mkdir() {
if (( DRY_RUN )); then
echo "[dry-run] mkdir -p $1"
else
mkdir -p "$1"
fi
}
dry_rm() {
if (( DRY_RUN )); then
echo "[dry-run] rm -f $@"
else
rm -f "$@"
fi
}
setup_link() {
src="$DOTFILES/$1"
dst="$HOME/$2"
if [ "$src" -ef "$dst" ]; then
return
fi
if [[ -e "$dst" || -h "$dst" ]]; then
>&2 echo "WARNING: $dst exists, overwriting"
if (( DRY_RUN )); then
echo "[dry-run] rm -rf $dst"
else
rm -rf "$dst"
fi
fi
if (( DRY_RUN )); then
echo "[dry-run] $src -> $dst"
else
echo "$src -> $dst"
ln -s "$src" "$dst"
fi
}
_dotfiles_install_hint() {
if [[ $OSTYPE == darwin* ]]; then
echo "${_dotfiles_brew_pkg[$1]}"
elif [[ $OSTYPE == linux* ]]; then
echo "${_dotfiles_pacman_pkg[$1]}"
fi
}
link_role() {
if (( DRY_RUN )); then
echo "[dry-run] Adding role $1"
else
echo "Adding role $1"
fi
role_dir=roles/$1
if [ ! -d $DOTFILES/$role_dir ]; then
echo "ERROR: No role dir at $role_dir"
return
fi
# Check for required commands before running install/setup
local requires_cmds_file="$DOTFILES/$role_dir/requires_cmds"
if [ -f "$requires_cmds_file" ]; then
local missing_cmds=()
local cmd
while IFS= read -r cmd; do
[[ -z "$cmd" || "$cmd" = \#* ]] && continue
if ! command -v "$cmd" >/dev/null 2>&1; then
missing_cmds+=("$cmd")
fi
done < "$requires_cmds_file"
if (( ${#missing_cmds} )); then
for cmd in "${missing_cmds[@]}"; do
local hint=$(_dotfiles_install_hint "$cmd")
if [[ -n "$hint" ]]; then
echo "Role '$1' requires '$cmd' — install with: $hint"
if (( ! DRY_RUN )); then
printf "Install now? [y/N] "
read -r answer
if [[ "$answer" == [yY]* ]]; then
eval "$hint"
fi
fi
else
echo "Role '$1' requires '$cmd' (no install hint available)"
fi
done
# Re-check after install attempts
local still_missing=()
while IFS= read -r cmd; do
[[ -z "$cmd" || "$cmd" = \#* ]] && continue
if ! command -v "$cmd" >/dev/null 2>&1; then
still_missing+=("$cmd")
fi
done < "$requires_cmds_file"
if (( ${#still_missing} )); then
echo "WARNING: skipping role '$1' — missing required commands: ${still_missing[*]}"
return
fi
fi
fi
install_file=$DOTFILES/$role_dir/install
if [ -f $install_file ]; then
if (( DRY_RUN )); then
echo "[dry-run] would run install for $1"
else
(source $install_file) || >&2 echo "WARNING: install for role '$1' failed (exit $?), continuing..."
fi
fi
setup_file=$DOTFILES/$role_dir/setup
if [ -f $setup_file ]; then
if (( DRY_RUN )); then
echo "[dry-run] would run setup for $1"
else
# Run role setup in a subshell so failures don't abort the entire setup
(source $setup_file) || >&2 echo "WARNING: setup for role '$1' failed (exit $?), continuing..."
fi
fi
zsh_plugin_file="$role_dir/zsh_plugin"
if [ -f $DOTFILES/$zsh_plugin_file ]; then
setup_link $zsh_plugin_file ".zsh/plugins/$1.zsh"
fi
}
# Resolve role dependencies via DFS topological sort.
# Populates _order array with roles in dependency-first order.
typeset -A _resolved _in_stack
_order=()
resolve_role() {
local role=$1
# Already fully processed
(( ${+_resolved[$role]} )) && return 0
# Cycle detection
if (( ${+_in_stack[$role]} )); then
echo "ERROR: dependency cycle detected involving role '$role'" >&2
return 1
fi
_in_stack[$role]=1
# Check role directory exists
if [ ! -d "$DOTFILES/roles/$role" ]; then
echo "ERROR: required role '$role' not found" >&2
return 1
fi
# Recurse into dependencies
local requires_file="$DOTFILES/roles/$role/requires"
if [ -f "$requires_file" ]; then
local dep
while IFS= read -r dep; do
# Skip empty lines and comments
[[ -z "$dep" || "$dep" = \#* ]] && continue
resolve_role "$dep" || return 1
done < "$requires_file"
fi
unset "_in_stack[$role]"
_resolved[$role]=1
_order+=("$role")
}
source "$DOTFILES/lib/install_go_tools.zsh"
dry_mkdir $HOME/.gradle
setup_link "gradle/properties" ".gradle/gradle.properties"
setup_link "zsh/zshrc" ".zshrc"
dry_mkdir $HOME/.zsh
dry_mkdir $HOME/.zsh/var/
dry_mkdir $HOME/.zsh/plugins/
# clear any existing plugin links
if (( DRY_RUN )); then
echo "[dry-run] rm -f $HOME/.zsh/plugins/*"
else
rm -f $HOME/.zsh/plugins/*
fi
for ZSH_PLUGIN in $DOTFILES/zsh/plugins/*; do
NAME=$(basename $ZSH_PLUGIN)
setup_link "zsh/plugins/$NAME" ".zsh/plugins/$NAME"
done
roles_file=$DOTFILES/hosts/$(hostname)/roles
if [ ! -f $roles_file ]; then
roles_file=$HOME/.zshroles
fi
if [ -f $roles_file ]; then
while IFS= read -r role; do
[[ -z "$role" || "$role" = \#* ]] && continue
resolve_role "$role" || exit 1
done < $roles_file
for role in "${_order[@]}"; do
link_role $role
done
fi
dry_mkdir $HOME/.ssh
setup_link "ssh/config" ".ssh/config"
ssh_host_config=hosts/$(hostname)/ssh_host_config
if [ -f $DOTFILES/$ssh_host_config ]; then
setup_link "$ssh_host_config" ".ssh/host_config"
fi
dry_mkdir $HOME/.vim
for VIMFILE in $DOTFILES/vim/*; do
NAME=$(basename $VIMFILE)
setup_link "vim/$NAME" ".vim/$NAME"
done
dry_mkdir $HOME/.claude
setup_link "claude/CLAUDE.md" ".claude/CLAUDE.md"
dry_mkdir $HOME/.claude/skills
for SKILL_DIR in $DOTFILES/claude/skills/*/; do
NAME=$(basename "$SKILL_DIR")
setup_link "claude/skills/$NAME" ".claude/skills/$NAME"
done
dry_mkdir $HOME/.pi
dry_mkdir $HOME/.pi/agent
dry_mkdir $HOME/.pi/agent/extensions
setup_link "pi/agent/settings.json" ".pi/agent/settings.json"
setup_link "pi/agent/extensions/claude-memories.ts" ".pi/agent/extensions/claude-memories.ts"
dry_mkdir $HOME/.git
setup_link "git/config" ".gitconfig"
setup_link "git/ignore" ".git/ignore"
dry_mkdir $HOME/bin
for SCRIPT in $DOTFILES/scripts/*; do
[[ -d "$SCRIPT" ]] && continue
NAME=$(basename $SCRIPT)
setup_link "scripts/$NAME" "bin/$NAME"
done
install_go_tools || true
# install pre-commit hook for the dotfiles repo itself
if [ -d "$DOTFILES/.git/hooks" ]; then
hook_src="$DOTFILES/hooks/pre-commit"
hook_dst="$DOTFILES/.git/hooks/pre-commit"
if [ -f "$hook_src" ] && ! [ "$hook_src" -ef "$hook_dst" ]; then
if (( DRY_RUN )); then
echo "[dry-run] $hook_src -> $hook_dst"
else
echo "$hook_src -> $hook_dst"
ln -sf "$hook_src" "$hook_dst"
fi
fi
fi
if [[ $OSTYPE == darwin* ]]; then
brewfile=hosts/$(hostname)/brewfile
if [ -f $DOTFILES/$brewfile ]; then
setup_link "$brewfile" ".Brewfile"
else
if (( DRY_RUN )); then
echo "[dry-run] touch $HOME/.Brewfile"
else
touch $HOME/.Brewfile
fi
fi
if [ -f $DOTFILES/$brewfile-x86 ]; then
setup_link "$brewfile-x86" ".Brewfile-x86"
fi
# symlink icloud drive to homedir
if [ ! -e $HOME/iCloud ] && [ ! -L $HOME/iCloud ]; then
if (( DRY_RUN )); then
echo "[dry-run] ln -s ~/Library/Mobile Documents/com~apple~CloudDocs ~/iCloud"
else
ln -s ~/Library/Mobile\ Documents/com\~apple\~CloudDocs ~/iCloud
fi
fi
fi
setup_link "tmux/tmux.conf" ".tmux.conf"
dry_mkdir $HOME/.config
setup_link "starship/starship.toml" ".config/starship.toml"
dry_mkdir $HOME/.config/gh
setup_link "gh/config.yml" ".config/gh/config.yml"
dry_mkdir $HOME/.codex/rules
setup_link "codex/config.toml" ".codex/config.toml"
setup_link "codex/rules/default.rules" ".codex/rules/default.rules"
echo ""
if (( DRY_RUN )); then
echo "Dry run complete. No changes were made."
else
echo "Config files are setup."
echo "Run 'reload-config' to re-source zsh config."
fi