PHP Composer Patch Creator
January 2, 2025 ยท View on GitHub
๐ Overview
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.
๐ Why Use This Script?
Traditional Patch Creation Workflow
# 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",
},
}
}
With Composer Patch Creation Utility
# 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 โจ
๐ Key Benefits
- 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
๐ฆ Prerequisites
Required Tools
gitcomposerjq- Standard Unix tools (
cp,mkdir,sed,date) - Composer Patches Plugin:
cweagans/composer-patches/vaimo/composer-patches
Supported Environments
- Linux
- macOS
๐ Installation
- 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
๐ก Usage
Basic Usage
./cpc.sh <vendor/package>
Advanced Options
# 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"
Options
-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 [ or composer patch apply if using vaimo/composer-patches ] 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.
In Action (Screenshots)
Fig: help command
Fig: patch command in action
๐ How It Works
- Checks system dependencies
- Validates vendor package existence
- Stages vendor package files
- Prompts for file modifications
- Creates patch file
- Updates
composer.jsonwith patch information
๐ Composer Configuration
Ensure your composer.json has patch plugin configuration:
{
"require": {
"cweagans/composer-patches": "^1.7"
},
"extra": {
"patches": {}
}
}
โ ๏ธ Best Practices
- Always review patches before applying
- Use descriptive patch names
- Keep patch files version-controlled
- Minimize patch scope and complexity
๐ Troubleshooting
- Ensure you're in a git repository
- Verify all dependencies are installed
- Check file permissions
- Confirm
composer.jsonis present
๐ License
MIT License
๐ฅ Contributing
Contributions welcome! Please open issues or submit pull requests.
๐ Credit
Developed with โค๏ธ by Raj KB magepsycho@gmail.com