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

  • git
  • composer
  • jq
  • Standard Unix tools (cp, mkdir, sed, date)
  • Composer Patches Plugin: cweagans/composer-patches / vaimo/composer-patches

Supported Environments

  • Linux
  • macOS

๐Ÿš€ Installation

  1. 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
  1. 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)

Composer Patch Creator - Help Fig: help command

Composer Patch Creator - Creator Fig: patch command in action

๐Ÿ” How It Works

  1. Checks system dependencies
  2. Validates vendor package existence
  3. Stages vendor package files
  4. Prompts for file modifications
  5. Creates patch file
  6. Updates composer.json with 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.json is present

๐Ÿ“„ License

MIT License

๐Ÿ‘ฅ Contributing

Contributions welcome! Please open issues or submit pull requests.

๐Ÿ™Œ Credit

Developed with โค๏ธ by Raj KB magepsycho@gmail.com