forked from joshukraine/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·418 lines (361 loc) · 11.3 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·418 lines (361 loc) · 11.3 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 -e # Terminate script if anything exits with a non-zero value
################################################################################
# setup.sh
#
# This script uses GNU Stow to symlink files and directories into place.
# It can be run safely multiple times on the same machine. (idempotency)
#
# Usage: ./setup.sh [OPTIONS]
#
# Options:
# --dry-run Show what would be done without making any changes
# --help Show this help message
################################################################################
# Global variables
DRY_RUN=false
show_help() {
cat <<EOF
Usage: $0 [OPTIONS]
This script sets up dotfiles using GNU Stow for symlink management.
It can be run safely multiple times on the same machine.
Options:
--dry-run Show what would be done without making any changes
--help Show this help message
Examples:
$0 # Normal setup
$0 --dry-run # Preview changes without applying them
EOF
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run)
DRY_RUN=true
shift
;;
--help)
show_help
exit 0
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
done
dotfiles_echo() {
local fmt="$1"
shift
# shellcheck disable=SC2059
printf "\\n[DOTFILES] ${fmt}\\n" "$@"
}
dotfiles_info() {
local fmt="$1"
shift
# shellcheck disable=SC2059
printf "[INFO] ${fmt}\\n" "$@"
}
dotfiles_warn() {
local fmt="$1"
shift
# shellcheck disable=SC2059
printf "[WARN] ${fmt}\\n" "$@" >&2
}
dotfiles_error() {
local fmt="$1"
shift
# shellcheck disable=SC2059
printf "[ERROR] ${fmt}\\n" "$@" >&2
}
run_command() {
local cmd="$1"
local description="$2"
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would run: %s" "${cmd}"
if [[ -n "${description}" ]]; then
dotfiles_info " Purpose: %s" "${description}"
fi
else
dotfiles_info "Running: %s" "${cmd}"
if ! eval "${cmd}"; then
dotfiles_error "Failed to execute: %s" "${cmd}"
return 1
fi
fi
}
backup_stow_conflict() {
dotfiles_warn "Conflict detected: ${1}"
local BACKUP_SUFFIX
BACKUP_SUFFIX="$(date +%Y-%m-%d)_$(date +%s)"
local backup_path="${1}_${BACKUP_SUFFIX}"
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would backup %s to %s" "$1" "${backup_path}"
else
dotfiles_info "Backing up %s to %s" "$1" "${backup_path}"
if ! mv -v "$1" "${backup_path}"; then
dotfiles_error "Failed to backup %s" "$1"
return 1
fi
fi
}
# Create a directory if it does not already exist, honoring DRY_RUN. Used for
# paths that must be REAL directories before stow runs: if the target dir is
# missing, GNU Stow folds the whole package into a single symlink into the repo.
ensure_dir() {
local dir="$1"
local description="$2"
if [ -d "${dir}" ]; then
return 0
fi
dotfiles_echo "Setting up ${dir} directory..."
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would create directory: %s" "${dir}"
else
run_command "mkdir -pv '${dir}'" "${description}"
fi
}
# Main script starts here
main() {
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_echo "DRY RUN MODE - No changes will be made"
fi
dotfiles_echo "Initializing dotfiles setup..."
# Check prerequisites
check_prerequisites
# Continue with existing setup logic but with improved error handling
setup_hostname
setup_directories
handle_stow_conflicts
setup_symlinks
setup_tmux
dotfiles_echo "Dotfiles setup complete!"
show_next_steps
}
check_prerequisites() {
local osname
osname=$(uname)
if [ "${osname}" != "Darwin" ]; then
dotfiles_error "This script only supports macOS. Current OS: %s" "${osname}"
exit 1
fi
if ! command -v stow >/dev/null; then
dotfiles_error "GNU Stow is required but was not found. Try: brew install stow"
exit 1
fi
dotfiles_info "Prerequisites check passed"
if [[ "${DRY_RUN}" == "false" ]]; then
dotfiles_info "Requesting sudo access for hostname setup..."
if ! sudo -v; then
dotfiles_error "Failed to obtain sudo access"
exit 1
fi
fi
}
setup_hostname() {
dotfiles_echo "Setting HostName..."
local computer_name local_host_name host_name
computer_name=$(scutil --get ComputerName)
local_host_name=$(scutil --get LocalHostName)
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would set HostName to: %s" "${local_host_name}"
dotfiles_info "[DRY RUN] Would update NetBIOSName in SMB server config"
else
run_command "sudo scutil --set HostName '${local_host_name}'" "Set system hostname"
host_name=$(scutil --get HostName)
run_command "sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist NetBIOSName -string '${host_name}'" "Update SMB server NetBIOS name"
fi
printf "ComputerName: ==> [%s]\\n" "${computer_name}"
printf "LocalHostName: ==> [%s]\\n" "${local_host_name}"
if [[ "${DRY_RUN}" == "false" ]]; then
printf "HostName: ==> [%s]\\n" "$(scutil --get HostName)"
fi
}
setup_directories() {
if [ -z "${DOTFILES}" ]; then
export DOTFILES="${HOME}/dotfiles"
fi
dotfiles_info "Using DOTFILES directory: %s" "${DOTFILES}"
if [[ ! -d "${DOTFILES}" ]]; then
dotfiles_error "DOTFILES directory not found: %s" "${DOTFILES}"
exit 1
fi
if [ -z "${XDG_CONFIG_HOME}" ]; then
ensure_dir "${HOME}/.config" "Create XDG config directory"
export XDG_CONFIG_HOME="${HOME}/.config"
fi
ensure_dir "${HOME}/.local/bin" "Create local bin directory"
# Pre-create ~/.claude as a real directory before stowing. Otherwise GNU Stow
# folds the whole directory into a single symlink into the repo, and Claude Code
# then writes its runtime state (sessions/, projects/, history.jsonl, …) into the
# dotfiles working tree. See README "Troubleshooting: ~/.claude folding".
ensure_dir "${HOME}/.claude" "Create Claude config directory (prevents stow folding)"
}
handle_stow_conflicts() {
dotfiles_echo "Checking for potential stow conflicts..."
if [[ "${DRY_RUN}" == "false" ]]; then
if ! cd "${DOTFILES}/"; then
dotfiles_error "Failed to change to DOTFILES directory: %s" "${DOTFILES}"
exit 1
fi
else
dotfiles_info "[DRY RUN] Would change to directory: %s" "${DOTFILES}"
fi
local stow_conflicts=(
".asdfrc"
".bashrc"
".claude/CLAUDE.md"
".claude/cheatsheet.md"
".claude/skills"
".claude/presets"
".claude/settings.json"
".claude/starship.toml"
".config/ghostty"
".config/kitty"
".config/lazygit"
".config/nvim"
".config/starship.toml"
".config/tmux"
".config/yamllint"
".config/zsh"
".config/zsh-abbr"
".default-gems"
".default-npm-packages"
".gemrc"
".gitconfig"
".gitignore_global"
".gitmessage"
".hushlogin"
".irbrc"
".laptop.local"
".local/bin/bb"
".local/bin/bbc"
".local/bin/bbcf"
".local/bin/bubo"
".local/bin/colortest"
".local/bin/copy"
".local/bin/g"
".local/bin/gbrm"
".local/bin/git-brst"
".local/bin/git-check-uncommitted"
".local/bin/git-cm"
".local/bin/git-publish"
".local/bin/git-uncommit"
".local/bin/gwip"
".local/bin/merge-presets"
".local/bin/setup-repo"
".local/bin/sha256"
".local/bin/starship-claude"
".local/bin/ta"
".local/bin/tat"
".local/bin/tk"
".local/bin/tka"
".local/bin/tn"
".local/bin/tna"
".local/bin/update_jumpstart"
".npmrc"
".pryrc"
".ripgreprc"
".rubocop.yml"
".tool-versions"
".zshrc"
"Brewfile"
)
local conflicts_found=0
for item in "${stow_conflicts[@]}"; do
if [ -e "${HOME}/${item}" ]; then
# Potential conflict detected
if [ -L "${HOME}/${item}" ]; then
# This is a symlink and we can ignore it.
dotfiles_info "Symlink already exists (OK): %s" "${HOME}/${item}"
continue
elif [ "${item}" == ".tool-versions" ]; then
# This was likely generated by Laptop, and we actually want to adopt it for now.
dotfiles_info "Found %s - will adopt existing file" "${HOME}/.tool-versions"
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would run: stow --adopt asdf/"
else
run_command "stow --adopt asdf/" "Adopt existing .tool-versions file"
fi
else
# This is a file or directory that will cause a conflict.
conflicts_found=$((conflicts_found + 1))
backup_stow_conflict "${HOME}/${item}"
fi
fi
done
if [[ ${conflicts_found} -gt 0 ]]; then
dotfiles_info "Handled %d potential conflicts" "${conflicts_found}"
else
dotfiles_info "No conflicts detected"
fi
}
setup_symlinks() {
dotfiles_echo "Setting up symlinks with GNU Stow..."
if [[ "${DRY_RUN}" == "false" ]] && [[ "${PWD}" != "${DOTFILES}" ]]; then
if ! cd "${DOTFILES}/"; then
dotfiles_error "Failed to change to DOTFILES directory: %s" "${DOTFILES}"
exit 1
fi
fi
local stow_packages=0
for item in *; do
if [ -d "${item}" ]; then
stow_packages=$((stow_packages + 1))
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would stow package: %s" "${item}"
else
run_command "stow '${item}'/" "Stow package: ${item}"
fi
fi
done
dotfiles_info "Processed %d stow packages" "${stow_packages}"
}
setup_tmux() {
if command -v tmux &>/dev/null; then
if [ ! -d "${HOME}/.terminfo" ]; then
dotfiles_echo "Installing custom terminfo entries..."
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would install terminfo entries for tmux and xterm"
else
# These entries enable, among other things, italic text in the terminal.
run_command "tic -x '${DOTFILES}/terminfo/tmux-256color.terminfo'" "Install tmux terminfo entry"
run_command "tic -x '${DOTFILES}/terminfo/xterm-256color-italic.terminfo'" "Install xterm italic terminfo entry"
fi
else
dotfiles_info "Custom terminfo entries already installed"
fi
if [ ! -d "${DOTFILES}/tmux/.config/tmux/plugins" ]; then
dotfiles_echo "Installing Tmux Plugin Manager..."
if [[ "${DRY_RUN}" == "true" ]]; then
dotfiles_info "[DRY RUN] Would clone TPM to: %s" "${DOTFILES}/tmux/.config/tmux/plugins/tpm"
else
run_command "git clone https://github.com/tmux-plugins/tpm '${DOTFILES}/tmux/.config/tmux/plugins/tpm'" "Install Tmux Plugin Manager"
fi
else
dotfiles_info "Tmux Plugin Manager already installed"
fi
else
dotfiles_info "Tmux not found - skipping tmux setup"
fi
}
show_next_steps() {
echo
echo "Possible next steps:"
echo "-> Install Zap (https://www.zapzsh.com)"
echo "-> Install Homebrew packages (brew bundle install)"
if command -v tmux &>/dev/null; then
echo "-> Install Tmux plugins with <prefix> + I (https://github.com/tmux-plugins/tpm)"
fi
echo "-> Set up Claude Code (https://docs.anthropic.com/en/docs/claude-code)"
echo "-> Set up 1Password CLI (https://developer.1password.com/docs/cli)"
echo "-> Check out documentation for LazyVim (https://www.lazyvim.org/)"
echo
}
# Run main only when executed directly, not when sourced (e.g. by the test suite).
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Error handling
trap 'dotfiles_error "Script failed at line $LINENO"' ERR
main "$@"
fi