-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1527 lines (1272 loc) · 54.5 KB
/
install.sh
File metadata and controls
executable file
·1527 lines (1272 loc) · 54.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
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
#
# Cursor Worktree Installer for Drupal/DDEV
# https://github.com/droptica/cursor-parallel-agents-for-drupal
#
# Usage:
# curl -sL https://raw.githubusercontent.com/droptica/cursor-parallel-agents-for-drupal/main/install.sh | bash
# curl -sL .../install.sh | bash -s -- --type=multisite
# curl -sL .../install.sh | bash -s -- --dry-run
#
set -e
#------------------------------------------------------------------------------
# Configuration
#------------------------------------------------------------------------------
REPO_URL="https://github.com/droptica/cursor-parallel-agents-for-drupal.git"
REPO_SSH_URL="git@github.com:droptica/cursor-parallel-agents-for-drupal.git"
VERSION="1.0.0"
TEMP_DIR=""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
#------------------------------------------------------------------------------
# Logging functions
#------------------------------------------------------------------------------
log() { echo -e "[$(date '+%H:%M:%S')] [$1] ${@:2}"; }
log_info() { log "${BLUE}INFO${NC}" "$@"; }
log_success() { log "${GREEN}OK${NC}" "$@"; }
log_warn() { log "${YELLOW}WARN${NC}" "$@"; }
log_error() { log "${RED}ERROR${NC}" "$@"; }
#------------------------------------------------------------------------------
# Header
#------------------------------------------------------------------------------
show_header() {
clear 2>/dev/null || true
echo ""
echo -e "${CYAN}${BOLD}"
echo " ██████╗██╗ ██╗██████╗ ███████╗ ██████╗ ██████╗ "
echo " ██╔════╝██║ ██║██╔══██╗██╔════╝██╔═══██╗██╔══██╗"
echo " ██║ ██║ ██║██████╔╝███████╗██║ ██║██████╔╝"
echo " ██║ ██║ ██║██╔══██╗╚════██║██║ ██║██╔══██╗"
echo " ╚██████╗╚██████╔╝██║ ██║███████║╚██████╔╝██║ ██║"
echo " ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝"
echo ""
echo " ██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗████████╗██████╗ ███████╗███████╗"
echo " ██║ ██║██╔═══██╗██╔══██╗██║ ██╔╝╚══██╔══╝██╔══██╗██╔════╝██╔════╝"
echo " ██║ █╗ ██║██║ ██║██████╔╝█████╔╝ ██║ ██████╔╝█████╗ █████╗ "
echo " ██║███╗██║██║ ██║██╔══██╗██╔═██╗ ██║ ██╔══██╗██╔══╝ ██╔══╝ "
echo " ╚███╔███╔╝╚██████╔╝██║ ██║██║ ██╗ ██║ ██║ ██║███████╗███████╗"
echo " ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝"
echo -e "${NC}"
echo -e "${BLUE} PARALLEL AGENTS FOR DRUPAL/DDEV${NC}"
echo ""
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN} Run multiple AI agents in isolated environments${NC}"
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e " Enable Cursor Parallel Agents for your Drupal project."
echo -e " Each agent runs in an isolated Git worktree with its own"
echo -e " DDEV environment and database."
echo ""
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${MAGENTA}${BOLD}Built with ❤️ by Droptica${NC}"
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "${YELLOW}Solid Open Source solutions for ambitious companies${NC}"
echo ""
echo -e "${BOLD}What we do:${NC}"
echo ""
echo -e " ${BOLD}Create:${NC} Open Intranet, Droopler CMS, Druscan"
echo -e " ${BOLD}AI Dev:${NC} AI chatbots (RAG), autonomous agents, OpenAI/Claude"
echo -e " integrations, custom AI models, workflow automation"
echo -e " ${BOLD}Customize:${NC} Drupal, Mautic, Sylius, Symfony"
echo -e " ${BOLD}Support:${NC} Security, updates, training, monitoring 24/7"
echo ""
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${CYAN}Website: ${BLUE}https://www.droptica.com${NC}"
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
}
#------------------------------------------------------------------------------
# Help
#------------------------------------------------------------------------------
show_help() {
echo ""
echo -e "${CYAN}${BOLD}Cursor Worktree Installer for Drupal/DDEV v${VERSION}${NC}"
echo -e "${MAGENTA}Built by Droptica${NC} - ${BLUE}https://www.droptica.com${NC}"
echo ""
echo -e "${BOLD}Usage:${NC}"
echo " curl -sL ${REPO_RAW_URL}/install.sh | bash"
echo " curl -sL .../install.sh | bash -s -- [OPTIONS]"
echo ""
echo -e "${BOLD}Options:${NC}"
echo " --type=singlesite Force singlesite installation"
echo " --type=multisite Force multisite installation"
echo " --dry-run Show what would be installed without making changes"
echo " --help, -h Show this help message"
echo ""
echo -e "${BOLD}Requirements:${NC}"
echo " - DDEV installed and configured"
echo " - Git repository initialized"
echo " - Drupal project with web/ or docroot/ structure"
echo ""
echo -e "${BOLD}Documentation:${NC} ${BLUE}https://github.com/droptica/cursor-parallel-agents-for-drupal${NC}"
echo ""
}
#------------------------------------------------------------------------------
# Parse arguments
#------------------------------------------------------------------------------
DRY_RUN=false
FORCE_TYPE=""
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run) DRY_RUN=true; shift ;;
--type=*) FORCE_TYPE="${1#*=}"; shift ;;
--help|-h) show_help; exit 0 ;;
*) log_error "Unknown option: $1"; show_help; exit 1 ;;
esac
done
#------------------------------------------------------------------------------
# Requirement checks
#------------------------------------------------------------------------------
check_requirements() {
log_info "Checking requirements..."
# Must have curl
if ! command -v curl &>/dev/null; then
log_error "curl is not installed"
exit 1
fi
# Must have DDEV
if ! command -v ddev &>/dev/null; then
log_error "DDEV is not installed. Install from: https://ddev.readthedocs.io/"
exit 1
fi
log_success "DDEV found: $(ddev --version 2>/dev/null | head -1 || echo 'unknown version')"
# Must be in DDEV project
if [[ ! -f ".ddev/config.yaml" ]]; then
log_error "Not in a DDEV project directory (no .ddev/config.yaml found)"
log_error "Run this command from your Drupal project root"
exit 1
fi
log_success "DDEV project found"
# Must have Git
if ! command -v git &>/dev/null; then
log_error "Git is not installed"
exit 1
fi
log_success "Git found"
# Must be Git repository
if [[ ! -d ".git" ]]; then
log_error "Not a Git repository. Initialize with: git init"
exit 1
fi
log_success "Git repository found"
}
#------------------------------------------------------------------------------
# Project detection
#------------------------------------------------------------------------------
detect_web_root() {
if [[ -d "web/sites" ]]; then
echo "web"
elif [[ -d "docroot/sites" ]]; then
echo "docroot"
else
log_error "Cannot detect web root (web/ or docroot/)"
exit 1
fi
}
get_project_name() {
grep "^name:" .ddev/config.yaml | awk '{print $2}' | tr -d '"' | tr -d "'"
}
detect_project_type() {
local web_root
web_root=$(detect_web_root)
local sites_dir="${web_root}/sites"
# Count site directories (exclude default, all, simpletest)
local site_count=0
if [[ -d "$sites_dir" ]]; then
site_count=$(find "$sites_dir" -maxdepth 1 -type d \
! -name "sites" ! -name "default" ! -name "all" ! -name "simpletest" \
-name "*.*" 2>/dev/null | wc -l | tr -d ' ')
fi
# Check for additional_hostnames in config.yaml
local has_hostnames=0
if grep -q "additional_hostnames:" .ddev/config.yaml 2>/dev/null; then
has_hostnames=1
fi
# Check for multiple databases in hooks
local has_multi_db=0
if grep -q "CREATE DATABASE" .ddev/config.yaml 2>/dev/null; then
has_multi_db=1
fi
if [[ $site_count -gt 0 ]] || [[ $has_hostnames -eq 1 && $has_multi_db -eq 1 ]]; then
echo "multisite"
else
echo "singlesite"
fi
}
#------------------------------------------------------------------------------
# Multisite configuration extraction
#------------------------------------------------------------------------------
# Global arrays to store extracted multisite config
MULTISITE_SITES=()
MULTISITE_DBS=()
MULTISITE_HOSTNAMES=()
MULTISITE_SITE_TO_HOSTNAME=()
MULTISITE_SITE_TO_DB=()
extract_multisite_config() {
local web_root="$1"
local project_name="$2"
log_info "Extracting multisite configuration..."
# 1. Find site directories (e.g., pl.droptica.com, de.example.com)
MULTISITE_SITES=("default")
if [[ -d "${web_root}/sites" ]]; then
while IFS= read -r site_dir; do
if [[ -n "$site_dir" ]]; then
local site_name=$(basename "$site_dir")
MULTISITE_SITES+=("$site_name")
fi
done < <(find "${web_root}/sites" -maxdepth 1 -type d \
! -name "sites" ! -name "default" ! -name "all" ! -name "simpletest" \
-name "*.*" 2>/dev/null | sort)
fi
# 2. Extract additional_hostnames from .ddev/config.yaml
MULTISITE_HOSTNAMES=()
if grep -q "additional_hostnames:" .ddev/config.yaml 2>/dev/null; then
while IFS= read -r hostname; do
hostname=$(echo "$hostname" | sed 's/^[[:space:]]*-[[:space:]]*//' | tr -d '"' | tr -d "'")
if [[ -n "$hostname" && "$hostname" != "additional_hostnames:" ]]; then
MULTISITE_HOSTNAMES+=("$hostname")
fi
done < <(grep -A 50 "additional_hostnames:" .ddev/config.yaml | grep "^\s*-" | head -20)
fi
# 3. Extract database names from hooks (CREATE DATABASE IF NOT EXISTS xxx)
MULTISITE_DBS=("db")
if grep -q "CREATE DATABASE" .ddev/config.yaml 2>/dev/null; then
while IFS= read -r db_name; do
if [[ -n "$db_name" && "$db_name" != "db" ]]; then
MULTISITE_DBS+=("$db_name")
fi
done < <(grep -oP "CREATE DATABASE IF NOT EXISTS \K[a-zA-Z0-9_]+" .ddev/config.yaml 2>/dev/null | sort -u)
fi
# 4. Try to map sites to hostnames and databases
# This uses heuristics based on common naming patterns
MULTISITE_SITE_TO_HOSTNAME=()
MULTISITE_SITE_TO_DB=()
for site in "${MULTISITE_SITES[@]}"; do
if [[ "$site" == "default" ]]; then
MULTISITE_SITE_TO_HOSTNAME+=("default:${project_name}")
MULTISITE_SITE_TO_DB+=("default:db")
else
# Extract prefix from site directory (e.g., "pl" from "pl.droptica.com")
local prefix=$(echo "$site" | cut -d'.' -f1)
# Find matching hostname
local matched_hostname=""
for hostname in "${MULTISITE_HOSTNAMES[@]}"; do
if [[ "$hostname" == "${prefix}."* || "$hostname" == "${prefix}" ]]; then
matched_hostname="$hostname"
break
fi
done
if [[ -z "$matched_hostname" ]]; then
matched_hostname="${prefix}.${project_name}"
fi
MULTISITE_SITE_TO_HOSTNAME+=("${site}:${matched_hostname}")
# Find matching database
local matched_db=""
for db in "${MULTISITE_DBS[@]}"; do
if [[ "$db" == "$prefix" || "$db" == "${prefix}_db" ]]; then
matched_db="$db"
break
fi
done
if [[ -z "$matched_db" ]]; then
matched_db="$prefix"
# Add to DBS if not exists
local db_exists=false
for existing_db in "${MULTISITE_DBS[@]}"; do
if [[ "$existing_db" == "$matched_db" ]]; then
db_exists=true
break
fi
done
if [[ "$db_exists" == "false" ]]; then
MULTISITE_DBS+=("$matched_db")
fi
fi
MULTISITE_SITE_TO_DB+=("${site}:${matched_db}")
fi
done
# Log detected configuration
log_info "Detected sites: ${MULTISITE_SITES[*]}"
log_info "Detected databases: ${MULTISITE_DBS[*]}"
log_info "Detected hostnames: ${MULTISITE_HOSTNAMES[*]}"
}
#------------------------------------------------------------------------------
# Repository cloning
#------------------------------------------------------------------------------
clone_repo() {
if [[ -n "$TEMP_DIR" && -d "$TEMP_DIR" ]]; then
return 0 # Already cloned
fi
TEMP_DIR=$(mktemp -d)
log_info "Cloning repository to temporary directory..."
# Try SSH first (for private repo access), then HTTPS
if git clone --depth 1 --quiet "$REPO_SSH_URL" "$TEMP_DIR" 2>/dev/null; then
log_success "Repository cloned via SSH"
elif git clone --depth 1 --quiet "$REPO_URL" "$TEMP_DIR" 2>/dev/null; then
log_success "Repository cloned via HTTPS"
else
log_error "Failed to clone repository"
log_error "Make sure you have access to: $REPO_URL"
log_error "For private repos, ensure SSH keys are configured"
rm -rf "$TEMP_DIR"
exit 1
fi
}
cleanup_temp() {
if [[ -n "$TEMP_DIR" && -d "$TEMP_DIR" ]]; then
rm -rf "$TEMP_DIR"
log_info "Cleaned up temporary files"
fi
}
# Ensure cleanup on exit
trap cleanup_temp EXIT
copy_template() {
local src="$1"
local dest="$2"
if [[ "$DRY_RUN" == "true" ]]; then
log_info "[DRY-RUN] Would copy: $src → $dest"
return
fi
mkdir -p "$(dirname "$dest")"
if [[ ! -f "${TEMP_DIR}/${src}" ]]; then
log_error "Template file not found: $src"
exit 1
fi
cp "${TEMP_DIR}/${src}" "$dest"
}
install_singlesite() {
local project_name
project_name=$(get_project_name)
log_info "Installing singlesite configuration..."
# Clone repository to temp directory
clone_repo
# Create directories
mkdir -p .cursor/lib
mkdir -p .ddev/commands/host
# Copy common templates
copy_template "templates/common/.cursor/worktrees.json" ".cursor/worktrees.json"
copy_template "templates/common/.cursor/cleanup-worktree.sh" ".cursor/cleanup-worktree.sh"
copy_template "templates/common/.cursor/lib/logging.sh" ".cursor/lib/logging.sh"
# Copy singlesite-specific templates
copy_template "templates/singlesite/.cursor/setup-worktree.sh" ".cursor/setup-worktree.sh"
copy_template "templates/singlesite/.cursor/README.md" ".cursor/README.md"
copy_template "templates/singlesite/.ddev/commands/host/export-snapshot" ".ddev/commands/host/export-snapshot"
copy_template "templates/singlesite/.ddev/commands/host/worktree-status" ".ddev/commands/host/worktree-status"
if [[ "$DRY_RUN" == "true" ]]; then
log_info "[DRY-RUN] Would replace __PROJECT_NAME__ with ${project_name}"
return
fi
# Replace placeholders
log_info "Configuring templates for project: ${project_name}"
# macOS compatible sed
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' "s/__PROJECT_NAME__/${project_name}/g" .cursor/setup-worktree.sh
sed -i '' "s/__PROJECT_NAME__/${project_name}/g" .cursor/cleanup-worktree.sh
sed -i '' "s/__PROJECT_NAME__/${project_name}/g" .cursor/README.md
sed -i '' "s/__PROJECT_NAME__/${project_name}/g" .ddev/commands/host/worktree-status
else
sed -i "s/__PROJECT_NAME__/${project_name}/g" .cursor/setup-worktree.sh
sed -i "s/__PROJECT_NAME__/${project_name}/g" .cursor/cleanup-worktree.sh
sed -i "s/__PROJECT_NAME__/${project_name}/g" .cursor/README.md
sed -i "s/__PROJECT_NAME__/${project_name}/g" .ddev/commands/host/worktree-status
fi
# Set permissions
chmod +x .cursor/*.sh
chmod +x .cursor/lib/*.sh 2>/dev/null || true
chmod +x .ddev/commands/host/*
log_success "Singlesite configuration installed"
}
install_multisite() {
local project_name
project_name=$(get_project_name)
local web_root
web_root=$(detect_web_root)
log_info "Installing multisite configuration..."
# Extract multisite configuration from existing project
extract_multisite_config "$web_root" "$project_name"
if [[ "$DRY_RUN" == "true" ]]; then
log_info "[DRY-RUN] Would generate multisite configuration"
log_info "[DRY-RUN] Sites: ${MULTISITE_SITES[*]}"
log_info "[DRY-RUN] DBs: ${MULTISITE_DBS[*]}"
return
fi
# Clone repository to temp directory
clone_repo
# Create directories
mkdir -p .cursor/lib
mkdir -p .ddev/commands/host
# Copy common templates
copy_template "templates/common/.cursor/worktrees.json" ".cursor/worktrees.json"
copy_template "templates/common/.cursor/cleanup-worktree.sh" ".cursor/cleanup-worktree.sh"
copy_template "templates/common/.cursor/lib/logging.sh" ".cursor/lib/logging.sh"
# Copy multisite-specific templates
copy_template "templates/multisite/.cursor/README.md" ".cursor/README.md"
copy_template "templates/multisite/.ddev/commands/host/worktree-status" ".ddev/commands/host/worktree-status"
# Generate setup-worktree.sh dynamically
generate_multisite_setup_worktree "$project_name" "$web_root"
# Generate prerequisites.sh dynamically
generate_multisite_prerequisites "$project_name"
# Generate export-multisite-snapshots dynamically
generate_multisite_export_snapshots
# Patch sites.php for worktree support
patch_sites_php "$project_name" "$web_root"
# Replace __PROJECT_NAME__ in downloaded templates
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' "s/__PROJECT_NAME__/${project_name}/g" .cursor/cleanup-worktree.sh
sed -i '' "s/__PROJECT_NAME__/${project_name}/g" .cursor/README.md
sed -i '' "s/__PROJECT_NAME__/${project_name}/g" .ddev/commands/host/worktree-status
else
sed -i "s/__PROJECT_NAME__/${project_name}/g" .cursor/cleanup-worktree.sh
sed -i "s/__PROJECT_NAME__/${project_name}/g" .cursor/README.md
sed -i "s/__PROJECT_NAME__/${project_name}/g" .ddev/commands/host/worktree-status
fi
# Set permissions
chmod +x .cursor/*.sh
chmod +x .cursor/lib/*.sh
chmod +x .ddev/commands/host/*
log_success "Multisite configuration installed (auto-configured)"
}
#------------------------------------------------------------------------------
# Generate setup-worktree.sh for multisite
#------------------------------------------------------------------------------
generate_multisite_setup_worktree() {
local project_name="$1"
local web_root="$2"
log_info "Generating setup-worktree.sh..."
# Build SITES array
local sites_array="SITES=(\n"
for site in "${MULTISITE_SITES[@]}"; do
sites_array+=" \"${site}\"\n"
done
sites_array+=")"
# Build DB_NAMES array
local db_names_array="DB_NAMES=(\n"
for db in "${MULTISITE_DBS[@]}"; do
db_names_array+=" \"${db}\"\n"
done
db_names_array+=")"
# Build FILES_DIRS array
local files_dirs_array="FILES_DIRS=(\n"
files_dirs_array+=" \"\${WEB_ROOT}/sites/default/files\"\n"
for site in "${MULTISITE_SITES[@]}"; do
if [[ "$site" != "default" ]]; then
files_dirs_array+=" \"\${WEB_ROOT}/sites/${site}/files\"\n"
fi
done
files_dirs_array+=")"
# Build ADDITIONAL_HOSTNAMES for config.local.yaml
local additional_hostnames=""
for site in "${MULTISITE_SITES[@]}"; do
if [[ "$site" != "default" ]]; then
local prefix=$(echo "$site" | cut -d'.' -f1)
additional_hostnames+=" - ${prefix}.\${PROJECT_NAME}\n"
fi
done
# Build DB imports
local db_imports="# Import main database\n"
db_imports+="import_db \"db\" \"\${ROOT_WORKTREE_PATH}/\${SNAPSHOTS_DIR}/main.sql.gz\" \"Main DB\"\n"
for mapping in "${MULTISITE_SITE_TO_DB[@]}"; do
local site="${mapping%%:*}"
local db="${mapping##*:}"
if [[ "$site" != "default" && "$db" != "db" ]]; then
local prefix=$(echo "$site" | cut -d'.' -f1)
db_imports+="\nimport_db \"${db}\" \"\${ROOT_WORKTREE_PATH}/\${SNAPSHOTS_DIR}/${db}.sql.gz\" \"${prefix^} DB\""
fi
done
# Build required snapshots for prerequisites
local required_snapshots="\"main.sql.gz\""
for db in "${MULTISITE_DBS[@]}"; do
if [[ "$db" != "db" ]]; then
required_snapshots+="\n \"${db}.sql.gz\""
fi
done
cat > .cursor/setup-worktree.sh << SETUP_EOF
#!/bin/bash
#
# Cursor Worktree Setup Script for Drupal/DDEV (Multisite)
# Auto-generated by cursor-parallel-agents-for-drupal installer
#
# Project: ${project_name}
# Sites: ${MULTISITE_SITES[*]}
# Databases: ${MULTISITE_DBS[*]}
#
set -e
#------------------------------------------------------------------------------
# Configuration
#------------------------------------------------------------------------------
SCRIPT_DIR="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
source "\${SCRIPT_DIR}/lib/logging.sh"
# Worktree ID - extract from environment, argument, or current directory
# Use bash parameter expansion \${var##*/} instead of basename for reliability
RAW_ID=""
if [[ -n "\$CURSOR_WORKTREE_ID" ]]; then
RAW_ID="\$CURSOR_WORKTREE_ID"
elif [[ -n "\${1:-}" ]]; then
RAW_ID="\$1"
else
RAW_ID="\$(pwd)"
fi
# Extract basename using parameter expansion (more reliable than basename command)
WORKTREE_ID="\${RAW_ID##*/}"
# Validate: WORKTREE_ID must not be empty or contain path separators
if [[ -z "\$WORKTREE_ID" ]] || [[ "\$WORKTREE_ID" == */* ]]; then
log_error "Invalid WORKTREE_ID: '\${WORKTREE_ID}' (from: '\${RAW_ID}')"
exit 1
fi
# Auto-detect ROOT_WORKTREE_PATH if not set
if [[ -z "\$ROOT_WORKTREE_PATH" ]]; then
# In a git worktree, .git is a file pointing to the main repo
if [[ -f ".git" ]]; then
# Get the main worktree path (first line from git worktree list)
ROOT_WORKTREE_PATH=\$(git worktree list --porcelain | grep "^worktree " | head -1 | cut -d' ' -f2-)
fi
fi
if [[ -z "\$ROOT_WORKTREE_PATH" ]]; then
log_error "Could not detect ROOT_WORKTREE_PATH - are you in a git worktree?"
exit 1
fi
PROJECT_NAME="${project_name}-\${WORKTREE_ID}"
SNAPSHOTS_DIR=".ddev/db-dumps"
WEB_ROOT="${web_root}"
# Multisite configuration (auto-detected)
$(echo -e "$sites_array")
$(echo -e "$db_names_array")
$(echo -e "$files_dirs_array")
#------------------------------------------------------------------------------
# Helper functions
#------------------------------------------------------------------------------
import_db() {
local db_name="\$1"
local snapshot_file="\$2"
local description="\$3"
if [[ ! -f "\$snapshot_file" ]]; then
log_warn "Snapshot not found: \${snapshot_file}"
return 1
fi
log_info "Importing \${description}..."
if [[ "\$db_name" == "db" ]]; then
ddev import-db --file="\$snapshot_file"
else
ddev import-db --database="\$db_name" --file="\$snapshot_file"
fi
log_success "Imported \${description}"
}
#------------------------------------------------------------------------------
# Validation
#------------------------------------------------------------------------------
log_step "Validating environment"
log_info "Worktree ID: \${WORKTREE_ID}"
log_info "Project name: \${PROJECT_NAME}"
log_info "Root worktree: \${ROOT_WORKTREE_PATH}"
#------------------------------------------------------------------------------
# Create fresh database snapshots from main project
#------------------------------------------------------------------------------
log_step "Creating fresh database snapshots"
SNAPSHOT_TOTAL_START=\$(date +%s)
# Ensure snapshots directory exists
mkdir -p "\${ROOT_WORKTREE_PATH}/\${SNAPSHOTS_DIR}"
# Export databases from main project context
pushd "\${ROOT_WORKTREE_PATH}" > /dev/null
# Export main database
log_info "Exporting main database..."
SNAP_START=\$(date +%s)
if ! ddev export-db --gzip --file="\${SNAPSHOTS_DIR}/main.sql.gz" 2>&1; then
log_error "Failed to create main database snapshot"
popd > /dev/null
exit 1
fi
SNAP_END=\$(date +%s)
SNAP_SIZE=\$(du -h "\${SNAPSHOTS_DIR}/main.sql.gz" | cut -f1)
log_success "Main DB snapshot: \${SNAP_SIZE} in \$((SNAP_END - SNAP_START))s"
# Export additional databases
for db in "\${DB_NAMES[@]}"; do
if [[ "\$db" != "db" ]]; then
log_info "Exporting \${db} database..."
SNAP_START=\$(date +%s)
if ddev export-db --database="\$db" --gzip --file="\${SNAPSHOTS_DIR}/\${db}.sql.gz" 2>&1; then
SNAP_END=\$(date +%s)
SNAP_SIZE=\$(du -h "\${SNAPSHOTS_DIR}/\${db}.sql.gz" | cut -f1)
log_success "\${db} DB snapshot: \${SNAP_SIZE} in \$((SNAP_END - SNAP_START))s"
else
log_warn "Could not export \${db} database (may not exist yet)"
fi
fi
done
popd > /dev/null
SNAPSHOT_TOTAL_END=\$(date +%s)
log_success "All snapshots created in \$((SNAPSHOT_TOTAL_END - SNAPSHOT_TOTAL_START))s"
#------------------------------------------------------------------------------
# Copy DDEV configuration from main project
#------------------------------------------------------------------------------
log_step "Copying DDEV configuration"
if [[ ! -d ".ddev" ]]; then
if [[ -d "\${ROOT_WORKTREE_PATH}/.ddev" ]]; then
# Copy .ddev directory from main project (excluding runtime files)
mkdir -p .ddev
cp -r "\${ROOT_WORKTREE_PATH}/.ddev/config.yaml" .ddev/ 2>/dev/null || true
cp -r "\${ROOT_WORKTREE_PATH}/.ddev/providers" .ddev/ 2>/dev/null || true
cp -r "\${ROOT_WORKTREE_PATH}/.ddev/commands" .ddev/ 2>/dev/null || true
cp -r "\${ROOT_WORKTREE_PATH}/.ddev/docker-compose.*.yaml" .ddev/ 2>/dev/null || true
cp -r "\${ROOT_WORKTREE_PATH}/.ddev/.gitignore" .ddev/ 2>/dev/null || true
log_success "Copied .ddev configuration from main project"
else
log_error "Main project .ddev directory not found: \${ROOT_WORKTREE_PATH}/.ddev"
exit 1
fi
else
log_info ".ddev directory already exists"
fi
#------------------------------------------------------------------------------
# Generate DDEV local configuration
#------------------------------------------------------------------------------
log_step "Generating DDEV local configuration"
cat > .ddev/config.local.yaml << EOF
# Auto-generated for Cursor worktree: \${WORKTREE_ID}
# DO NOT COMMIT THIS FILE
name: \${PROJECT_NAME}
override_config: true
additional_hostnames:
$(echo -e "$additional_hostnames")
web_environment:
- PLATFORM_ENVIRONMENT=staging
- CURSOR_WORKTREE_ID=\${WORKTREE_ID}
EOF
log_success "Generated .ddev/config.local.yaml"
#------------------------------------------------------------------------------
# Create Git branch
#------------------------------------------------------------------------------
log_step "Setting up Git branch"
BRANCH_NAME="worktree/\${WORKTREE_ID}"
if git show-ref --verify --quiet "refs/heads/\${BRANCH_NAME}"; then
log_info "Branch \${BRANCH_NAME} already exists, checking out"
git checkout "\${BRANCH_NAME}"
else
log_info "Creating new branch: \${BRANCH_NAME}"
git checkout -b "\${BRANCH_NAME}"
fi
log_success "Git branch ready: \${BRANCH_NAME}"
#------------------------------------------------------------------------------
# Setup files directory symlinks
#------------------------------------------------------------------------------
log_step "Setting up files directories"
for files_dir in "\${FILES_DIRS[@]}"; do
MAIN_FILES_DIR="\${ROOT_WORKTREE_PATH}/\${files_dir}"
if [[ -d "\$MAIN_FILES_DIR" ]]; then
rm -rf "\$files_dir"
mkdir -p "\$(dirname "\$files_dir")"
ln -sf "\$MAIN_FILES_DIR" "\$files_dir"
log_success "Symlinked: \${files_dir}"
else
log_warn "Main files directory not found: \${MAIN_FILES_DIR}"
mkdir -p "\$files_dir"
log_info "Created empty directory: \${files_dir}"
fi
done
#------------------------------------------------------------------------------
# Start DDEV
#------------------------------------------------------------------------------
log_step "Starting DDEV"
ddev start
log_success "DDEV started"
#------------------------------------------------------------------------------
# Install composer dependencies
#------------------------------------------------------------------------------
log_step "Checking composer dependencies"
if [[ ! -d "vendor" ]]; then
log_info "vendor/ not found - running composer install..."
COMPOSER_START=\$(date +%s)
# Capture composer output for logging
COMPOSER_OUTPUT=\$(mktemp)
if ddev composer install --no-interaction 2>&1 | tee "\$COMPOSER_OUTPUT"; then
COMPOSER_END=\$(date +%s)
COMPOSER_PACKAGES=\$(grep -c "Installing\|Updating" "\$COMPOSER_OUTPUT" 2>/dev/null || echo "0")
log_success "Composer install completed in \$((COMPOSER_END - COMPOSER_START))s (\${COMPOSER_PACKAGES} packages)"
else
COMPOSER_END=\$(date +%s)
log_error "Composer install failed after \$((COMPOSER_END - COMPOSER_START))s"
# Log last 10 lines of error output
log_error "Last output:"
tail -10 "\$COMPOSER_OUTPUT" | while read -r line; do
log_error " \$line"
done
log_warn "Site may not work correctly without vendor/"
fi
rm -f "\$COMPOSER_OUTPUT"
else
log_info "vendor/ exists - skipping composer install"
fi
#------------------------------------------------------------------------------
# Build theme assets (if needed)
#------------------------------------------------------------------------------
log_step "Checking theme build"
THEME_BUILT=false
# Method 1: ddev theme command (custom DDEV command)
if command -v ddev &>/dev/null && ddev describe 2>/dev/null | grep -q "theme"; then
log_info "Found 'ddev theme' command - running..."
THEME_START=\$(date +%s)
if ddev theme 2>&1; then
THEME_END=\$(date +%s)
log_success "Theme built with 'ddev theme' in \$((THEME_END - THEME_START))s"
THEME_BUILT=true
else
log_warn "'ddev theme' failed"
fi
fi
# Method 2: yarn/npm + gulp in theme directory
if [[ "\$THEME_BUILT" == "false" ]]; then
# Find theme directory with package.json
THEME_DIR=""
for dir in web/themes/custom/*/package.json themes/custom/*/package.json; do
if [[ -f "\$dir" ]]; then
THEME_DIR=\$(dirname "\$dir")
break
fi
done
if [[ -n "\$THEME_DIR" ]]; then
log_info "Found theme: \${THEME_DIR}"
THEME_START=\$(date +%s)
# Determine build tool (gulp vs npm scripts)
HAS_GULP=false
if [[ -f "\${THEME_DIR}/gulpfile.js" ]]; then
HAS_GULP=true
fi
# Determine package manager (yarn vs npm)
if [[ -f "\${THEME_DIR}/yarn.lock" ]]; then
PKG_MANAGER="yarn"
PKG_INSTALL="yarn install"
else
PKG_MANAGER="npm"
PKG_INSTALL="npm ci || npm install"
fi
log_info "Using \${PKG_MANAGER} + \$(if [[ "\$HAS_GULP" == "true" ]]; then echo "gulp"; else echo "npm scripts"; fi)"
if [[ "\$HAS_GULP" == "true" ]]; then
# Gulp-based build (common in Drupal themes)
if ddev exec "cd \${THEME_DIR} && \${PKG_INSTALL} && gulp compile" 2>&1; then
THEME_BUILT=true
elif ddev exec "cd \${THEME_DIR} && \${PKG_INSTALL} && gulp dist" 2>&1; then
THEME_BUILT=true
elif ddev exec "cd \${THEME_DIR} && \${PKG_INSTALL} && gulp build" 2>&1; then
THEME_BUILT=true
elif ddev exec "cd \${THEME_DIR} && \${PKG_INSTALL} && gulp" 2>&1; then
THEME_BUILT=true
fi
else
# npm scripts based build
if ddev exec "cd \${THEME_DIR} && \${PKG_INSTALL} && npm run build" 2>&1; then
THEME_BUILT=true
elif ddev exec "cd \${THEME_DIR} && \${PKG_INSTALL} && npm run production" 2>&1; then
THEME_BUILT=true
elif ddev exec "cd \${THEME_DIR} && \${PKG_INSTALL} && npm run dist" 2>&1; then
THEME_BUILT=true
fi
fi
if [[ "\$THEME_BUILT" == "true" ]]; then
THEME_END=\$(date +%s)
log_success "Theme built in \$((THEME_END - THEME_START))s"
else
log_warn "Theme build failed - CSS/JS may be missing"
fi
else
log_info "No theme with package.json found - skipping build"
fi
fi
#------------------------------------------------------------------------------
# Import databases
#------------------------------------------------------------------------------
log_step "Importing databases"
$(echo -e "$db_imports")
log_success "All databases imported"
#------------------------------------------------------------------------------
# Clear cache
#------------------------------------------------------------------------------
log_step "Clearing Drupal cache"
ddev drush cr || log_warn "Cache clear failed (may be normal for fresh installs)"
for site in "\${SITES[@]}"; do
if [[ "\$site" != "default" ]]; then
log_info "Clearing cache for site: \${site}"
ddev drush --uri="\${site}" cr 2>/dev/null || log_warn "Cache clear failed for \${site}"
fi
done
log_success "Cache cleared"
#------------------------------------------------------------------------------
# Health check & Summary
#------------------------------------------------------------------------------
log_step "Running health check"
SITE_URL="https://\${PROJECT_NAME}.ddev.site"
log_info "Site URL: \${SITE_URL}"
if curl -sI "\$SITE_URL" | head -1 | grep -q "200\|301\|302\|303"; then
log_success "Main site is responding"
else
log_warn "Main site may not be fully ready yet"
fi
LOGIN_URL=\$(ddev drush uli --uri="\$SITE_URL" 2>/dev/null || echo "")
if [[ -n "\$LOGIN_URL" ]]; then
log_success "Admin login URL: \${LOGIN_URL}"
else
log_warn "Could not generate login URL"
fi
#------------------------------------------------------------------------------
# Summary
#------------------------------------------------------------------------------
log_step "Setup complete"
log_info "Worktree ID: \${WORKTREE_ID}"
log_info "Project: \${PROJECT_NAME}"
log_info "Branch: \${BRANCH_NAME}"
log_info "Main URL: \${SITE_URL}"
# Log all site URLs
for site in "\${SITES[@]}"; do
if [[ "\$site" == "default" ]]; then
log_info "Site [default]: \${SITE_URL}"
else
prefix=\$(echo "\$site" | cut -d'.' -f1)
log_info "Site [\${site}]: https://\${prefix}.\${PROJECT_NAME}.ddev.site"
fi
done
echo ""
echo "╔════════════════════════════════════════════════════════════════════╗"
echo "║ ✅ Multisite Worktree Setup Complete! ║"
echo "╠════════════════════════════════════════════════════════════════════╣"
echo "║ Worktree ID: \${WORKTREE_ID}"
echo "║ Project: \${PROJECT_NAME}"
echo "║ Branch: \${BRANCH_NAME}"
echo "╠════════════════════════════════════════════════════════════════════╣"
echo "║ Sites:"
for site in "\${SITES[@]}"; do
if [[ "\$site" == "default" ]]; then
echo "║ - Main: \${SITE_URL}"
else
prefix=\$(echo "\$site" | cut -d'.' -f1)
echo "║ - \${site}: https://\${prefix}.\${PROJECT_NAME}.ddev.site"
fi
done
echo "╠════════════════════════════════════════════════════════════════════╣"