Skip to content

Commit

Permalink
Merge pull request #9 from szeidler/feature/8-install-after-adding-patch
Browse files Browse the repository at this point in the history
Run `composer install` after adding a patch
  • Loading branch information
szeidler authored Nov 21, 2019
2 parents f2207c4 + b5b027e commit 2f923df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ Example:
composer patch-add drupal/core "SA-CORE-2018-002" "https://cgit.drupalcode.org/drupal/rawdiff/?h=8.5.x&id=5ac8738fa69df34a0635f0907d661b509ff9a28f"
```

The patch add command accepts the following options.

* `--no-update` Use this option to prevent composer to update the package and apply the patch. The patch will only end
up in your `composer.json`, not `composer.lock` file.

* `--no-dev` Run the dependency update with the --no-dev option.

You can omit arguments for an interactive mode.

### Patch Remove
Expand Down
26 changes: 26 additions & 0 deletions src/Composer/PatchAddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Composer\Installer;

class PatchAddCommand extends PatchBaseCommand {

Expand All @@ -32,6 +34,8 @@ protected function configure() {
new InputArgument('package', InputArgument::REQUIRED),
new InputArgument('description', InputArgument::REQUIRED),
new InputArgument('url', InputArgument::REQUIRED),
new InputOption('no-update', null, InputOption::VALUE_NONE, 'Do not run an update: as side effect patch will not be applied.'),
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'),
]);

parent::configure();
Expand Down Expand Up @@ -59,6 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$package = $input->getArgument('package');
$description = $input->getArgument('description');
$url = $input->getArgument('url');
$updateDevMode = !$input->getOption('no-dev');

// The patch needs to be an existing local path or a valid URL.
if (!file_exists($url) && filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
Expand Down Expand Up @@ -103,6 +108,27 @@ protected function execute(InputInterface $input, OutputInterface $output) {
file_put_contents($config->getPatchesFile(), $manipulator->getContents());

$output->writeln('The patch was successfully added.');

if (!$input->getOption('no-update')) {
// Trigger install command after adding a patch.
$install = Installer::create($this->getIO(), $this->getComposer());

// We run an update, because the patch will otherwise not end up in the
// composer.lock. Beware: This could update the package unwanted.
$install->setUpdate(TRUE)
// Forward the option
->setVerbose($input->getOption('verbose'))
// Only update the current package
->setUpdateWhitelist([$package])
// Don't update the dependencies of the patched package.
->setWhitelistTransitiveDependencies(FALSE)
->setWhitelistAllDependencies(FALSE)
// Patches are always considered to be applied in "dev mode".
// This is also required to prevent composer from removing all installed
// dev dependencies.
->setDevMode($updateDevMode)
->run();
}
}

/**
Expand Down

0 comments on commit 2f923df

Please sign in to comment.