From 97ef0e535d49b15868a9bf440fc966427374ea4c Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 11:32:18 -0700 Subject: [PATCH 01/13] Require composer-configurable-plugin --- composer.json | 3 ++- composer.lock | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index aa3b88b4..a26f4dba 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ ], "require": { "php": ">=5.6.0", - "composer-plugin-api": "^1.0" + "composer-plugin-api": "^1.0", + "cweagans/composer-configurable-plugin": "^1.0" }, "require-dev": { "composer/composer": "~1.0", diff --git a/composer.lock b/composer.lock index 84db7e0c..a4433904 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,49 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "4083a703b8ed2ed7ff5cff34c8a226c6", - "packages": [], + "content-hash": "22bc9b56528428a17f52fed1908da23c", + "packages": [ + { + "name": "cweagans/composer-configurable-plugin", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/cweagans/composer-configurable-plugin.git", + "reference": "2df389bb1f13830fd95461d51f6eb52d02fc1c21" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cweagans/composer-configurable-plugin/zipball/2df389bb1f13830fd95461d51f6eb52d02fc1c21", + "reference": "2df389bb1f13830fd95461d51f6eb52d02fc1c21", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "composer/composer": "~1.0", + "phpunit/phpunit": "~5.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "cweagans\\Composer\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Cameron Eagans", + "email": "me@cweagans.net" + } + ], + "description": "Provides a lightweight configuration system for Composer plugins.", + "time": "2017-05-25T04:32:06+00:00" + } + ], "packages-dev": [ { "name": "behat/gherkin", From 3ca47230f2cc5d5645673b133a9a2c5fc0ff21cf Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 13:21:35 -0700 Subject: [PATCH 02/13] Add some spacing --- src/Patches.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Patches.php b/src/Patches.php index 975f306e..7e16d28d 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -32,18 +32,22 @@ class Patches implements PluginInterface, EventSubscriberInterface * @var Composer $composer */ protected $composer; + /** * @var IOInterface $io */ protected $io; + /** * @var EventDispatcher $eventDispatcher */ protected $eventDispatcher; + /** * @var ProcessExecutor $executor */ protected $executor; + /** * @var array $patches */ From 9a97f107e6bb2a29a4b12051fd4673f7e495b976 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 16:46:34 -0700 Subject: [PATCH 03/13] Use ConfigurablePlugin train and declare config vars --- src/Patches.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Patches.php b/src/Patches.php index 7e16d28d..b0264298 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -28,6 +28,8 @@ class Patches implements PluginInterface, EventSubscriberInterface { + use ConfigurablePlugin; + /** * @var Composer $composer */ @@ -67,6 +69,30 @@ public function activate(Composer $composer, IOInterface $io) $this->executor = new ProcessExecutor($this->io); $this->patches = array(); $this->installedPatches = array(); + + $this->configuration = [ + 'exit-on-patch-failure' => [ + 'type' => 'bool', + 'default' => true, + ], + 'disable-patching' => [ + 'type' => 'bool', + 'default' => false, + ], + 'disable-patching-from-dependencies' => [ + 'type' => 'bool', + 'default' => false, + ], + 'ignore-patches' => [ + 'type' => 'list', + 'default' => [], + ], + 'patch-levels' => [ + 'type' => 'list', + 'default' => ['-p1', '-p0', '-p2', '-p4'] + ], + ]; + $this->configure($this->composer->getPackage()->getExtra(), 'composer-patches'); } /** From 9dc10a0b4b379347d4c719b95bd9152d6861991f Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 16:47:41 -0700 Subject: [PATCH 04/13] Make patch levels configurable --- src/Patches.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Patches.php b/src/Patches.php index b0264298..81f256ec 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -414,7 +414,7 @@ protected function getAndApplyPatch(RemoteFilesystem $downloader, $install_path, // The order here is intentional. p1 is most likely to apply with git apply. // p0 is next likely. p2 is extremely unlikely, but for some special cases, // it might be useful. p4 is useful for Magento 2 patches - $patch_levels = array('-p1', '-p0', '-p2', '-p4'); + $patch_levels = $this->getConfig('patch-levels'); foreach ($patch_levels as $patch_level) { if ($this->io->isVerbose()) { $comment = 'Testing ability to patch with git apply.'; From 41dd90330b6dc21304a2ad258dfc98831278a426 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 16:51:13 -0700 Subject: [PATCH 05/13] Make patch reports configurable --- src/Patches.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Patches.php b/src/Patches.php index 81f256ec..0102398e 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -83,6 +83,10 @@ public function activate(Composer $composer, IOInterface $io) 'type' => 'bool', 'default' => false, ], + 'disable-patch-reports' => [ + 'type' => 'bool', + 'default' => false, + ], 'ignore-patches' => [ 'type' => 'list', 'default' => [], @@ -502,6 +506,10 @@ protected function isPatchingEnabled() */ protected function writePatchReport($patches, $directory) { + if ($this->getConfig('disable-patch-reports')) { + return; + } + $output = "This file was automatically generated by Composer Patches"; $output .= " (https://github.com/cweagans/composer-patches)\n"; $output .= "Patches applied to this directory:\n\n"; From f6b4455915d10af0bf4fa7a3160b4e5ae526345e Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 16:58:23 -0700 Subject: [PATCH 06/13] Clean up isPatchingEnabled() --- src/Patches.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Patches.php b/src/Patches.php index 0102398e..73a28bf6 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -487,15 +487,17 @@ protected function getAndApplyPatch(RemoteFilesystem $downloader, $install_path, */ protected function isPatchingEnabled() { - $extra = $this->composer->getPackage()->getExtra(); + $enabled = true; - if (empty($extra['patches']) && empty($extra['patches-ignore']) && !isset($extra['patches-file'])) { - // The root package has no patches of its own, so only allow patching if - // it has specifically opted in. - return isset($extra['enable-patching']) ? $extra['enable-patching'] : false; - } else { - return true; + $has_no_patches = empty($extra['patches']); + $has_no_patches_file = !isset($extra['patches_file']); + $patching_disabled = $this->getConfig('disable-patching'); + + if ($patching_disabled || ($has_no_patches && $has_no_patches_file)) { + $enabled = false; } + + return $enabled; } /** From 6a26d05eb3f6f4dc99f5728c90d80c38f3ec8ac0 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 17:00:15 -0700 Subject: [PATCH 07/13] Remove ignore-patches param - this won't work through envvar anyway --- src/Patches.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Patches.php b/src/Patches.php index 73a28bf6..7bbeb70e 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -87,10 +87,6 @@ public function activate(Composer $composer, IOInterface $io) 'type' => 'bool', 'default' => false, ], - 'ignore-patches' => [ - 'type' => 'list', - 'default' => [], - ], 'patch-levels' => [ 'type' => 'list', 'default' => ['-p1', '-p0', '-p2', '-p4'] From cd92c599f1ba1f6bf6284f78762f3fed69183941 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 17:09:40 -0700 Subject: [PATCH 08/13] Convert patches-file to config var --- src/Patches.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Patches.php b/src/Patches.php index 7bbeb70e..5fe2572a 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -91,6 +91,10 @@ public function activate(Composer $composer, IOInterface $io) 'type' => 'list', 'default' => ['-p1', '-p0', '-p2', '-p4'] ], + 'patches-file' => [ + 'type' => 'string', + 'default' => '', + ] ]; $this->configure($this->composer->getPackage()->getExtra(), 'composer-patches'); } @@ -260,9 +264,9 @@ public function grabPatches() $patches = $extra['patches']; return $patches; } // If it's not specified there, look for a patches-file definition. - elseif (isset($extra['patches-file'])) { + elseif ($this->getConfig('patches-file') != '') { $this->io->write('Gathering patches from patch file.'); - $patches = file_get_contents($extra['patches-file']); + $patches = file_get_contents($this->getConfig('patches-file')); $patches = json_decode($patches, true); $error = json_last_error(); if ($error != 0) { From 6186fa0ffd10541b24b7dc0255fa1fbe50e479d6 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 20 Oct 2017 17:10:11 -0700 Subject: [PATCH 09/13] Use config system for exit-on-patch-failure --- src/Patches.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Patches.php b/src/Patches.php index 5fe2572a..f181b85d 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -309,10 +309,6 @@ public function grabPatches() */ public function postInstall(PackageEvent $event) { - // Check if we should exit in failure. - $extra = $this->composer->getPackage()->getExtra(); - $exitOnFailure = getenv('COMPOSER_EXIT_ON_PATCH_FAILURE') || !empty($extra['composer-exit-on-patch-failure']); - // Get the package object for the current operation. $operation = $event->getOperation(); /** @var PackageInterface $package */ @@ -359,7 +355,7 @@ public function postInstall(PackageEvent $event) $e->getMessage() . '' ); - if ($exitOnFailure) { + if ($this->getConfig('exit-on-patch-failure')) { throw new \Exception("Cannot apply patch $description ($url)!"); } } From 72c4a192273cbba663739da84423a10d6f07f4f4 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Sat, 21 Oct 2017 23:18:42 -0600 Subject: [PATCH 10/13] Make dependency patches toggleable --- src/Patches.php | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Patches.php b/src/Patches.php index f181b85d..596bf528 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -206,32 +206,34 @@ public function gatherPatches(PackageEvent $event) $patches_ignore = isset($extra['patches-ignore']) ? $extra['patches-ignore'] : array(); // Now add all the patches from dependencies that will be installed. - $operations = $event->getOperations(); - $this->io->write('Gathering patches for dependencies. This might take a minute.'); - foreach ($operations as $operation) { - if ($operation->getJobType() == 'install' || $operation->getJobType() == 'update') { - $package = $this->getPackageFromOperation($operation); - $extra = $package->getExtra(); - if (isset($extra['patches'])) { - if (isset($patches_ignore[$package->getName()])) { - foreach ($patches_ignore[$package->getName()] as $package_name => $patches) { - if (isset($extra['patches'][$package_name])) { - $extra['patches'][$package_name] = array_diff( - $extra['patches'][$package_name], - $patches - ); + if (!$this->getConfig('disable-patching-from-dependenies')) { + $operations = $event->getOperations(); + $this->io->write('Gathering patches for dependencies. This might take a minute.'); + foreach ($operations as $operation) { + if ($operation->getJobType() == 'install' || $operation->getJobType() == 'update') { + $package = $this->getPackageFromOperation($operation); + $extra = $package->getExtra(); + if (isset($extra['patches'])) { + if (isset($patches_ignore[$package->getName()])) { + foreach ($patches_ignore[$package->getName()] as $package_name => $patches) { + if (isset($extra['patches'][$package_name])) { + $extra['patches'][$package_name] = array_diff( + $extra['patches'][$package_name], + $patches + ); + } } } + $this->patches = $this->arrayMergeRecursiveDistinct($this->patches, $extra['patches']); + } + // Unset installed patches for this package + if (isset($this->installedPatches[$package->getName()])) { + unset($this->installedPatches[$package->getName()]); } - $this->patches = $this->arrayMergeRecursiveDistinct($this->patches, $extra['patches']); - } - // Unset installed patches for this package - if (isset($this->installedPatches[$package->getName()])) { - unset($this->installedPatches[$package->getName()]); } } } - + // Merge installed patches from dependencies that did not receive an update. foreach ($this->installedPatches as $patches) { $this->patches = array_merge_recursive($this->patches, $patches); From 55c13566646130f1657c7dcc4e7963db95870830 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Sat, 21 Oct 2017 23:30:45 -0600 Subject: [PATCH 11/13] Move config key to appropriate place --- .../patches-file-patch-from-web/composer.json | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/tests/acceptance/fixtures/patches-file-patch-from-web/composer.json b/tests/acceptance/fixtures/patches-file-patch-from-web/composer.json index 7ff99527..a28dd92c 100644 --- a/tests/acceptance/fixtures/patches-file-patch-from-web/composer.json +++ b/tests/acceptance/fixtures/patches-file-patch-from-web/composer.json @@ -1,19 +1,21 @@ { - "name": "cweagans/composer-patches-test-project", - "description": "Project for use in cweagans/composer-patches acceptance tests.", - "type": "project", - "license": "BSD-2-Clause", - "repositories": [ - { - "type": "path", - "url": "../composer-patches" + "name": "cweagans/composer-patches-test-project", + "description": "Project for use in cweagans/composer-patches acceptance tests.", + "type": "project", + "license": "BSD-2-Clause", + "repositories": [ + { + "type": "path", + "url": "../composer-patches" + } + ], + "require": { + "cweagans/composer-patches": "*@dev", + "drupal/drupal": "~8.2" + }, + "extra": { + "composer-patches": { + "patches-file": "patches.json" + } } - ], - "require": { - "cweagans/composer-patches": "*@dev", - "drupal/drupal": "~8.2" - }, - "extra": { - "patches-file": "patches.json" - } } From 8ebfda3e3d9a176578d7da1dc25dd2f0b73109f0 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Mon, 23 Oct 2017 11:11:25 -0600 Subject: [PATCH 12/13] Fix a typo + fix isPatchingEnabled() --- src/Patches.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Patches.php b/src/Patches.php index 596bf528..985da43d 100644 --- a/src/Patches.php +++ b/src/Patches.php @@ -206,7 +206,7 @@ public function gatherPatches(PackageEvent $event) $patches_ignore = isset($extra['patches-ignore']) ? $extra['patches-ignore'] : array(); // Now add all the patches from dependencies that will be installed. - if (!$this->getConfig('disable-patching-from-dependenies')) { + if (!$this->getConfig('disable-patching-from-dependencies')) { $operations = $event->getOperations(); $this->io->write('Gathering patches for dependencies. This might take a minute.'); foreach ($operations as $operation) { @@ -233,7 +233,7 @@ public function gatherPatches(PackageEvent $event) } } } - + // Merge installed patches from dependencies that did not receive an update. foreach ($this->installedPatches as $patches) { $this->patches = array_merge_recursive($this->patches, $patches); @@ -488,10 +488,10 @@ protected function isPatchingEnabled() $enabled = true; $has_no_patches = empty($extra['patches']); - $has_no_patches_file = !isset($extra['patches_file']); + $has_no_patches_file = ($this->getConfig('patches-file') == ''); $patching_disabled = $this->getConfig('disable-patching'); - if ($patching_disabled || ($has_no_patches && $has_no_patches_file)) { + if ($patching_disabled || !($has_no_patches && $has_no_patches_file)) { $enabled = false; } From 5dafe1d7f9ff20a2697fc29af77d089299ed79ca Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Fri, 13 Apr 2018 12:11:25 -0500 Subject: [PATCH 13/13] Empty commit to kick off tests.