-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·387 lines (357 loc) · 12.5 KB
/
install.sh
File metadata and controls
executable file
·387 lines (357 loc) · 12.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
#!/bin/bash
set -e
CONFIG_DIR="$(cd "$(dirname "$0")" && pwd)"
BACKUP_DIR="$CONFIG_DIR/.backup"
DRY_RUN=false
FORCE=false
# Colors
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
RESET='\033[0m'
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-n|--dry-run)
DRY_RUN=true
shift
;;
-f|--force)
FORCE=true
shift
;;
-h|--help)
echo "Usage: ./install.sh [options]"
echo ""
echo "Options:"
echo " -n, --dry-run Show what would be done without making changes"
echo " -f, --force Overwrite conflicts without prompting"
echo " -h, --help Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Create backup with timestamp
create_backup() {
local timestamp=$(date +%Y%m%d_%H%M%S)
local backup_path="$BACKUP_DIR/$timestamp"
mkdir -p "$backup_path"
echo "$backup_path"
}
# Backup a file or directory
backup_item() {
local src="$1"
local backup_path="$2"
local relative_path="${src#$HOME/}"
local dest="$backup_path/$relative_path"
mkdir -p "$(dirname "$dest")"
if [ -L "$src" ]; then
# For symlinks, store the target
echo "$(readlink "$src")" > "$dest.symlink"
elif [ -d "$src" ]; then
cp -r "$src" "$dest"
else
cp "$src" "$dest"
fi
}
# Write manifest
write_manifest() {
local backup_path="$1"
local operation="$2"
shift 2
local items=("$@")
cat > "$backup_path/manifest.json" << EOF
{
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"operation": "$operation",
"items": [$(printf '"%s",' "${items[@]}" | sed 's/,$//')],
"user": "$USER",
"hostname": "$(hostname)"
}
EOF
}
# Check if item has conflict (exists locally and is not a symlink to our repo)
has_conflict() {
local path="$1"
if [ -e "$path" ] && [ ! -L "$path" ]; then
return 0
fi
if [ -L "$path" ]; then
local target=$(readlink "$path")
if [[ "$target" != "$CONFIG_DIR"* ]]; then
return 0
fi
fi
return 1
}
# Handle conflict interactively
handle_conflict() {
local src="$1"
local dest="$2"
local backup_path="$3"
local item_name="$4"
if $FORCE; then
backup_item "$dest" "$backup_path"
return 0 # Proceed with overwrite
fi
echo ""
echo -e "${YELLOW}Conflict:${RESET} $item_name exists locally and differs from repo"
echo " Local: $dest"
echo " Repo: $src"
echo ""
echo "Options:"
echo " [r] Use repo version (backup local)"
echo " [l] Keep local version (skip)"
echo " [d] Show diff"
echo " [q] Quit"
while true; do
read -p "Choice [r/l/d/q]: " choice
case $choice in
r|R)
backup_item "$dest" "$backup_path"
return 0 # Proceed with overwrite
;;
l|L)
return 1 # Skip this item
;;
d|D)
echo ""
if [ -d "$src" ]; then
diff -r "$dest" "$src" 2>/dev/null || true
else
diff "$dest" "$src" 2>/dev/null || true
fi
echo ""
;;
q|Q)
echo "Aborted."
exit 1
;;
*)
echo "Invalid choice. Use r, l, d, or q."
;;
esac
done
}
# Dry run output
dry_run_msg() {
echo -e "${BLUE}[dry-run]${RESET} $1"
}
# Main installation
main() {
if $DRY_RUN; then
echo -e "${BOLD}Dry run - showing what would be done:${RESET}"
echo ""
else
echo "Installing Claude Code config from $CONFIG_DIR"
echo ""
fi
local backup_path=""
local backed_up_items=()
local has_changes=false
if ! $DRY_RUN; then
mkdir -p ~/.claude
fi
# Settings
if [ -f "$CONFIG_DIR/settings.json" ]; then
if $DRY_RUN; then
if has_conflict ~/.claude/settings.json; then
dry_run_msg "Would backup and replace ~/.claude/settings.json"
else
dry_run_msg "Would link settings.json"
fi
else
if has_conflict ~/.claude/settings.json; then
[ -z "$backup_path" ] && backup_path=$(create_backup)
if handle_conflict "$CONFIG_DIR/settings.json" ~/.claude/settings.json "$backup_path" "settings.json"; then
backed_up_items+=("settings.json")
rm -rf ~/.claude/settings.json
ln -sf "$CONFIG_DIR/settings.json" ~/.claude/settings.json
echo -e "${GREEN}✓${RESET} settings.json (replaced, backup saved)"
has_changes=true
else
echo -e "${YELLOW}○${RESET} settings.json (kept local)"
fi
else
ln -sf "$CONFIG_DIR/settings.json" ~/.claude/settings.json
echo -e "${GREEN}✓${RESET} settings.json"
has_changes=true
fi
fi
fi
# MCP settings
if [ -f "$CONFIG_DIR/mcp.json" ]; then
if $DRY_RUN; then
if has_conflict ~/.claude/mcp.json; then
dry_run_msg "Would backup and replace ~/.claude/mcp.json"
else
dry_run_msg "Would link mcp.json"
fi
else
if has_conflict ~/.claude/mcp.json; then
[ -z "$backup_path" ] && backup_path=$(create_backup)
if handle_conflict "$CONFIG_DIR/mcp.json" ~/.claude/mcp.json "$backup_path" "mcp.json"; then
backed_up_items+=("mcp.json")
rm -rf ~/.claude/mcp.json
ln -sf "$CONFIG_DIR/mcp.json" ~/.claude/mcp.json
echo -e "${GREEN}✓${RESET} mcp.json (replaced, backup saved)"
has_changes=true
else
echo -e "${YELLOW}○${RESET} mcp.json (kept local)"
fi
else
ln -sf "$CONFIG_DIR/mcp.json" ~/.claude/mcp.json
echo -e "${GREEN}✓${RESET} mcp.json"
has_changes=true
fi
fi
fi
# Statusline
if [ -f "$CONFIG_DIR/statusline.sh" ]; then
if $DRY_RUN; then
if has_conflict ~/.claude/statusline.sh; then
dry_run_msg "Would backup and replace ~/.claude/statusline.sh"
else
dry_run_msg "Would link statusline.sh"
fi
else
if has_conflict ~/.claude/statusline.sh; then
[ -z "$backup_path" ] && backup_path=$(create_backup)
if handle_conflict "$CONFIG_DIR/statusline.sh" ~/.claude/statusline.sh "$backup_path" "statusline.sh"; then
backed_up_items+=("statusline.sh")
rm -rf ~/.claude/statusline.sh
ln -sf "$CONFIG_DIR/statusline.sh" ~/.claude/statusline.sh
echo -e "${GREEN}✓${RESET} statusline.sh (replaced, backup saved)"
has_changes=true
else
echo -e "${YELLOW}○${RESET} statusline.sh (kept local)"
fi
else
ln -sf "$CONFIG_DIR/statusline.sh" ~/.claude/statusline.sh
echo -e "${GREEN}✓${RESET} statusline.sh"
has_changes=true
fi
fi
fi
# Skills (directory symlinks per skill)
if [ -d "$CONFIG_DIR/skills" ] && [ -n "$(ls -A "$CONFIG_DIR/skills" 2>/dev/null)" ]; then
mkdir -p ~/.claude/skills
for skill in "$CONFIG_DIR/skills"/*/; do
[ -d "$skill" ] || continue
skill_name=$(basename "$skill")
local dest=~/.claude/skills/"$skill_name"
if $DRY_RUN; then
if has_conflict "$dest"; then
dry_run_msg "Would backup and replace skills/$skill_name"
else
dry_run_msg "Would link skills/$skill_name"
fi
else
if has_conflict "$dest"; then
[ -z "$backup_path" ] && backup_path=$(create_backup)
if handle_conflict "$skill" "$dest" "$backup_path" "skills/$skill_name"; then
backed_up_items+=("skills/$skill_name")
rm -rf "$dest"
ln -sfn "$skill" "$dest"
echo -e "${GREEN}✓${RESET} skills/$skill_name (replaced, backup saved)"
has_changes=true
else
echo -e "${YELLOW}○${RESET} skills/$skill_name (kept local)"
fi
else
ln -sfn "$skill" "$dest"
echo -e "${GREEN}✓${RESET} skills/$skill_name"
has_changes=true
fi
fi
done
fi
# Agents (file symlinks per agent)
if [ -d "$CONFIG_DIR/agents" ] && ls "$CONFIG_DIR/agents"/*.md &>/dev/null; then
mkdir -p ~/.claude/agents
for agent in "$CONFIG_DIR/agents"/*.md; do
[ -f "$agent" ] || continue
agent_name=$(basename "$agent")
local dest=~/.claude/agents/"$agent_name"
if $DRY_RUN; then
if has_conflict "$dest"; then
dry_run_msg "Would backup and replace agents/$agent_name"
else
dry_run_msg "Would link agents/$agent_name"
fi
else
if has_conflict "$dest"; then
[ -z "$backup_path" ] && backup_path=$(create_backup)
if handle_conflict "$agent" "$dest" "$backup_path" "agents/$agent_name"; then
backed_up_items+=("agents/$agent_name")
rm -rf "$dest"
ln -sf "$agent" "$dest"
echo -e "${GREEN}✓${RESET} agents/$agent_name (replaced, backup saved)"
has_changes=true
else
echo -e "${YELLOW}○${RESET} agents/$agent_name (kept local)"
fi
else
ln -sf "$agent" "$dest"
echo -e "${GREEN}✓${RESET} agents/$agent_name"
has_changes=true
fi
fi
done
fi
# Rules (file symlinks per rule)
if [ -d "$CONFIG_DIR/rules" ] && ls "$CONFIG_DIR/rules"/*.md &>/dev/null; then
mkdir -p ~/.claude/rules
for rule in "$CONFIG_DIR/rules"/*.md; do
[ -f "$rule" ] || continue
rule_name=$(basename "$rule")
local dest=~/.claude/rules/"$rule_name"
if $DRY_RUN; then
if has_conflict "$dest"; then
dry_run_msg "Would backup and replace rules/$rule_name"
else
dry_run_msg "Would link rules/$rule_name"
fi
else
if has_conflict "$dest"; then
[ -z "$backup_path" ] && backup_path=$(create_backup)
if handle_conflict "$rule" "$dest" "$backup_path" "rules/$rule_name"; then
backed_up_items+=("rules/$rule_name")
rm -rf "$dest"
ln -sf "$rule" "$dest"
echo -e "${GREEN}✓${RESET} rules/$rule_name (replaced, backup saved)"
has_changes=true
else
echo -e "${YELLOW}○${RESET} rules/$rule_name (kept local)"
fi
else
ln -sf "$rule" "$dest"
echo -e "${GREEN}✓${RESET} rules/$rule_name"
has_changes=true
fi
fi
done
fi
echo ""
if $DRY_RUN; then
echo "Run without --dry-run to apply changes."
else
if [ -n "$backup_path" ] && [ ${#backed_up_items[@]} -gt 0 ]; then
write_manifest "$backup_path" "install" "${backed_up_items[@]}"
echo -e "${BLUE}Backup saved:${RESET} $backup_path"
echo "Run './sync.sh undo' to restore."
echo ""
fi
echo "Done! Claude Code config installed."
echo ""
echo "Local-only items in ~/.claude/ are preserved."
echo "Use ./sync.sh to manage what gets shared."
fi
}
main