-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstall.php
More file actions
1828 lines (1629 loc) · 59.3 KB
/
install.php
File metadata and controls
1828 lines (1629 loc) · 59.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
<?php
/**
* Cursor Rules Installer Script.
*
* This script downloads and installs Cursor rules into your project's .cursor/rules directory.
* It provides interactive prompts to select which rule sets to install based on project type.
*
* CLI Options:
* --web-stack, -w: Install core, web, and Drupal rules
* --python, -p: Install core and Python rules
* --all, -a: Install all rule sets
* --core, -c: Install only core rules
* --custom: Enable selective installation (interactive)
* --tags: Filter rules by tag expression (e.g., "language:php category:security")
* --help, -h: Display help information
* --quiet, -q: Suppress verbose output
* --yes, -y: Automatically confirm all prompts
*/
declare(strict_types=1);
// Define constants.
define('CURSOR_RULES_VERSION', '1.1.0');
define('CURSOR_RULES_DIR', '.cursor/rules');
define('CURSOR_DIR', '.cursor');
define('CURSOR_COMMANDS_DIR', '.cursor/commands');
const COLORS = [
'red' => "\033[0;31m",
'green' => "\033[0;32m",
'yellow' => "\033[1;33m",
'blue' => "\033[0;34m",
'magenta' => "\033[0;35m",
'cyan' => "\033[0;36m",
'white' => "\033[1;37m",
'reset' => "\033[0m",
];
// Define tag presets for common use cases
const TAG_PRESETS = [
'web' => 'language:javascript OR language:html OR language:css OR language:php',
'frontend' => 'language:javascript OR language:html OR language:css',
'drupal' => 'framework:drupal',
'react' => 'framework:react',
'vue' => 'framework:vue',
'python' => 'language:python',
'security' => 'category:security',
'owasp' => 'standard:owasp-top10',
'a11y' => 'category:accessibility',
// New language-specific security presets
'php-security' => 'language:php category:security',
'js-security' => 'language:javascript category:security',
'python-security' => 'language:python category:security',
'drupal-security' => 'framework:drupal category:security',
'php-owasp' => 'language:php standard:owasp-top10',
'js-owasp' => 'language:javascript standard:owasp-top10',
'python-owasp' => 'language:python standard:owasp-top10',
'drupal-owasp' => 'framework:drupal standard:owasp-top10',
];
// Command files bundled with the installer.
const COMMAND_FILES = [
'behat-drevops.md',
'drupal-lint.md',
'gh-issue-create.md',
'gh-issue-resolve.md',
'npm-audit-fix.md',
'pr-assess.md',
'pr-draft.md',
'pr-resolve.md',
'security-scan.md',
'security-verify.md',
'session-summary.md',
'speckit.analyze.md',
'speckit.checklist.md',
'speckit.clarify.md',
'speckit.constitution.md',
'speckit.implement.md',
'speckit.plan.md',
'speckit.specify.md',
'speckit.tasks.md',
];
// Main function to install cursor rules.
function install_cursor_rules(array $options = []): bool {
// Default options.
$default_options = [
'debug' => false,
'copy_only' => false,
'destination' => CURSOR_RULES_DIR,
'web_stack' => false,
'python' => false,
'javascript' => false,
'tags' => false,
'tag-preset' => false,
'ignore-files' => 'yes',
'all' => false,
'core' => false,
'yes' => false,
'help' => false,
'commands' => 'auto',
];
// Merge options.
$options = array_merge($default_options, $options);
// Determine if STDIN is available for interactive prompts.
$stdin_available = false;
if (function_exists('stream_isatty') && defined('STDIN')) {
$stdin_available = @stream_isatty(STDIN);
}
$scripted_input = getenv('CURSOR_INSTALLER_INPUT');
if (!$stdin_available && $scripted_input !== false && $scripted_input !== '') {
$stdin_available = true;
}
// Show help if requested.
if ($options['help']) {
show_help();
return true;
}
// Check for conflicting options.
$option_count = 0;
if ($options['web_stack']) $option_count++;
if ($options['python']) $option_count++;
if ($options['javascript']) $option_count++;
if ($options['all']) $option_count++;
if ($options['core']) $option_count++;
if ($options['tags']) $option_count++;
if ($options['tag-preset']) $option_count++;
if ($option_count > 1) {
echo "Error: Conflicting options. Please choose only one installation type.\n";
echo "Run with --help for usage information.\n";
return false;
}
// Debug mode.
if ($options['debug']) {
echo "Debug mode enabled\n";
echo "Options: " . print_r($options, true) . "\n";
}
// Create destination directory if it doesn't exist.
if (!is_dir($options['destination'])) {
if (!mkdir($options['destination'], 0755, true)) {
echo "Error: Failed to create directory: {$options['destination']}\n";
return false;
}
if ($options['debug']) {
echo "Created directory: {$options['destination']}\n";
}
}
// Create .cursor directory if it doesn't exist.
$cursor_dir = dirname($options['destination']);
if ($cursor_dir !== '.' && !is_dir($cursor_dir)) {
if (!mkdir($cursor_dir, 0755, true)) {
echo "Error: Failed to create .cursor directory.\n";
return false;
}
}
// Define available rules.
$core_rules = [
'cursor-rules.mdc',
'git-commit-standards.mdc',
'github-actions-standards.mdc',
'improve-cursorrules-efficiency.mdc',
'pull-request-changelist-instructions.mdc',
'readme-maintenance-standards.mdc',
'testing-guidelines.mdc',
];
$web_stack_rules = [
'accessibility-standards.mdc',
'api-standards.mdc',
'build-optimization.mdc',
'code-generation-standards.mdc',
'debugging-standards.mdc',
'docker-compose-standards.mdc',
'drupal-authentication-failures.mdc',
'drupal-broken-access-control.mdc',
'drupal-cryptographic-failures.mdc',
'drupal-database-standards.mdc',
'drupal-file-permissions.mdc',
'drupal-injection.mdc',
'drupal-insecure-design.mdc',
'drupal-integrity-failures.mdc',
'drupal-logging-failures.mdc',
'drupal-security-misconfiguration.mdc',
'drupal-ssrf.mdc',
'drupal-vulnerable-components.mdc',
'generic_bash_style.mdc',
'javascript-performance.mdc',
'javascript-standards.mdc',
'lagoon-docker-compose-standards.mdc',
'lagoon-yml-standards.mdc',
'multi-agent-coordination.mdc',
'node-dependencies.mdc',
'php-drupal-best-practices.mdc',
'php-drupal-development-standards.mdc',
'project-definition-template.mdc',
'react-patterns.mdc',
'security-practices.mdc',
'secret-detection.mdc',
'tailwind-standards.mdc',
'tests-documentation-maintenance.mdc',
'third-party-integration.mdc',
'vortex-cicd-standards.mdc',
'vortex-scaffold-standards.mdc',
'vue-best-practices.mdc',
'behat-steps.mdc',
'behat-ai-guide.mdc',
];
$python_rules = [
'python-broken-access-control.mdc',
'python-cryptographic-failures.mdc',
'python-injection.mdc',
'python-insecure-design.mdc',
'python-security-misconfiguration.mdc',
'python-vulnerable-outdated-components.mdc',
'python-authentication-failures.mdc',
'python-integrity-failures.mdc',
'python-logging-monitoring-failures.mdc',
'python-ssrf.mdc',
'security-practices.mdc',
];
$javascript_rules = [
'javascript-broken-access-control.mdc',
'javascript-cryptographic-failures.mdc',
'javascript-injection.mdc',
'javascript-insecure-design.mdc',
'javascript-security-misconfiguration.mdc',
'javascript-vulnerable-outdated-components.mdc',
'javascript-identification-authentication-failures.mdc',
'javascript-software-data-integrity-failures.mdc',
'javascript-security-logging-monitoring-failures.mdc',
'javascript-server-side-request-forgery.mdc',
];
// Determine which rules to install.
$rules_to_install = [];
// Handle tag-based filtering
if ($options['tags'] || $options['tag-preset']) {
$tag_expression = $options['tags'] ?: TAG_PRESETS[$options['tag-preset']] ?? '';
if (empty($tag_expression)) {
echo "Error: Invalid tag preset '{$options['tag-preset']}'\n";
echo "Available presets: " . implode(', ', array_keys(TAG_PRESETS)) . "\n";
return false;
}
echo "Installing rules matching tag expression: $tag_expression\n";
// When using tags, we need to check all available rules
$rules_to_install = array_merge($core_rules, $web_stack_rules, $python_rules, $javascript_rules);
if ($options['debug']) {
echo "Will filter " . count($rules_to_install) . " rules based on tags\n";
}
} else {
// Interactive mode if no specific option is selected and not in auto-yes mode and STDIN is available
if ($option_count === 0 && !$options['yes'] && $stdin_available) {
echo "Welcome to Cursor Rules Installer v" . CURSOR_RULES_VERSION . "\n\n";
echo "Please select which rules to install:\n";
echo "1) Core rules only\n";
echo "2) Web stack rules (PHP, Drupal, etc.)\n";
echo "3) Python rules\n";
echo "4) JavaScript security rules (OWASP Top 10)\n";
echo "5) All rules\n";
echo "6) Tag-based installation (advanced)\n";
echo "7) Install .cursorignore files\n";
echo "8) Exit\n";
$valid_choice = false;
while (!$valid_choice) {
echo "\nEnter your choice (1-8): ";
$choice = read_stdin_line();
switch ($choice) {
case '1':
$rules_to_install = $core_rules;
$valid_choice = true;
echo "Installing core rules...\n";
break;
case '2':
$rules_to_install = array_merge($core_rules, $web_stack_rules);
$valid_choice = true;
echo "Installing web stack rules...\n";
if ($options['debug']) {
echo "Selected " . count($rules_to_install) . " rules to install (" . count($core_rules) . " core + " . count($web_stack_rules) . " web stack)\n";
}
break;
case '3':
$rules_to_install = array_merge($core_rules, $python_rules);
$valid_choice = true;
echo "Installing Python rules...\n";
if ($options['debug']) {
echo "Selected " . count($rules_to_install) . " rules to install (" . count($core_rules) . " core + " . count($python_rules) . " python)\n";
}
break;
case '4':
$rules_to_install = array_merge($core_rules, $javascript_rules);
$valid_choice = true;
echo "Installing JavaScript security rules...\n";
if ($options['debug']) {
echo "Selected " . count($rules_to_install) . " rules to install (" . count($core_rules) . " core + " . count($javascript_rules) . " JavaScript)\n";
}
break;
case '5':
$rules_to_install = array_merge($core_rules, $web_stack_rules, $python_rules, $javascript_rules);
$valid_choice = true;
echo "Installing all rules...\n";
if ($options['debug']) {
echo "Selected " . count($rules_to_install) . " rules to install (" . count($core_rules) . " core + " . count($web_stack_rules) . " web stack + " . count($python_rules) . " python + " . count($javascript_rules) . " JavaScript)\n";
}
break;
case '6':
// Tag-based installation
echo "Available tag presets:\n";
foreach (TAG_PRESETS as $preset => $expression) {
echo " - $preset: $expression\n";
}
echo "\nEnter tag preset name or custom tag expression: ";
$tag_input = read_stdin_line();
if (array_key_exists($tag_input, TAG_PRESETS)) {
$tag_expression = TAG_PRESETS[$tag_input];
} else {
$tag_expression = $tag_input;
}
echo "Installing rules matching: $tag_expression\n";
$rules_to_install = array_merge($core_rules, $web_stack_rules, $python_rules, $javascript_rules);
$options['tags'] = $tag_expression;
$valid_choice = true;
break;
case '7':
// Install only .cursorignore files
$rules_to_install = [];
$options['ignore-files'] = true;
$valid_choice = true;
echo "Installing .cursorignore files...\n";
break;
case '8':
echo "Installation cancelled.\n";
return true;
default:
echo "Invalid choice. Please enter a number between 1 and 8.\n";
}
}
} else if ($option_count === 0 && !$stdin_available) {
// If STDIN is not available (e.g., when piped through curl), default to core rules
echo "⚠️ Interactive mode not available when using curl piping (STDIN is already in use).\n";
echo "Defaulting to core rules installation.\n\n";
echo "For interactive installation with prompts, use the two-step process instead:\n";
echo "1. curl -s https://raw.githubusercontent.com/ivangrynenko/cursor-rules/main/install.php -o install.php\n";
echo "2. php install.php\n\n";
echo "For specific options without interactive mode, use:\n";
echo "curl -s https://raw.githubusercontent.com/ivangrynenko/cursor-rules/main/install.php | php -- --help\n\n";
$rules_to_install = $core_rules;
} else if ($options['all']) {
$rules_to_install = array_merge($core_rules, $web_stack_rules, $python_rules, $javascript_rules);
} elseif ($options['web_stack']) {
$rules_to_install = array_merge($core_rules, $web_stack_rules, $javascript_rules);
} elseif ($options['python']) {
$rules_to_install = array_merge($core_rules, $python_rules);
} elseif ($options['javascript']) {
$rules_to_install = array_merge($core_rules, $javascript_rules);
} elseif ($options['core']) {
$rules_to_install = $core_rules;
} else {
// Default to core rules if no option specified and in auto-yes mode.
$rules_to_install = $core_rules;
}
}
// Determine command installation targets.
$commands_option = strtolower((string)($options['commands'] ?? 'auto'));
$command_targets = [];
switch ($commands_option) {
case 'skip':
case 'none':
case 'no':
$command_targets = [];
$commands_option = 'skip';
break;
case 'home':
$command_targets = ['home'];
break;
case 'project':
$command_targets = ['project'];
break;
case 'both':
case 'all':
$command_targets = ['home', 'project'];
$commands_option = 'both';
break;
default:
// Interactive prompt when auto and we can ask the user.
if ($options['yes'] || !$stdin_available) {
$command_targets = ['project'];
$commands_option = 'project';
} else {
echo "\nCursor slash commands provide ready-to-use prompts for the Cursor agent.\n";
echo "Install slash commands as part of this setup? (Y/n): ";
$response = strtolower(read_stdin_line());
if ($response === 'n' || $response === 'no') {
$command_targets = [];
$commands_option = 'skip';
} else {
echo "\nWhere should the commands be installed?\n";
echo " 1) User home directory (~/.cursor/commands)\n";
echo " 2) Project directory (" . CURSOR_COMMANDS_DIR . ")\n";
echo " 3) Both locations\n";
echo "Enter choice (1-3) [default: 2]: ";
$location_choice = read_stdin_line();
if ($location_choice === '') {
$location_choice = '2';
}
switch ($location_choice) {
case '1':
$command_targets = ['home'];
$commands_option = 'home';
break;
case '3':
$command_targets = ['home', 'project'];
$commands_option = 'both';
break;
case '2':
default:
$command_targets = ['project'];
$commands_option = 'project';
break;
}
}
}
}
$command_targets = array_values(array_unique($command_targets));
$should_install_commands = count($command_targets) > 0;
// Persist the normalized command option for later use.
$options['commands'] = $commands_option;
$commands_temp_dir = null;
$command_install_summary = [];
// Define possible source directories.
$possible_source_dirs = [
__DIR__ . '/.cursor/rules',
dirname(__FILE__) . '/.cursor/rules',
realpath(__DIR__ . '/../.cursor/rules'),
getcwd() . '/.cursor/rules',
];
// Filter out false values (realpath returns false if path doesn't exist)
$possible_source_dirs = array_filter($possible_source_dirs, function($dir) {
return $dir !== false;
});
// Check if we're in the cloned repo root
if (file_exists(__DIR__ . '/.cursor/rules')) {
// We're likely in the root of the cloned repository
$possible_source_dirs[] = __DIR__ . '/.cursor/rules';
}
// Validate source directory has the rules we need
function is_valid_source_dir($dir, $rule_files) {
if (!is_dir($dir)) {
return false;
}
// Check if at least half of the expected rule files exist
$found_files = 0;
$min_files = max(1, intval(count($rule_files) * 0.5));
if (isset($options['debug']) && $options['debug']) {
echo "Checking if directory is valid source: $dir\n";
echo "Looking for at least $min_files of " . count($rule_files) . " rule files\n";
}
foreach ($rule_files as $file) {
if (file_exists($dir . '/' . $file)) {
$found_files++;
if (isset($options['debug']) && $options['debug']) {
echo " Found rule file: $file\n";
}
if ($found_files >= $min_files) {
return true;
}
}
}
if (isset($options['debug']) && $options['debug']) {
echo " Found only $found_files files, need at least $min_files\n";
}
return false;
}
// Add debug output for rules to install
if ($options['debug']) {
echo "\nRules to install (" . count($rules_to_install) . " total):\n";
foreach ($rules_to_install as $index => $rule) {
echo ($index + 1) . ". $rule\n";
}
echo "\n";
}
// Remove duplicates from rules to install
$rules_to_install = array_unique($rules_to_install);
// If copy_only option is set, skip the source directory check.
if ($options['copy_only']) {
echo "Copy-only mode enabled. Skipping source directory check.\n";
return true;
}
// Try to download rules from GitHub if no local source is found
$github_source = 'https://raw.githubusercontent.com/ivangrynenko/cursor-rules/main/.cursor/rules/';
$temp_dir = sys_get_temp_dir() . '/cursor-rules-' . uniqid();
// Find a valid source directory
$source_dir = null;
foreach ($possible_source_dirs as $dir) {
if (is_valid_source_dir($dir, $rules_to_install)) {
$source_dir = $dir;
if ($options['debug']) {
echo "Found valid source directory: $dir\n";
}
break;
}
}
// If no local source found, try to download from GitHub
if ($source_dir === null) {
if ($options['debug']) {
echo "No local source found, attempting to download from GitHub...\n";
}
if (!mkdir($temp_dir, 0755, true)) {
echo "Error: Failed to create temporary directory.\n";
return false;
}
$download_success = true;
// Download all rules that need to be installed
if ($options['debug']) {
echo "Downloading " . count($rules_to_install) . " rules from GitHub...\n";
}
foreach ($rules_to_install as $rule_file) {
$url = $github_source . $rule_file;
$content = @file_get_contents($url);
if ($content === false) {
if ($options['debug']) {
echo "Failed to download: $rule_file\n";
}
$download_success = false;
continue;
}
file_put_contents($temp_dir . '/' . $rule_file, $content);
if ($options['debug']) {
echo "Downloaded: $rule_file\n";
}
}
// Verify we have at least the core rules
if (is_valid_source_dir($temp_dir, $rules_to_install)) {
$source_dir = $temp_dir;
if ($options['debug']) {
echo "Successfully downloaded rules from GitHub to: $temp_dir\n";
}
} else {
// Clean up temp directory
@rmdir($temp_dir);
echo "Error: Could not download rules from GitHub. Please check your internet connection or try again later.\n";
echo "Alternatively, you can manually download the rules from https://github.com/ivangrynenko/cursor-rules\n";
return false;
}
}
// Final check to ensure we have a valid source directory
if ($source_dir === null) {
echo "Error: Could not find source directory containing rule files.\n";
if ($options['debug']) {
echo "Tried the following directories:\n";
foreach ($possible_source_dirs as $dir) {
echo " - $dir\n";
}
}
return false;
}
$destination_dir = $options['destination'];
// Ensure destination directory is not the same as source directory
if (realpath($source_dir) === realpath($destination_dir)) {
if ($options['debug']) {
echo "Source and destination directories are the same, downloading from GitHub instead...\n";
}
// Create a temporary directory for downloading rules
$temp_dir = sys_get_temp_dir() . '/cursor-rules-' . uniqid();
if (!mkdir($temp_dir, 0755, true)) {
echo "Error: Failed to create temporary directory.\n";
return false;
}
$download_success = true;
$github_source = 'https://raw.githubusercontent.com/ivangrynenko/cursor-rules/main/.cursor/rules/';
// Download all rules that need to be installed
if ($options['debug']) {
echo "Downloading " . count($rules_to_install) . " rules from GitHub...\n";
}
foreach ($rules_to_install as $rule_file) {
$url = $github_source . $rule_file;
$content = @file_get_contents($url);
if ($content === false) {
if ($options['debug']) {
echo "Failed to download: $rule_file\n";
}
// Check if the file exists locally in the destination directory
if (file_exists($destination_dir . '/' . $rule_file)) {
if ($options['debug']) {
echo "File exists locally, will use local copy: $rule_file\n";
}
// Copy the local file to the temp directory
copy($destination_dir . '/' . $rule_file, $temp_dir . '/' . $rule_file);
}
continue;
}
file_put_contents($temp_dir . '/' . $rule_file, $content);
if ($options['debug']) {
echo "Downloaded: $rule_file\n";
}
}
// Verify we have at least the core rules
if (is_valid_source_dir($temp_dir, $rules_to_install)) {
$source_dir = $temp_dir;
if ($options['debug']) {
echo "Successfully downloaded rules from GitHub to: $temp_dir\n";
}
} else {
// Clean up temp directory
@rmdir($temp_dir);
echo "Error: Could not download rules from GitHub. Please check your internet connection or try again later.\n";
echo "Alternatively, you can manually download the rules from https://github.com/ivangrynenko/cursor-rules\n";
return false;
}
}
$copied_count = 0;
$failed_count = 0;
$filtered_count = 0;
if ($options['debug']) {
echo "Source directory: $source_dir\n";
echo "Destination directory: $destination_dir\n";
echo "Rules to install: " . count($rules_to_install) . "\n";
}
foreach ($rules_to_install as $rule_file) {
$source_file = $source_dir . '/' . $rule_file;
$dest_file = $destination_dir . '/' . $rule_file;
// Skip this rule if tag filtering is enabled and the rule doesn't match
if (($options['tags'] || $options['tag-preset']) && !rule_matches_tag_filter($source_file, $options)) {
if ($options['debug']) {
echo "Skipping due to tag filter: $rule_file\n";
}
$filtered_count++;
continue;
}
if (file_exists($source_file)) {
if (copy($source_file, $dest_file)) {
$copied_count++;
if ($options['debug']) {
echo "Copied: $rule_file\n";
}
} else {
$failed_count++;
echo "Failed to copy: $rule_file\n";
}
} else {
if ($options['debug']) {
echo "Source file not found: $source_file\n";
}
}
}
if ($options['debug']) {
echo "Copied $copied_count files, failed to copy $failed_count files.\n";
}
// Show summary of tag filtering if enabled
if (($options['tags'] || $options['tag-preset']) && $filtered_count > 0) {
echo "Filtered out $filtered_count rules based on tag criteria.\n";
}
// Inform the user if we're updating existing rules
if (isset($temp_dir) && strpos($source_dir, $temp_dir) === 0 && is_dir($destination_dir)) {
echo "Updated existing Cursor Rules with the latest version.\n";
}
// Clean up temporary directory if it was created
if (isset($temp_dir) && strpos($source_dir, $temp_dir) === 0) {
if ($options['debug']) {
echo "Cleaning up temporary directory: $temp_dir\n";
}
// Remove all files in the temp directory
$files = glob($temp_dir . '/*');
foreach ($files as $file) {
@unlink($file);
}
// Remove the directory
@rmdir($temp_dir);
}
// Install slash commands if requested.
$commands_source_dir = null;
if ($should_install_commands) {
if ($options['debug']) {
echo "Preparing to install slash commands...\n";
}
$commands_source_candidates = [];
if (!empty($source_dir)) {
$commands_source_candidates[] = dirname($source_dir) . '/commands';
}
$env_commands_source = getenv('CURSOR_COMMAND_SOURCE') ?: getenv('CURSOR_COMMANDS_SOURCE');
if (!empty($env_commands_source)) {
$commands_source_candidates[] = rtrim($env_commands_source, DIRECTORY_SEPARATOR);
}
$commands_source_candidates[] = __DIR__ . '/.cursor/commands';
$commands_source_candidates[] = realpath(__DIR__ . '/../.cursor/commands');
$commands_source_candidates[] = getcwd() . '/.cursor/commands';
$commands_source_candidates = array_values(array_filter(array_unique($commands_source_candidates)));
foreach ($commands_source_candidates as $candidate) {
if ($candidate && is_valid_commands_source_dir($candidate)) {
$commands_source_dir = $candidate;
if ($options['debug']) {
echo "Using commands source: $candidate\n";
}
break;
}
}
if ($commands_source_dir === null) {
$commands_temp_dir = sys_get_temp_dir() . '/cursor-commands-' . uniqid();
if (!mkdir($commands_temp_dir, 0755, true)) {
echo "Warning: Failed to create temporary directory for commands. Skipping command installation.\n";
$command_install_summary[] = [
'target' => 'all',
'status' => 'failed',
'details' => 'temporary directory creation failed',
];
} else {
$commands_github_source = 'https://raw.githubusercontent.com/ivangrynenko/cursor-rules/main/.cursor/commands/';
$downloaded_commands = 0;
foreach (COMMAND_FILES as $command_file) {
$url = $commands_github_source . $command_file;
$content = @file_get_contents($url);
if ($content === false) {
if ($options['debug']) {
echo "Failed to download command file: $command_file\n";
}
continue;
}
$dest_path = $commands_temp_dir . '/' . $command_file;
$dest_dir = dirname($dest_path);
if (!is_dir($dest_dir) && !mkdir($dest_dir, 0755, true)) {
if ($options['debug']) {
echo "Failed to create directory for $dest_path\n";
}
continue;
}
if (file_put_contents($dest_path, $content) !== false) {
$downloaded_commands++;
}
}
if ($downloaded_commands > 0) {
$commands_source_dir = $commands_temp_dir;
if ($options['debug']) {
echo "Downloaded $downloaded_commands command file(s) to $commands_temp_dir\n";
}
} else {
echo "Warning: Could not download command files from GitHub. Skipping command installation.\n";
$command_install_summary[] = [
'target' => 'all',
'status' => 'failed',
'details' => 'download failed',
];
}
}
}
if ($commands_source_dir !== null) {
foreach ($command_targets as $target) {
if ($target === 'home') {
$home_dir = get_user_home_directory();
if ($home_dir === null) {
echo "Warning: Could not determine the user home directory. Skipping home command installation.\n";
$command_install_summary[] = [
'target' => 'home',
'status' => 'skipped',
'details' => 'home directory unavailable',
];
continue;
}
$destination_commands_dir = rtrim($home_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.cursor/commands';
} else {
$destination_commands_dir = dirname($options['destination']) . '/commands';
}
$source_real = realpath($commands_source_dir);
$dest_real = realpath($destination_commands_dir);
if ($dest_real !== false && $source_real !== false && $dest_real === $source_real) {
if ($options['debug']) {
echo "Commands already present at {$destination_commands_dir}; skipping copy.\n";
}
$command_install_summary[] = [
'target' => $target,
'status' => 'existing',
'details' => 'source and destination are identical',
];
continue;
}
if (!is_dir($destination_commands_dir) && !mkdir($destination_commands_dir, 0755, true)) {
echo "Warning: Failed to create command directory at {$destination_commands_dir}.\n";
$command_install_summary[] = [
'target' => $target,
'status' => 'failed',
'details' => 'destination directory creation failed',
];
continue;
}
$copied_commands = copy_directory_recursive($commands_source_dir, $destination_commands_dir);
if ($copied_commands >= 0) {
if ($options['debug']) {
echo "Installed {$copied_commands} command file(s) to {$destination_commands_dir}\n";
}
$command_install_summary[] = [
'target' => $target,
'status' => 'installed',
'details' => $destination_commands_dir,
'count' => $copied_commands,
];
} else {
echo "Warning: Failed to copy command files to {$destination_commands_dir}.\n";
$command_install_summary[] = [
'target' => $target,
'status' => 'failed',
'details' => 'copy operation failed',
];
}
}
}
} else {
if ($options['debug']) {
echo "Skipping slash command installation per user selection.\n";
}
$command_install_summary[] = [
'target' => 'all',
'status' => 'skipped',
'details' => 'user opted out',
];
}
$command_summary_lines = summarise_command_installation($command_install_summary);
// Handle .cursorignore files installation
$ignore_files_option = $options['ignore-files'];
$should_install_ignore_files = false;
if ($ignore_files_option === 'yes' || $ignore_files_option === 'y') {
$should_install_ignore_files = true;
} else if ($ignore_files_option === 'ask' || $ignore_files_option === 'a') {
if (function_exists('stream_isatty') && stream_isatty(STDIN)) {
echo "\nWould you like to install recommended .cursorignore files? (Y/n): ";
$response = strtolower(read_stdin_line());
$should_install_ignore_files = ($response === '' || $response === 'y' || $response === 'yes');
} else {
// Default to yes if we can't ask interactively
$should_install_ignore_files = true;
}
}
if ($should_install_ignore_files) {
$ignore_files_dir = dirname(__FILE__) . '/.cursor/ignore-files';
// Try GitHub if local files don't exist
if (!is_dir($ignore_files_dir)) {
$ignore_files_dir = $source_dir . '/../ignore-files';
}
if (is_dir($ignore_files_dir)) {
$ignore_files = ['.cursorignore', '.cursorindexingignore'];
$copied_ignore_count = 0;
foreach ($ignore_files as $ignore_file) {
$source_ignore = $ignore_files_dir . '/' . $ignore_file;
$dest_ignore = dirname($options['destination']) . '/../' . $ignore_file;
if (file_exists($source_ignore)) {
if (file_exists($dest_ignore)) {
if ($options['debug']) {
echo "$ignore_file already exists, skipping...\n";
}
} else {
if (copy($source_ignore, $dest_ignore)) {
$copied_ignore_count++;
if ($options['debug']) {
echo "Copied: $ignore_file\n";
}
}
}
}
}
if ($copied_ignore_count > 0) {
echo "Installed $copied_ignore_count ignore file(s) to help improve Cursor AI performance.\n";
}
} else {
// Try to download from GitHub
$github_ignore_base = 'https://raw.githubusercontent.com/ivangrynenko/cursor-rules/main/';
$ignore_files = ['.cursorignore', '.cursorindexingignore'];
$downloaded_ignore_count = 0;
foreach ($ignore_files as $ignore_file) {
$dest_ignore = dirname($options['destination']) . '/../' . $ignore_file;
if (file_exists($dest_ignore)) {
if ($options['debug']) {
echo "$ignore_file already exists, skipping...\n";
}
continue;
}
$url = $github_ignore_base . $ignore_file;
$content = @file_get_contents($url);
if ($content !== false) {
if (file_put_contents($dest_ignore, $content)) {
$downloaded_ignore_count++;
if ($options['debug']) {
echo "Downloaded and installed: $ignore_file\n";
}
}
}
}
if ($downloaded_ignore_count > 0) {
echo "Downloaded and installed $downloaded_ignore_count ignore file(s) to help improve Cursor AI performance.\n";
}
}
}
// Optionally generate a project-local AGENTS.md summarising installed rules.
// Write if absent; overwrite only when --yes was passed.
try {
$project_root = getcwd();
$agents_md_path = $project_root . '/AGENTS.md';
$should_write_agents = !file_exists($agents_md_path) || !empty($options['yes']);
if ($should_write_agents) {
// Prepare bundle definitions for grouping in AGENTS.md
$bundle_map = [
'Core' => $core_rules,
'Web Stack' => $web_stack_rules,