A robust and lightning-fast Bash utility script designed to simplify and accelerate the process of creating and managing vendor package patches for Composer-based PHP projects (such as Magento, Laravel, Symfony, etc.).
This is likely the quickest and most efficient way to generate Composer-compatible patches for vendor packages, saving developers significant time and effort.
# Manually stage specific files
git add -f ./vendor/{vendor}/{package}/file1.php ./vendor/{vendor}/{package}/file2.php ...
# Perform required changes on files
# ... (manual editing)
# Create patch manually
git diff ./vendor/{vendor}/{package}/file1.php ./vendor/{vendor}/{package}/file2.php ... > patches/{patch-name}.patch
# Cleanup steps
git restore ./vendor/{vendor}/{package}/file1.php ./vendor/{vendor}/{package}/file2.php ...
git reset HEAD ./vendor/{vendor}/{package}/file1.php ./vendor/{vendor}/{package}/file2.php ...
# OR If you are using diff command
# cp ./vendor/{vendor}/{package}/file.php ./vendor/{vendor}/{package}/file.php.old
# {perform required changes on file.php}
# diff -u ./vendor/{vendor}/{package}/file.php.old ./vendor/{vendor}/{package}/file.php > patches/{patch-name}.patch
# rm ./vendor/{vendor}/{package}/file.php
# mv ./vendor/{vendor}/{package}/file.php.old ./vendor/{vendor}/{package}/file.php
# Manually update composer.json
"extra": {
"patches": {
"{vendor}/{package}": {
"{patch-message}": "patches/{patch-name}.patch",
},
}
}
# Single command to start patch creation
cpc {vendor}/{package} -n {patch-name}.patch -m {patch-message}
# Perform required changes in vendor repo files
# Press 'y' when done
# Automatic patch generation and composer.json update β¨
- Simplified Workflow: Reduce multiple manual steps to a single command
- Automatic File Management:
- Automatically stages files
- Generates patch
- Restores original files
- Cleans up git staging
- Composer.json Integration:
- Automatically updates patch configuration
- Creates backup before modification
- Interactive Process:
- Guides you through patch creation
- Provides clear prompts and feedback
- Error Handling:
- Checks dependencies
- Validates input
- Provides detailed error messages
git
composer
jq
- Standard Unix tools (
cp
,mkdir
,sed
,date
) - Composer Patches Plugin:
cweagans/composer-patches
- Linux
- macOS
- Clone the script to your project:
curl -0 https://raw.githubusercontent.com/MagePsycho/composer-patch-creator/main/src/composer-patch-creator.sh -o cpc.sh
chmod +x cpc.sh
To make it system-wide command
sudo mv cpc.sh /usr/local/bin/cpc
- Ensure all dependencies are installed
./cpc.sh <vendor/package>
# Custom patch name
./cpc.sh magento/module-url-rewrite -n TICKET-custom-patch.patch
# Patch with description
./cpc.sh magento/module-url-rewrite -m "Fixed critical URL rewrite bug"
# Full example
./cpc.sh magento/module-url-rewrite -n TICKET-123.patch -m "Resolved routing issue"
-h, --help
: Show help message-n, --name
: Specify custom patch filename-m, --message
: Add patch description
Once the script execution is complete, run the composer install
command to apply the patches.
For more details, refer to the Composer Configuration
section.
Caution
Only edit the files after you run the command.
Changes made prior to running the command won't be detected.
- Checks system dependencies
- Validates vendor package existence
- Stages vendor package files
- Prompts for file modifications
- Creates patch file
- Updates
composer.json
with patch information
Ensure your composer.json
has patch plugin configuration:
{
"require": {
"cweagans/composer-patches": "^1.7"
},
"extra": {
"patches": {}
}
}
- Always review patches before applying
- Use descriptive patch names
- Keep patch files version-controlled
- Minimize patch scope and complexity
- Ensure you're in a git repository
- Verify all dependencies are installed
- Check file permissions
- Confirm
composer.json
is present
MIT License
Contributions welcome! Please open issues or submit pull requests.
Developed with β€οΈ by Raj KB magepsycho@gmail.com