Skip to content

Commit 884a635

Browse files
authored
Merge pull request #68 from WebDevStudios/feature/auto-cursor-generation
auto generate cursor rules from scripts
2 parents 0d00b71 + 04e7236 commit 884a635

File tree

7 files changed

+611
-4
lines changed

7 files changed

+611
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ build/
2929
#tests
3030
pa11y-ci-report/
3131
phpcs-report.txt
32+
33+
# cursor
34+
.cursor/

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
],
3939
"phpcs-fix": [
4040
"@php vendor/bin/phpcbf --report=summary,source"
41+
],
42+
"post-install-cmd": [
43+
"./scripts/update-cursorrules.sh"
4144
]
4245
},
4346
"version": "1.3.0"

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@
117117
"lint:php": "composer run-script phpcs",
118118
"lint:pkg-json": "wp-scripts lint-pkg-json",
119119
"packages-update": "npx npm-check-updates -u && npm install && npm dedupe && npm audit fix",
120-
"postinstall": "npx lefthook install && git config --local core.hooksPath .git/hooks",
120+
"postinstall": "npx lefthook install && git config --local core.hooksPath .git/hooks && ./scripts/update-cursorrules.sh",
121121
"preinstall": "npx cross-env npm_config_legacy_peer_deps=true",
122122
"reset": "rm -rf node_modules vendor build blocks package-lock.json composer.lock",
123123
"setup": "npm run reset && npm i && composer i && npm run build",
124124
"start": "rm -rf build blocks && npx cross-env NODE_ENV=development wp-scripts start",
125+
"update-cursorrules": "./scripts/update-cursorrules.sh",
125126
"version-update": "dotenv node updateVersion.js"
126127
},
127128
"lint-staged": {

scripts/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Scripts Directory
2+
3+
This directory contains utility scripts for the WDS Block Theme project.
4+
5+
## update-cursorrules.sh
6+
7+
This script automatically extracts the PHP version from `composer.json` and updates the Cursor rules files in the `.cursor/rules/` directory to ensure Cursor uses the correct PHP version for code analysis and suggestions.
8+
9+
### Features
10+
11+
- **Automatic PHP Version Detection**: Uses `jq` to parse `composer.json` and extract the PHP version
12+
- **Multiple Version Sources**: Checks multiple locations in composer.json:
13+
- `config.platform.php` (preferred)
14+
- `require-dev.php`
15+
- `require.php`
16+
- **Version String Cleaning**: Removes version constraints (>=, ^, ~, etc.) to get the base version
17+
- **Modern Cursor Rules Structure**: Creates separate `.mdc` files in `.cursor/rules/` directory:
18+
- `dependency-management.mdc` - PHP version and package management
19+
- `development-workflow.mdc` - Project structure and workflow
20+
- `javascript-standards.mdc` - Frontend development standards
21+
- `php-code-standards.mdc` - Comprehensive PHP coding standards
22+
- `theme-development.mdc` - WordPress theme development guidelines
23+
24+
### Prerequisites
25+
26+
- `jq` must be installed on your system
27+
- macOS: `brew install jq`
28+
- Ubuntu/Debian: `sudo apt-get install jq`
29+
- Windows: Download from https://stedolan.github.io/jq/download/
30+
31+
### Usage
32+
33+
#### Manual Execution
34+
```bash
35+
./scripts/update-cursorrules.sh
36+
```
37+
38+
#### Via NPM Script
39+
```bash
40+
npm run update-cursorrules
41+
```
42+
43+
#### Automatic Execution
44+
The script runs automatically after:
45+
- `npm install` (via postinstall script)
46+
- `composer install` (via post-install-cmd script)
47+
48+
### Output
49+
50+
The script creates/updates files in `.cursor/rules/` directory:
51+
52+
```
53+
.cursor/rules/
54+
├── dependency-management.mdc # PHP version and package management
55+
├── development-workflow.mdc # Project structure and workflow
56+
├── javascript-standards.mdc # Frontend development standards
57+
├── php-code-standards.mdc # Comprehensive PHP coding standards
58+
└── theme-development.mdc # WordPress theme development guidelines
59+
```
60+
61+
Each file contains specific guidelines for different aspects of development:
62+
63+
#### dependency-management.mdc
64+
- PHP version configuration (automatically updated)
65+
- Package management guidelines
66+
- Development environment setup
67+
- Build process instructions
68+
69+
#### development-workflow.mdc
70+
- Project structure overview
71+
- Code standards and quality assurance
72+
- Testing and error handling
73+
- Performance optimization
74+
75+
#### javascript-standards.mdc
76+
- Modern JavaScript (ES6+) standards
77+
- Block development guidelines
78+
- Code style and documentation
79+
- Testing and build processes
80+
81+
#### php-code-standards.mdc
82+
- WordPress PHP Coding Standards (WPCS)
83+
- Code style and naming conventions
84+
- Security and performance guidelines
85+
- Testing and error handling
86+
- Database best practices
87+
88+
#### theme-development.mdc
89+
- WordPress block theme standards
90+
- Theme structure and setup
91+
- Block patterns and templates
92+
- Internationalization and accessibility
93+
- Performance optimization
94+
95+
### Error Handling
96+
97+
The script includes comprehensive error handling:
98+
- Checks for `jq` installation
99+
- Validates `composer.json` exists
100+
- Provides helpful error messages if PHP version cannot be extracted
101+
- Uses colored output for better readability
102+
103+
### Integration
104+
105+
This script is integrated into the project's build process:
106+
107+
1. **NPM Integration**: Added to `package.json` postinstall script
108+
2. **Composer Integration**: Added to `composer.json` post-install-cmd script
109+
3. **Manual Script**: Available as `npm run update-cursorrules`
110+
111+
### Benefits
112+
113+
- **Consistent PHP Version**: Ensures Cursor always uses the correct PHP version
114+
- **Organized Rules**: Separates concerns into focused rule files
115+
- **Automatic Updates**: No manual intervention required
116+
- **Project-Specific Rules**: Tailored to WordPress block theme development
117+
- **Modern PHP Features**: Leverages PHP 8.2+ features in suggestions
118+
- **WordPress Standards**: Includes WordPress-specific coding guidelines
119+
- **Comprehensive Coverage**: Covers all aspects of theme development
120+
121+
## test-update-cursorrules.sh
122+
123+
This script is used to test the extraction and update logic for the Cursor rules automation.

scripts/test-update-cursorrules.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
3+
# Test script to verify PHP version extraction from different composer.json formats
4+
5+
set -e
6+
7+
# Colors for output
8+
GREEN='\033[0;32m'
9+
RED='\033[0;31m'
10+
NC='\033[0m' # No Color
11+
12+
print_test() {
13+
echo -e "${GREEN}[TEST]${NC} $1"
14+
}
15+
16+
print_result() {
17+
if [ $1 -eq 0 ]; then
18+
echo -e "${GREEN}[PASS]${NC} $2"
19+
else
20+
echo -e "${RED}[FAIL]${NC} $2"
21+
fi
22+
}
23+
24+
# Test 1: Platform config format (current format)
25+
print_test "Testing platform config format..."
26+
echo '{"config": {"platform": {"php": "8.2"}}}' > test-composer.json
27+
PHP_VERSION=$(jq -r '.config.platform.php // empty' test-composer.json 2>/dev/null)
28+
PHP_VERSION=$(echo "$PHP_VERSION" | sed 's/[^0-9.]//g')
29+
if [ "$PHP_VERSION" = "8.2" ]; then
30+
print_result 0 "Platform config format works"
31+
else
32+
print_result 1 "Platform config format failed: got $PHP_VERSION"
33+
fi
34+
35+
# Test 2: Require-dev format
36+
print_test "Testing require-dev format..."
37+
echo '{"require-dev": {"php": ">=8.2"}}' > test-composer.json
38+
PHP_VERSION=$(jq -r '.require-dev.php // empty' test-composer.json 2>/dev/null)
39+
PHP_VERSION=$(echo "$PHP_VERSION" | sed 's/[^0-9.]//g')
40+
if [ "$PHP_VERSION" = "8.2" ]; then
41+
print_result 0 "Require-dev format works"
42+
else
43+
print_result 1 "Require-dev format failed: got $PHP_VERSION"
44+
fi
45+
46+
# Test 3: Require format
47+
print_test "Testing require format..."
48+
echo '{"require": {"php": "^8.2.0"}}' > test-composer.json
49+
PHP_VERSION=$(jq -r '.require.php // empty' test-composer.json 2>/dev/null)
50+
PHP_VERSION=$(echo "$PHP_VERSION" | sed 's/[^0-9.]//g')
51+
if [ "$PHP_VERSION" = "8.2.0" ]; then
52+
print_result 0 "Require format works"
53+
else
54+
print_result 1 "Require format failed: got $PHP_VERSION"
55+
fi
56+
57+
# Test 4: Simple version constraint
58+
print_test "Testing simple version constraint..."
59+
echo '{"require": {"php": "~8.2.0"}}' > test-composer.json
60+
PHP_VERSION=$(jq -r '.require.php // empty' test-composer.json 2>/dev/null)
61+
PHP_VERSION=$(echo "$PHP_VERSION" | sed 's/[^0-9.]//g')
62+
if [ "$PHP_VERSION" = "8.2.0" ]; then
63+
print_result 0 "Simple version constraint works"
64+
else
65+
print_result 1 "Simple version constraint failed: got $PHP_VERSION"
66+
fi
67+
68+
# Cleanup
69+
rm -f test-composer.json
70+
71+
print_test "All tests completed!"

0 commit comments

Comments
 (0)