-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·449 lines (385 loc) · 13.5 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·449 lines (385 loc) · 13.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
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
#!/usr/bin/env bash
# Re-exec under bash if invoked as "sh install.sh"
if [ -z "${BASH_VERSION:-}" ]; then
exec bash "$0" "$@" || { echo "Error: bash is required but not found in PATH" >&2; exit 1; }
fi
set -euo pipefail
# ~/Developer/claude-config/install.sh
# Idempotent symlink installer for Claude Code configuration.
# Creates per-file symlinks from this repo into ~/.claude/.
# Safe to re-run at any time — checks before acting.
# ── Formatting helpers ───────────────────────────────────
_info() { printf '\033[1;34m[INFO]\033[0m %s\n' "$*"; }
_ok() { printf '\033[1;32m[OK]\033[0m %s\n' "$*"; }
_warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$*"; }
_err() { printf '\033[1;31m[ERR]\033[0m %s\n' "$*" >&2; }
_skip() {
printf '\033[0;90m[SKIP]\033[0m %s\n' "$*"
skipped+=("$*")
}
_dry() { printf '\033[1;35m[DRY]\033[0m %s\n' "$*"; }
# ── Tracking arrays ─────────────────────────────────────
installed=()
skipped=()
failures=()
# ── Constants ────────────────────────────────────────────
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPLOY_DIR="${HOME}/.claude"
BACKUP_DIR="${DEPLOY_DIR}/backups/symlink-migration"
# ── Parse arguments ─────────────────────────────────────
DRY_RUN=false
REPAIR_ONLY=false
for arg in "$@"; do
case "${arg}" in
--dry-run) DRY_RUN=true ;;
--repair) REPAIR_ONLY=true ;;
--help)
echo "Usage: install.sh [--dry-run] [--repair] [--help]"
echo ""
echo " --dry-run Show what would be done without making changes"
echo " --repair Repair broken symlinks only (no new installs)"
echo " --help Show this help message"
exit 0
;;
*)
_err "Unknown argument: ${arg}"
echo "Usage: install.sh [--dry-run] [--repair] [--help]"
exit 1
;;
esac
done
if [[ "${DRY_RUN}" == true ]]; then
_info "Dry-run mode — no changes will be made"
fi
# ============================================================================
# 1. PRE-FLIGHT CHECKS
# ============================================================================
detected_os="$(uname -s)" || true
if [[ "${detected_os}" != "Darwin" ]]; then
_err "This script is designed for macOS (Darwin). Detected: ${detected_os}"
exit 1
fi
if [[ ! -d "${REPO_DIR}/.git" ]]; then
_err "Not a git repository: ${REPO_DIR}"
_err "This script must be run from the claude-config repo root."
exit 1
fi
if [[ ! -f "${REPO_DIR}/settings.json" ]]; then
_err "Canary file missing: ${REPO_DIR}/settings.json"
exit 1
fi
if [[ ! -f "${REPO_DIR}/CLAUDE.md" ]]; then
_err "Canary file missing: ${REPO_DIR}/CLAUDE.md"
exit 1
fi
if [[ ! -f "${REPO_DIR}/hooks/run-review.sh" ]]; then
_err "Canary file missing: ${REPO_DIR}/hooks/run-review.sh"
exit 1
fi
if [[ "${EUID}" -eq 0 ]]; then
_err "Do not run this script as root."
exit 1
fi
_ok "Pre-flight checks passed (macOS, git repo at ${REPO_DIR}, non-root)"
# ============================================================================
# 2. SUBMODULE ROOTS
# ============================================================================
_SUBMODULE_ROOTS=()
while IFS= read -r sm; do
[[ -n "${sm}" ]] && _SUBMODULE_ROOTS+=("${sm}")
done < <(git -C "${REPO_DIR}" submodule --quiet foreach "echo \$sm_path" 2>/dev/null || true)
if [[ ${#_SUBMODULE_ROOTS[@]} -gt 0 ]]; then
_info "Found ${#_SUBMODULE_ROOTS[@]} submodule(s): ${_SUBMODULE_ROOTS[*]}"
fi
# Cache tracked file list (avoids repeated git ls-files + fixes SC2312)
_TRACKED_FILES=()
while IFS= read -r _tf; do
_TRACKED_FILES+=("${_tf}")
done < <(git -C "${REPO_DIR}" ls-files || true)
# ============================================================================
# 3. HELPER FUNCTIONS
# ============================================================================
_is_excluded() {
case "$1" in
# CI / GitHub metadata
.github/*) return 0 ;;
# Git files
.gitignore) return 0 ;;
.gitmodules) return 0 ;;
.gitattributes) return 0 ;;
# Repo management
.editorconfig) return 0 ;;
.flake8) return 0 ;;
.pre-commit-config.yaml) return 0 ;;
# Documentation
README.md) return 0 ;;
*/README.md) return 0 ;;
# This script
install.sh) return 0 ;;
# Plans
docs/plans/*) return 0 ;;
# Licensing
LICENSE*) return 0 ;;
# Test files
*.bats) return 0 ;;
test-*.sh) return 0 ;;
tests/*) return 0 ;;
scripts/tests/*) return 0 ;;
hooks/tests/*) return 0 ;;
*) return 1 ;;
esac
}
_is_submodule_path() {
local file="$1"
for sm_path in "${_SUBMODULE_ROOTS[@]}"; do
case "${file}" in
"${sm_path}"/*) return 0 ;;
*) ;;
esac
done
return 1
}
_ensure_symlink() {
local target="$1" link="$2"
if [[ -L "${link}" ]]; then
local current
current="$(readlink "${link}")"
if [[ "${current}" == "${target}" ]]; then
_skip "Symlink already correct: ${link}"
return
fi
_warn "Symlink ${link} points to ${current}, replacing"
if [[ "${DRY_RUN}" == true ]]; then
_dry "Would replace symlink: ${link} -> ${target}"
return
fi
rm "${link}"
fi
if [[ "${DRY_RUN}" == true ]]; then
if [[ -e "${link}" ]]; then
_dry "Would back up and replace: ${link}"
else
_dry "Would symlink: ${link} -> ${target}"
fi
return
fi
# Back up existing regular file
if [[ -e "${link}" ]]; then
mkdir -p "${BACKUP_DIR}"
local backup_name
backup_name="${link#"${DEPLOY_DIR}/"}"
backup_name="${backup_name//\//_}.$(date +%Y%m%d%H%M%S)"
mv "${link}" "${BACKUP_DIR}/${backup_name}"
_warn "Backed up ${link} to ${BACKUP_DIR}/${backup_name}"
fi
mkdir -p "$(dirname "${link}")"
ln -s "${target}" "${link}"
_ok "Created symlink: ${link} -> ${target}"
installed+=("symlink:${link}")
}
# ============================================================================
# 4. REPAIR MODE
# ============================================================================
repair_symlinks() {
local repair_count=0
_info "Repair mode — checking for broken symlinks..."
for file in "${_TRACKED_FILES[@]}"; do
_is_excluded "${file}" && continue
_is_submodule_path "${file}" && continue
local link="${DEPLOY_DIR}/${file}"
local target="${REPO_DIR}/${file}"
# Only repair files that exist as regular files where symlinks should be
if [[ -f "${link}" && ! -L "${link}" ]]; then
if [[ "${DRY_RUN}" == true ]]; then
_dry "Would repair: ${link} -> ${target}"
((repair_count += 1))
continue
fi
# Compare content — if deploy copy has edits, preserve them and stage
if ! diff -q "${link}" "${target}" &>/dev/null; then
_warn "Content differs — copying ${link} back to repo and staging"
cp "${link}" "${target}"
git -C "${REPO_DIR}" add "${file}" || _warn "git add failed for ${file} — stage manually"
fi
rm "${link}"
ln -s "${target}" "${link}"
_ok "Repaired: ${link} -> ${target}"
((repair_count += 1))
fi
done
if [[ "${repair_count}" -eq 0 ]]; then
_ok "All symlinks healthy — nothing to repair"
else
_ok "Repaired ${repair_count} symlink(s)"
fi
}
if ${REPAIR_ONLY}; then
repair_symlinks
exit 0
fi
# ============================================================================
# 5. MAIN SYMLINK LOOP
# ============================================================================
_info "Creating config symlinks from repo to ${DEPLOY_DIR}..."
for file in "${_TRACKED_FILES[@]}"; do
_is_excluded "${file}" && continue
_is_submodule_path "${file}" && continue
_ensure_symlink "${REPO_DIR}/${file}" "${DEPLOY_DIR}/${file}"
done
# ============================================================================
# 6. SUBMODULE SYMLINKS (directory-level)
# ============================================================================
if [[ ${#_SUBMODULE_ROOTS[@]} -gt 0 ]]; then
_info "Creating submodule directory symlinks..."
for sm_path in "${_SUBMODULE_ROOTS[@]}"; do
_ensure_symlink "${REPO_DIR}/${sm_path}" "${DEPLOY_DIR}/${sm_path}"
done
fi
# ============================================================================
# 7. POST-INSTALL SMOKE TESTS
# ============================================================================
_info "Running smoke tests..."
if [[ "${DRY_RUN}" == true ]]; then
_dry "Would run smoke tests (skipped in dry-run mode)"
else
# Key files must be symlinks
for key_file in settings.json CLAUDE.md; do
link="${DEPLOY_DIR}/${key_file}"
if [[ -L "${link}" ]]; then
if [[ -e "${link}" ]]; then
_ok "Symlink resolves: ${link}"
else
_warn "Broken symlink: ${link}"
failures+=("broken-symlink:${key_file}")
fi
elif [[ -e "${link}" ]]; then
_warn "Not a symlink (expected symlink): ${link}"
failures+=("not-symlink:${key_file}")
else
_warn "Missing: ${link}"
failures+=("missing:${key_file}")
fi
done
# Hook scripts must be symlinks and executable
for file in "${_TRACKED_FILES[@]}"; do
case "${file}" in
hooks/*.sh)
_is_excluded "${file}" && continue
link="${DEPLOY_DIR}/${file}"
if [[ -L "${link}" ]]; then
if [[ ! -e "${link}" ]]; then
_warn "Broken hook symlink: ${link}"
failures+=("broken-hook:${file}")
elif [[ ! -x "${link}" ]]; then
_warn "Hook not executable: ${link}"
failures+=("not-executable:${file}")
else
_ok "Hook symlink OK: ${file}"
fi
elif [[ -e "${link}" ]]; then
_warn "Hook not a symlink: ${link}"
failures+=("not-symlink:${file}")
fi
;;
*) ;;
esac
done
fi # end dry-run skip for smoke tests
# Full symlink health check — verify all non-excluded deploy paths
if [[ "${DRY_RUN}" == true ]]; then
_dry "Would verify symlink health for all tracked files"
else
_info "Checking symlink health..."
symlink_errors=0
for file in "${_TRACKED_FILES[@]}"; do
_is_excluded "${file}" && continue
_is_submodule_path "${file}" && continue
link="${DEPLOY_DIR}/${file}"
if [[ -L "${link}" ]]; then
if [[ ! -e "${link}" ]]; then
_warn "Broken symlink: ${link}"
failures+=("broken-symlink:${link}")
((symlink_errors += 1))
fi
elif [[ -e "${link}" ]]; then
_warn "Not a symlink (expected symlink): ${link}"
failures+=("not-symlink:${link}")
((symlink_errors += 1))
else
_warn "Missing symlink: ${link}"
failures+=("missing-symlink:${link}")
((symlink_errors += 1))
fi
done
if [[ "${symlink_errors}" -eq 0 ]]; then
_ok "All config symlinks healthy"
fi
fi
# ============================================================================
# 8. CLEAN UP DEPLOY-DIR GIT METADATA
# ============================================================================
# If ~/.claude was previously its own git clone, remove the repo metadata.
# Tracked files are now symlinks — the git repo belongs in ~/Developer/claude-config.
# Only remove files that are git repo artifacts — not user config files.
# .pre-commit-config.yaml, .flake8, .editorconfig are repo-tooling that
# should not exist in the deploy dir, but are harmless if present.
# Safety: if REPO_DIR and DEPLOY_DIR resolve to the same path, skip —
# we'd be deleting our own repo's git data.
_resolved_repo="$(cd "${REPO_DIR}" && pwd -P)"
_resolved_deploy="$(cd "${DEPLOY_DIR}" && pwd -P)"
if [[ "${_resolved_repo}" == "${_resolved_deploy}" ]]; then
_warn "REPO_DIR and DEPLOY_DIR are the same path — skipping git metadata cleanup"
else
_GIT_META_FILES=(".git" ".gitignore" ".gitmodules")
for meta in "${_GIT_META_FILES[@]}"; do
meta_path="${DEPLOY_DIR}/${meta}"
# Skip if it's already a symlink (managed by us)
[[ -L "${meta_path}" ]] && continue
if [[ -e "${meta_path}" ]]; then
if [[ "${DRY_RUN}" == true ]]; then
_dry "Would remove deploy-dir git metadata: ${meta_path}"
else
if rm -rf "${meta_path}"; then
_ok "Removed deploy-dir git metadata: ${meta_path}"
installed+=("removed:${meta}")
else
_err "Failed to remove: ${meta_path}"
failures+=("rm-failed:${meta}")
fi
fi
fi
done
fi # end REPO_DIR != DEPLOY_DIR guard
# ============================================================================
# 9. SUMMARY
# ============================================================================
echo ""
echo "═══════════════════════════════════════════════════════"
echo " Claude Config Install Summary"
echo "═══════════════════════════════════════════════════════"
if [[ ${#installed[@]} -gt 0 ]]; then
_info "Installed/created:"
for item in "${installed[@]}"; do
echo " + ${item}"
done
fi
if [[ ${#skipped[@]} -gt 0 ]]; then
echo ""
_info "Skipped (already present):"
for item in "${skipped[@]}"; do
echo " - ${item}"
done
fi
if [[ ${#failures[@]} -gt 0 ]]; then
echo ""
_warn "Issues found:"
for item in "${failures[@]}"; do
echo " ! ${item}"
done
fi
echo ""
if [[ ${#failures[@]} -gt 0 ]]; then
_err "Completed with ${#failures[@]} issue(s). Review warnings above."
exit 1
fi
_ok "All done — ${#installed[@]} installed, ${#skipped[@]} skipped, 0 failures."