-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcli.sh
More file actions
executable file
·1185 lines (1044 loc) · 35.2 KB
/
Copy pathcli.sh
File metadata and controls
executable file
·1185 lines (1044 loc) · 35.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/common.sh"
BACKUP_DIR="$HOME/ai-tools-backup-$(date +%Y%m%d-%H%M%S)"
DRY_RUN=false
BACKUP=false
PROMPT_BACKUP=true
YES_TO_ALL=false
VERBOSE=false
# Detect OS (Windows vs Unix-like)
IS_WINDOWS=false
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" || -n "$MSYSTEM" ]]; then
IS_WINDOWS=true
fi
# Track whether Amp is installed (for backlog.md dependency)
AMP_INSTALLED=false
# Auto-detect non-interactive mode (stdin is piped)
if [ ! -t 0 ]; then
YES_TO_ALL=true
fi
for arg in "$@"; do
case $arg in
--dry-run)
DRY_RUN=true
shift
;;
--backup)
BACKUP=true
PROMPT_BACKUP=false
shift
;;
--no-backup)
BACKUP=false
PROMPT_BACKUP=false
shift
;;
--yes|-y)
YES_TO_ALL=true
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
--rollback)
log_info "Rolling back last transaction..."
rollback_transaction
exit $?
;;
*)
echo "Unknown option: $arg"
echo "Usage: $0 [--dry-run] [--backup] [--no-backup] [--yes|-y] [-v|--verbose] [--rollback]"
exit 1
;;
esac
done
# Preflight check for required tools
preflight_check() {
local missing_tools=()
log_info "Running preflight checks..."
# Core utilities required by the script
local required_tools=("jq" "awk" "sed" "basename" "cat" "head" "tail" "grep" "date")
for tool in "${required_tools[@]}"; do
if ! command -v "$tool" &>/dev/null; then
missing_tools+=("$tool")
fi
done
if [ ${#missing_tools[@]} -gt 0 ]; then
log_error "Missing required tools: ${missing_tools[*]}"
log_info "Please install the missing tools and try again."
exit 1
fi
log_success "All required tools available"
}
# Install MCP server with better error handling
install_mcp_server() {
local server_name="$1"
local install_cmd="$2"
# Capture stderr to temp file for error analysis
local err_file="/tmp/claude-mcp-${server_name}.err"
if execute "$install_cmd" 2>"$err_file"; then
log_success "${server_name} MCP server added (global)"
rm -f "$err_file"
return 0
else
# Check if it's an "already exists" error (expected)
if grep -qi "already" "$err_file" 2>/dev/null; then
log_info "${server_name} already installed"
else
# Actual error - provide details for debugging
log_warning "${server_name} installation failed - check $err_file for details"
fi
rm -f "$err_file"
return 1
fi
}
# Set up TMPDIR to avoid cross-device link errors
# Uses a temp directory within $HOME to ensure same filesystem
setup_tmpdir() {
local tmp_dir="$HOME/.claude/tmp"
mkdir -p "$tmp_dir" 2>/dev/null || true
export TMPDIR="$tmp_dir"
}
check_prerequisites() {
log_info "Checking prerequisites..."
if ! command -v git &>/dev/null; then
log_error "Git is not installed. Please install git first."
exit 1
fi
log_success "Git found"
if command -v bun &>/dev/null; then
BUN_VERSION=$(bun --version)
log_success "Bun found ($BUN_VERSION)"
elif command -v node &>/dev/null; then
NODE_VERSION=$(node --version)
log_success "Node.js found ($NODE_VERSION)"
log_warning "Some scripts (e.g., context-check) prefer Bun. Install with: brew install oven-sh/bun/bun"
else
log_error "Neither Bun nor Node.js is installed. Please install one of them first."
exit 1
fi
}
install_global_tools() {
log_info "Checking global tools for hooks..."
# Check/install jq (required for JSON parsing in hooks)
if ! command -v jq &>/dev/null; then
log_warning "jq not found. Installing jq..."
if [ "$IS_WINDOWS" = true ]; then
# Windows: use choco or winget, or download binary
if command -v choco &>/dev/null; then
execute "choco install jq -y"
elif command -v winget &>/dev/null; then
execute "winget install jq"
else
log_warning "Please install jq manually: https://stedolan.github.io/jq/download/"
fi
else
# Mac/Linux: use brew or apt
if command -v brew &>/dev/null; then
execute "brew install jq"
elif command -v apt-get &>/dev/null; then
execute "sudo apt-get install -y jq"
else
log_warning "Please install jq manually: https://stedolan.github.io/jq/download/"
fi
fi
else
log_success "jq found"
fi
# Check/install biome (required for JS/TS formatting)
if ! command -v biome &>/dev/null; then
log_warning "biome not found. Installing biome globally..."
execute "npm install -g @biomejs/biome"
else
log_success "biome found"
fi
# Check/install backlog.md (only if Amp is installed)
if [ "$AMP_INSTALLED" = true ]; then
if ! command -v backlog &>/dev/null; then
log_info "Installing backlog.md for Amp integration..."
execute "npm install -g backlog.md"
else
log_success "backlog.md found"
fi
fi
log_success "Global tools check complete"
}
# Helper: Safely copy a directory, handling "Text file busy" errors
# Usage: safe_copy_dir "source_dir" "dest_dir"
safe_copy_dir() {
local source_dir="$1"
local dest_dir="$2"
local skipped=0
local errors=0
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would copy $source_dir to $dest_dir"
return 0
fi
# Ensure destination parent exists
if ! mkdir -p "$(dirname "$dest_dir")" 2>/dev/null; then
log_warning "Failed to create destination directory: $(dirname "$dest_dir")"
return 1
fi
# Try regular copy first
if cp -r "$source_dir" "$dest_dir" 2>/dev/null; then
return 0
fi
# If copy failed, try with rsync to handle busy files
if command -v rsync &>/dev/null; then
# Use rsync to copy, matching cp -r behavior
if rsync -a --ignore-errors "$source_dir" "$(dirname "$dest_dir")/" 2>/dev/null; then
return 0
fi
fi
# Fallback: copy non-binary files, skip busy binaries
mkdir -p "$dest_dir"
while IFS= read -r file; do
rel_path="${file#$source_dir/}"
dest_file="$dest_dir/$rel_path"
mkdir -p "$(dirname "$dest_file")"
if cp "$file" "$dest_file" 2>/dev/null; then
((errors++))
else
((skipped++))
[ "$VERBOSE" = true ] && log_warning "Skipped busy file: $rel_path"
fi
done < <(find "$source_dir" -type f 2>/dev/null)
# Log summary in verbose mode
[ "$VERBOSE" = true ] && [ $skipped -gt 0 ] && log_info "Skipped $skipped busy file(s)"
return 0
}
# Helper: Copy a config directory if it exists in source and destination
# Usage: copy_config_dir "source_dir" "dest_parent" "dest_name"
copy_config_dir() {
local source_dir="$1"
local dest_parent="$2"
local dest_name="$3"
if [ -d "$source_dir" ]; then
execute "mkdir -p $dest_parent"
execute "cp -r $source_dir $dest_parent/$dest_name"
log_success "Backed up $dest_name configs"
fi
}
# Helper: Copy a config file if it exists in source
# Usage: copy_config_file "source_file" "dest_dir"
copy_config_file() {
local source_file="$1"
local dest_dir="$2"
if [ -f "$source_file" ]; then
execute "mkdir -p $dest_dir"
execute "cp $source_file $dest_dir/"
return 0
fi
return 1
}
# Helper: Ensure a CLI tool is installed, prompting if interactive
# Usage: ensure_cli_tool "tool_name" "install_check_cmd" "install_cmd" "version_cmd"
ensure_cli_tool() {
local name="$1"
local check_cmd="$2"
local install_cmd="$3"
local version_cmd="$4"
if command -v "$name" &>/dev/null; then
if [ -n "$version_cmd" ]; then
local version
version=$($version_cmd 2>/dev/null)
log_success "$name found ($version)"
else
log_success "$name found"
fi
return 0
fi
log_warning "$name not found. Installing..."
$install_cmd
}
backup_configs() {
# Clean up old backups first (keep last 5)
cleanup_old_backups 5
if [ "$PROMPT_BACKUP" = true ]; then
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting backup (--yes flag)"
BACKUP=true
elif [ -t 0 ]; then
read -p "Do you want to backup existing configurations? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
BACKUP=true
fi
else
log_info "Skipping backup prompt in non-interactive mode (use --backup to force backup)"
fi
fi
if [ "$BACKUP" = true ]; then
log_info "Creating backup at $BACKUP_DIR..."
execute "mkdir -p $BACKUP_DIR"
copy_config_dir "$HOME/.claude" "$BACKUP_DIR" "claude"
copy_config_dir "$HOME/.config/claude" "$BACKUP_DIR" "config-claude"
copy_config_dir "$HOME/.config/opencode" "$BACKUP_DIR" "opencode"
copy_config_dir "$HOME/.config/amp" "$BACKUP_DIR" "amp"
copy_config_dir "$HOME/.ccs" "$BACKUP_DIR" "ccs"
copy_config_dir "$HOME/.codex" "$BACKUP_DIR" "codex"
copy_config_dir "$HOME/.gemini" "$BACKUP_DIR" "gemini"
copy_config_file "$HOME/.config/ai-switcher/config.json" "$BACKUP_DIR/ai-switcher"
log_success "Backup completed: $BACKUP_DIR"
fi
}
install_claude_code() {
log_info "Installing Claude Code..."
if command -v claude &>/dev/null; then
log_warning "Claude Code is already installed ($(claude --version))"
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-skipping reinstall (--yes flag)"
return
elif [ -t 0 ]; then
read -p "Do you want to reinstall? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
return
fi
else
log_info "Skipping reinstall in non-interactive mode"
return
fi
fi
execute "npm install -g @anthropic-ai/claude-code"
log_success "Claude Code installed"
}
install_opencode() {
prompt_and_install() {
log_info "Installing OpenCode..."
if command -v opencode &>/dev/null; then
log_warning "OpenCode is already installed"
else
execute_installer "https://opencode.ai/install" "" "OpenCode"
log_success "OpenCode installed"
fi
}
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting OpenCode installation (--yes flag)"
prompt_and_install
elif [ -t 0 ]; then
read -p "Do you want to install OpenCode? (y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] && prompt_and_install || log_warning "Skipping OpenCode installation"
else
log_info "Installing OpenCode (non-interactive mode)..."
prompt_and_install
fi
}
install_amp() {
prompt_and_install() {
log_info "Installing Amp..."
if command -v amp &>/dev/null; then
log_warning "Amp is already installed"
else
execute_installer "https://ampcode.com/install.sh" "" "Amp"
fi
AMP_INSTALLED=true
log_success "Amp installed"
}
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting Amp installation (--yes flag)"
prompt_and_install
elif [ -t 0 ]; then
read -p "Do you want to install Amp? (y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] && prompt_and_install || log_warning "Skipping Amp installation"
else
log_info "Installing Amp (non-interactive mode)..."
prompt_and_install
fi
}
install_ccs() {
prompt_and_install() {
log_info "Installing CCS..."
if command -v ccs &>/dev/null; then
log_warning "CCS is already installed ($(ccs --version))"
else
execute "npm install -g @kaitranntt/ccs"
log_success "CCS installed"
fi
}
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting CCS installation (--yes flag)"
prompt_and_install
elif [ -t 0 ]; then
read -p "Do you want to install CCS (Claude Code Switch)? (y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] && prompt_and_install || log_warning "Skipping CCS installation"
else
log_info "Installing CCS (non-interactive mode)..."
prompt_and_install
fi
}
install_ai_switcher() {
prompt_and_install() {
log_info "Installing ai-switcher..."
if command -v ai-switcher &>/dev/null; then
log_warning "ai-switcher is already installed"
else
execute_installer "https://raw.githubusercontent.com/jellydn/ai-launcher/main/install.sh" "" "ai-switcher"
log_success "ai-switcher installed"
fi
}
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting ai-switcher installation (--yes flag)"
prompt_and_install
elif [ -t 0 ]; then
read -p "Do you want to install ai-switcher? (y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] && prompt_and_install || log_warning "Skipping ai-switcher installation"
else
log_info "Installing ai-switcher (non-interactive mode)..."
prompt_and_install
fi
}
install_codex() {
prompt_and_install() {
log_info "Installing Codex CLI..."
if command -v codex &>/dev/null; then
log_warning "Codex CLI is already installed"
else
execute "npm install -g @openai/codex"
log_success "Codex CLI installed"
fi
}
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting Codex installation (--yes flag)"
prompt_and_install
elif [ -t 0 ]; then
read -p "Do you want to install OpenAI Codex CLI? (y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] && prompt_and_install || log_warning "Skipping Codex CLI installation"
else
log_info "Installing Codex CLI (non-interactive mode)..."
prompt_and_install
fi
}
install_gemini() {
prompt_and_install() {
log_info "Installing Gemini CLI..."
if command -v gemini &>/dev/null; then
log_warning "Gemini CLI is already installed"
else
execute "npm install -g @google/gemini-cli"
log_success "Gemini CLI installed"
fi
}
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting Gemini CLI installation (--yes flag)"
prompt_and_install
elif [ -t 0 ]; then
read -p "Do you want to install Google Gemini CLI? (y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] && prompt_and_install || log_warning "Skipping Gemini CLI installation"
else
log_info "Installing Gemini CLI (non-interactive mode)..."
prompt_and_install
fi
}
# Helper: Copy non-marketplace skills from source to destination
# Usage: copy_non_marketplace_skills "source_dir" "dest_dir"
copy_non_marketplace_skills() {
local source_dir="$1"
local dest_dir="$2"
if [ -d "$source_dir" ] && [ "$(ls -A "$source_dir" 2>/dev/null)" ]; then
execute "rm -rf $dest_dir"
execute "mkdir -p $dest_dir"
for skill_dir in "$source_dir"/*; do
if [ -d "$skill_dir" ]; then
skill_name="$(basename "$skill_dir")"
case "$skill_name" in
prd|ralph|qmd-knowledge|codemap)
# Skip marketplace plugins
;;
*)
execute "cp -r \"$skill_dir\" \"$dest_dir/\""
;;
esac
fi
done
fi
}
# Helper: Install MCP server with interactive prompts
# Usage: install_mcp_interactive "name" "install_cmd" "description"
install_mcp_interactive() {
local name="$1"
local install_cmd="$2"
local description="$3"
if [ "$YES_TO_ALL" = true ]; then
log_info "Auto-accepting MCP server installation (--yes flag)"
if execute "$install_cmd"; then
log_success "$name MCP server added (global)"
else
log_warning "$name already installed or failed"
fi
elif [ -t 0 ]; then
read -p "Install $name MCP server ($description)? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if execute "$install_cmd"; then
log_success "$name MCP server added (global)"
else
log_warning "$name already installed or failed"
fi
fi
else
install_mcp_server "$name" "$install_cmd"
fi
}
copy_configurations() {
log_info "Copying configurations..."
# Create ~/.claude directory
execute "mkdir -p $HOME/.claude"
# Copy Claude Code configs
execute "cp $SCRIPT_DIR/configs/claude/settings.json $HOME/.claude/settings.json"
execute "cp $SCRIPT_DIR/configs/claude/mcp-servers.json $HOME/.claude/mcp-servers.json"
execute "cp $SCRIPT_DIR/configs/claude/CLAUDE.md $HOME/.claude/CLAUDE.md"
execute "rm -rf $HOME/.claude/commands"
execute "cp -r $SCRIPT_DIR/configs/claude/commands $HOME/.claude/"
if [ -d "$SCRIPT_DIR/configs/claude/agents" ]; then
execute "mkdir -p $HOME/.claude/agents"
execute "cp $SCRIPT_DIR/configs/claude/agents/* $HOME/.claude/agents/"
fi
# Add MCP servers using Claude Code CLI (globally, available in all projects)
if command -v claude &>/dev/null; then
log_info "Setting up Claude Code MCP servers (global scope)..."
install_mcp_interactive "context7" "claude mcp add --scope user --transport stdio context7 -- npx -y @upstash/context7-mcp@latest" "documentation lookup"
install_mcp_interactive "sequential-thinking" "claude mcp add --scope user --transport stdio sequential-thinking -- npx -y @modelcontextprotocol/server-sequential-thinking" "multi-step reasoning"
if command -v qmd &>/dev/null; then
install_mcp_interactive "qmd" "claude mcp add --scope user --transport stdio qmd -- qmd mcp" "knowledge management"
else
log_warning "qmd not found. MCP setup skipped. Install with: bun install -g https://github.com/tobi/qmd"
fi
log_success "MCP server setup complete (global scope)"
fi
log_success "Claude Code configs copied"
# Copy OpenCode configs
if [ -d "$HOME/.config/opencode" ] || command -v opencode &>/dev/null; then
execute "mkdir -p $HOME/.config/opencode"
execute "cp $SCRIPT_DIR/configs/opencode/opencode.json $HOME/.config/opencode/"
execute "rm -rf $HOME/.config/opencode/agent"
execute "cp -r $SCRIPT_DIR/configs/opencode/agent $HOME/.config/opencode/"
execute "rm -rf $HOME/.config/opencode/command"
execute "cp -r $SCRIPT_DIR/configs/opencode/command $HOME/.config/opencode/"
execute "rm -rf $HOME/.config/opencode/skill"
copy_non_marketplace_skills "$SCRIPT_DIR/configs/opencode/skill" "$HOME/.config/opencode/skill"
log_success "OpenCode configs copied"
fi
# Copy Amp configs
if [ -d "$HOME/.config/amp" ] || command -v amp &>/dev/null; then
execute "mkdir -p $HOME/.config/amp"
execute "cp $SCRIPT_DIR/configs/amp/settings.json $HOME/.config/amp/"
copy_non_marketplace_skills "$SCRIPT_DIR/configs/amp/skills" "$HOME/.config/amp/skills"
if [ -f "$SCRIPT_DIR/configs/amp/AGENTS.md" ]; then
execute "cp $SCRIPT_DIR/configs/amp/AGENTS.md $HOME/.config/amp/"
if [ -f "$HOME/.config/AGENTS.md" ]; then
cp "$HOME/.config/AGENTS.md" "$HOME/.config/AGENTS.md.bak"
log_warning "Backed up existing AGENTS.md to .bak"
fi
execute "cp $SCRIPT_DIR/configs/amp/AGENTS.md $HOME/.config/AGENTS.md"
fi
log_success "Amp configs copied"
fi
# Copy CCS configs
if [ -d "$HOME/.ccs" ] || command -v ccs &>/dev/null; then
execute "mkdir -p $HOME/.ccs"
execute "cp $SCRIPT_DIR/configs/ccs/*.yaml $HOME/.ccs/ 2>/dev/null || true"
execute "cp $SCRIPT_DIR/configs/ccs/*.json $HOME/.ccs/ 2>/dev/null || true"
execute "cp $SCRIPT_DIR/configs/ccs/*.settings.json $HOME/.ccs/ 2>/dev/null || true"
# Safely copy cliproxy (may contain running binaries)
if [ -d "$SCRIPT_DIR/configs/ccs/cliproxy" ]; then
safe_copy_dir "$SCRIPT_DIR/configs/ccs/cliproxy" "$HOME/.ccs/cliproxy"
fi
[ -d "$SCRIPT_DIR/configs/ccs/hooks" ] && execute "cp -r $SCRIPT_DIR/configs/ccs/hooks $HOME/.ccs/"
log_success "CCS configs copied"
fi
# Copy ai-switcher configs
if [ -d "$HOME/.config/ai-switcher" ] || [ -f "$HOME/.config/ai-switcher/config.json" ]; then
if copy_config_file "$SCRIPT_DIR/configs/ai-switcher/config.json" "$HOME/.config/ai-switcher"; then
log_success "ai-switcher configs copied"
else
log_info "ai-switcher config not found in source, preserving existing"
fi
fi
# Copy Codex CLI configs
if [ -d "$HOME/.codex" ] || command -v codex &>/dev/null; then
execute "mkdir -p $HOME/.codex"
copy_config_file "$SCRIPT_DIR/configs/codex/AGENTS.md" "$HOME/.codex/"
copy_config_file "$SCRIPT_DIR/configs/codex/config.json" "$HOME/.codex/"
if [ -f "$SCRIPT_DIR/configs/codex/config.toml" ]; then
if [ -f "$HOME/.codex/config.toml" ]; then
# Backup existing config before overwriting
execute "cp $HOME/.codex/config.toml $HOME/.codex/config.toml.bak"
log_success "Backed up existing config.toml to config.toml.bak"
fi
# Copy new config (whether or not there was an old one)
execute "cp $SCRIPT_DIR/configs/codex/config.toml $HOME/.codex/"
log_success "Copied Codex config.toml"
fi
log_success "Codex CLI configs copied (skills invoked via \$, prompts no longer needed)"
fi
# Copy Gemini CLI configs
if [ -d "$HOME/.gemini" ] || command -v gemini &>/dev/null; then
execute "mkdir -p $HOME/.gemini"
copy_config_file "$SCRIPT_DIR/configs/gemini/AGENTS.md" "$HOME/.gemini/"
copy_config_file "$SCRIPT_DIR/configs/gemini/GEMINI.md" "$HOME/.gemini/"
copy_config_file "$SCRIPT_DIR/configs/gemini/settings.json" "$HOME/.gemini/"
execute "rm -rf $HOME/.gemini/agents"
execute "cp -r $SCRIPT_DIR/configs/gemini/agents $HOME/.gemini/"
execute "rm -rf $HOME/.gemini/commands"
execute "cp -r $SCRIPT_DIR/configs/gemini/commands $HOME/.gemini/"
execute "rm -rf $HOME/.gemini/skills"
copy_non_marketplace_skills "$SCRIPT_DIR/configs/gemini/skills" "$HOME/.gemini/skills"
log_success "Gemini CLI configs copied"
fi
# Copy best practices and MEMORY.md
execute "mkdir -p $HOME/.ai-tools"
execute "cp $SCRIPT_DIR/configs/best-practices.md $HOME/.ai-tools/"
log_success "Best practices copied to ~/.ai-tools/"
[ -f "$SCRIPT_DIR/MEMORY.md" ] && execute "cp $SCRIPT_DIR/MEMORY.md $HOME/.ai-tools/" && log_success "MEMORY.md copied to ~/.ai-tools/ (reference copy)"
}
enable_plugins() {
log_info "Installing Claude Code plugins..."
# Ask for skill installation source
if [ -t 0 ]; then
log_info "How would you like to install community skills?"
printf "1) Local (from .claude-plugin folder) 2) Remote (from jellydn/my-ai-tools marketplace) [1/2]: "
read REPLY
echo
case "$REPLY" in
2) SKILL_INSTALL_SOURCE="remote" ;;
*) SKILL_INSTALL_SOURCE="local" ;;
esac
else
SKILL_INSTALL_SOURCE="local"
fi
official_plugins=(
"typescript-lsp@claude-plugins-official"
"pyright-lsp@claude-plugins-official"
"context7@claude-plugins-official"
"frontend-design@claude-plugins-official"
"learning-output-style@claude-plugins-official"
"swift-lsp@claude-plugins-official"
"lua-lsp@claude-plugins-official"
"code-simplifier@claude-plugins-official"
"rust-analyzer-lsp@claude-plugins-official"
"claude-md-management@claude-plugins-official"
)
# Community plugins (name, plugin_spec, marketplace_repo)
# Format: "name|plugin_spec|marketplace_repo"
community_plugins=(
"plannotator|plannotator@plannotator|backnotprop/plannotator"
"prd|prd@my-ai-tools|$SCRIPT_DIR"
"ralph|ralph@my-ai-tools|$SCRIPT_DIR"
"qmd-knowledge|qmd-knowledge@my-ai-tools|$SCRIPT_DIR"
"codemap|codemap@my-ai-tools|$SCRIPT_DIR"
"claude-hud|claude-hud@claude-hud|jarrodwatts/claude-hud"
"worktrunk|worktrunk@worktrunk|max-sixty/worktrunk"
)
install_plugin() {
local plugin="$1"
if [ "$YES_TO_ALL" = true ]; then
setup_tmpdir
if ! execute "claude plugin install '$plugin' 2>/dev/null"; then
log_warning "$plugin install failed (may already be installed)"
fi
elif [ -t 0 ]; then
read -p "Install $plugin? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
setup_tmpdir
execute "claude plugin install '$plugin' && log_success '$plugin installed' || log_warning '$plugin install failed (may already be installed)'"
fi
else
setup_tmpdir
if ! execute "claude plugin install '$plugin' 2>/dev/null"; then
log_warning "$plugin install failed (may already be installed)"
fi
fi
}
install_community_plugin() {
local name="$1"
local plugin_spec="$2"
local marketplace_repo="$3"
if [ "$YES_TO_ALL" = true ] || [ ! -t 0 ]; then
# Non-interactive or auto-install mode - install CLI tools if needed
case "$name" in
plannotator)
if ! command -v plannotator &>/dev/null; then
log_info "Installing Plannator CLI..."
execute_installer "https://plannotator.ai/install.sh" "" "Plannator CLI" || log_warning "Plannator installation failed"
fi
;;
qmd-knowledge)
if ! command -v qmd &>/dev/null && command -v bun &>/dev/null; then
log_info "Installing qmd CLI via bun..."
bun install -g https://github.com/tobi/qmd 2>&1 || log_warning "qmd installation failed"
fi
;;
worktrunk)
if ! command -v wt &>/dev/null && command -v brew &>/dev/null; then
log_info "Installing Worktrunk CLI via Homebrew..."
brew install worktrunk 2>&1 && wt config shell install 2>&1 || log_warning "Worktrunk installation failed"
fi
;;
esac
# Add marketplace and install plugin
setup_tmpdir
execute "claude plugin marketplace add '$marketplace_repo' 2>/dev/null || true"
# Clear any stale plugin cache that might cause cross-device link errors
execute "rm -rf '$HOME/.claude/plugins/cache/$name' 2>/dev/null || true"
if ! execute "claude plugin install '$plugin_spec' 2>/dev/null"; then
log_warning "$name plugin install failed (may already be installed)"
fi
elif [ -t 0 ]; then
read -p "Install $name? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Install CLI tool if needed
case "$name" in
plannotator)
if ! command -v plannotator &>/dev/null; then
log_info "Installing Plannator CLI (this may take a moment)..."
if execute_installer "https://plannotator.ai/install.sh" "" "Plannator CLI"; then
log_success "Plannator CLI installed"
else
log_warning "Plannator installation failed or was cancelled"
fi
else
log_info "Plannator CLI already installed"
fi
;;
qmd-knowledge)
if ! command -v qmd &>/dev/null; then
if command -v bun &>/dev/null; then
log_info "Installing qmd CLI via bun..."
if bun install -g https://github.com/tobi/qmd 2>&1; then
log_success "qmd CLI installed"
else
log_warning "qmd installation failed"
fi
else
log_warning "bun is required for qmd. Install from https://bun.sh"
fi
else
log_info "qmd CLI already installed"
fi
;;
worktrunk)
if ! command -v wt &>/dev/null; then
if command -v brew &>/dev/null; then
log_info "Installing Worktrunk CLI via Homebrew (this may take a moment)..."
if brew install worktrunk 2>&1 && wt config shell install 2>&1; then
log_success "Worktrunk CLI installed"
else
log_warning "Worktrunk installation failed"
fi
else
log_warning "Homebrew is required for worktrunk. Install from https://brew.sh"
fi
else
log_info "Worktrunk CLI already installed"
fi
;;
esac
# Add marketplace first
setup_tmpdir
if ! execute "claude plugin marketplace add '$marketplace_repo' 2>/dev/null"; then
log_info "Marketplace $marketplace_repo may already be added"
fi
# Clear any stale plugin cache that might cause cross-device link errors
execute "rm -rf '$HOME/.claude/plugins/cache/$name' 2>/dev/null || true"
# Install plugin
if execute "claude plugin install '$plugin_spec'"; then
log_success "$name installed"
else
log_warning "$name install failed (may already be installed)"
fi
fi
fi
}
# Extract compatibility field from SKILL.md
# Returns 0 (true) if skill is compatible with platform, 1 (false) otherwise
skill_is_compatible_with() {
local skill_dir="$1"
local platform="$2"
local skill_md="$skill_dir/SKILL.md"
if [ ! -f "$skill_md" ]; then
# No SKILL.md means assume compatible with all
return 0
fi
# Extract compatibility line from frontmatter
local compat_line=$(awk '/^compatibility:/ {print; exit}' "$skill_md" 2>/dev/null)
if [ -z "$compat_line" ]; then
# No compatibility field means assume compatible with all
return 0
fi
# Check if platform is in the compatibility list
# Compatibility format: "compatibility: claude, opencode, amp, codex"
if echo "$compat_line" | grep -qi "\\b$platform\\b"; then
return 0
else
return 1
fi
}
install_local_skills() {
if [ ! -d "$SCRIPT_DIR/.claude-plugin/plugins" ]; then
log_info ".claude-plugin/plugins folder not found, skipping local skills"
return
fi
log_info "Installing skills from local .claude-plugin/plugins folder..."
# Define target directories
CLAUDE_SKILLS_DIR="$HOME/.claude/skills"
OPENCODE_SKILL_DIR="$HOME/.config/opencode/skill"
OPENCODE_COMMAND_DIR="$HOME/.config/opencode/command/ai"
AMP_SKILLS_DIR="$HOME/.config/amp/skills"
CODEX_SKILLS_DIR="$HOME/.codex/skills"
GEMINI_SKILLS_DIR="$HOME/.gemini/skills"
# Copy to Claude Code (~/.claude/skills/)
if [ -d "$CLAUDE_SKILLS_DIR" ]; then
# Remove existing skills safely using rm with quoted path
for existing_skill in "$CLAUDE_SKILLS_DIR"/*; do
[ -d "$existing_skill" ] && rm -rf "$existing_skill"
done
fi
mkdir -p "$CLAUDE_SKILLS_DIR"
# Copy to OpenCode (~/.config/opencode/skill/)
if [ -d "$OPENCODE_SKILL_DIR" ]; then
for existing_skill in "$OPENCODE_SKILL_DIR"/*; do
[ -d "$existing_skill" ] && rm -rf "$existing_skill"
done
fi
mkdir -p "$OPENCODE_SKILL_DIR"
# Create OpenCode commands directory
mkdir -p "$OPENCODE_COMMAND_DIR"
# Copy to Amp (~/.config/amp/skills/)
if [ -d "$AMP_SKILLS_DIR" ]; then
for existing_skill in "$AMP_SKILLS_DIR"/*; do
[ -d "$existing_skill" ] && rm -rf "$existing_skill"
done
fi
mkdir -p "$AMP_SKILLS_DIR"
# Copy to Codex CLI (~/.codex/skills/)
if [ -d "$CODEX_SKILLS_DIR" ]; then
for existing_skill in "$CODEX_SKILLS_DIR"/*; do
[ -d "$existing_skill" ] && rm -rf "$existing_skill"
done
fi
mkdir -p "$CODEX_SKILLS_DIR"
# Copy to Gemini CLI (~/.gemini/skills/)
if [ -d "$GEMINI_SKILLS_DIR" ]; then
for existing_skill in "$GEMINI_SKILLS_DIR"/*; do
[ -d "$existing_skill" ] && rm -rf "$existing_skill"
done
fi
mkdir -p "$GEMINI_SKILLS_DIR"
# Copy all skills from plugins folder to targets
for skill_dir in "$SCRIPT_DIR/.claude-plugin/plugins"/*; do
if [ -d "$skill_dir" ]; then
skill_name=$(basename "$skill_dir")
# Check compatibility and copy to each platform
if skill_is_compatible_with "$skill_dir" "claude"; then
cp -r "$skill_dir" "$CLAUDE_SKILLS_DIR/"
log_success "Copied $skill_name to Claude Code"
else
log_info "Skipped $skill_name for Claude Code (not compatible)"
fi
if skill_is_compatible_with "$skill_dir" "opencode"; then
cp -r "$skill_dir" "$OPENCODE_SKILL_DIR/"
log_success "Copied $skill_name to OpenCode"
else
log_info "Skipped $skill_name for OpenCode (not compatible)"
fi
if skill_is_compatible_with "$skill_dir" "amp"; then
cp -r "$skill_dir" "$AMP_SKILLS_DIR/"
log_success "Copied $skill_name to Amp"
else
log_info "Skipped $skill_name for Amp (not compatible)"
fi
if skill_is_compatible_with "$skill_dir" "codex"; then
cp -r "$skill_dir" "$CODEX_SKILLS_DIR/"
log_success "Copied $skill_name to Codex CLI"
else
log_info "Skipped $skill_name for Codex CLI (not compatible)"
fi
if skill_is_compatible_with "$skill_dir" "gemini"; then
cp -r "$skill_dir" "$GEMINI_SKILLS_DIR/"
log_success "Copied $skill_name to Gemini CLI"
else
log_info "Skipped $skill_name for Gemini CLI (not compatible)"
fi
# Generate OpenCode command from skill (only if compatible)
if skill_is_compatible_with "$skill_dir" "opencode"; then
generate_opencode_command "$skill_dir" "$OPENCODE_COMMAND_DIR"
fi
fi
done
}
# Generate OpenCode command file from skill SKILL.md
generate_opencode_command() {
local skill_dir="$1"
local command_dir="$2"
local skill_name=$(basename "$skill_dir")
local skill_md="$skill_dir/SKILL.md"
if [ ! -f "$skill_md" ]; then
log_warning "No SKILL.md found for $skill_name, skipping command generation"
return
fi
# Description for command - simple and consistent