From 5796976ddcbe5d59bab2e8405a856288fd0bedce Mon Sep 17 00:00:00 2001 From: TBiesenbeek Date: Mon, 24 Jun 2024 14:59:56 +0200 Subject: [PATCH 1/4] Made patches.lock.json configurable --- src/Command/RelockCommand.php | 6 ++-- src/Plugin/Patches.php | 55 +++++++++++++++++++------------- tests/unit/PatchesPluginTest.php | 5 +-- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/Command/RelockCommand.php b/src/Command/RelockCommand.php index 52d9553..c97d8d8 100644 --- a/src/Command/RelockCommand.php +++ b/src/Command/RelockCommand.php @@ -13,7 +13,9 @@ class RelockCommand extends PatchesCommandBase protected function configure(): void { $this->setName('patches-relock'); - $filename = pathinfo(Patches::getPatchesLockFilePath(), \PATHINFO_BASENAME); + $plugin = $this->getPatchesPluginInstance(); + + $filename = pathinfo($plugin->getPatchesLockFilePath(), \PATHINFO_BASENAME); $this->setDescription("Find all patches defined in the project and re-write $filename."); $this->setAliases(['prl']); } @@ -29,7 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int unlink($plugin->getLockFile()->getPath()); } $plugin->createNewPatchesLock(); - $output->write(' - patches.lock.json has been recreated successfully.', true); + $output->write(" - {$plugin->getPatchesLockFilePath()} has been recreated successfully.", true); return 0; } } diff --git a/src/Plugin/Patches.php b/src/Plugin/Patches.php index 5b74ca1..1098a7e 100644 --- a/src/Plugin/Patches.php +++ b/src/Plugin/Patches.php @@ -85,23 +85,6 @@ class Patches implements PluginInterface, EventSubscriberInterface, Capable protected JsonFile $lockFile; - /** - * Get the path to the current patches lock file. - */ - public static function getPatchesLockFilePath(): string - { - $composer_file = \Composer\Factory::getComposerFile(); - - $dir = dirname(realpath($composer_file)); - $base = pathinfo($composer_file, \PATHINFO_FILENAME); - - if ($base === 'composer') { - return "$dir/patches.lock.json"; - } - - return "$dir/$base-patches.lock.json"; - } - /** * Apply plugin modifications to composer * @@ -115,12 +98,6 @@ public function activate(Composer $composer, IOInterface $io): void $this->executor = new ProcessExecutor($this->io); $this->patches = array(); $this->installedPatches = array(); - $this->lockFile = new JsonFile( - static::getPatchesLockFilePath(), - null, - $this->io - ); - $this->locker = new Locker($this->lockFile); $this->configuration = [ 'disable-resolvers' => [ 'type' => 'list', @@ -146,12 +123,44 @@ public function activate(Composer $composer, IOInterface $io): void 'type' => 'string', 'default' => 'patches.json', ], + 'patches-lock-file' => [ + 'type' => 'string', + 'default' => 'patches.lock.json', + ], "ignore-dependency-patches" => [ 'type' => 'list', 'default' => [], ], ]; $this->configure($this->composer->getPackage()->getExtra(), 'composer-patches'); + $this->lockFile = new JsonFile( + $this->getPatchesLockFilePath(), + null, + $this->io + ); + $this->locker = new Locker($this->lockFile); + } + + /** + * Get the path to the current patches lock file. + */ + public function getPatchesLockFilePath(): string + { + $composer_file = \Composer\Factory::getComposerFile(); + + $dir = dirname(realpath($composer_file)); + $base = pathinfo($composer_file, \PATHINFO_FILENAME); + + $lock_file = $this->getConfig('patches-lock-file'); + if (!is_string($lock_file)) { + $lock_file = 'patches-lock-file'; + } + + if ($base === 'composer') { + return "$dir/$lock_file"; + } + + return "$dir/$base-$lock_file"; } /** diff --git a/tests/unit/PatchesPluginTest.php b/tests/unit/PatchesPluginTest.php index 82e804f..469297a 100644 --- a/tests/unit/PatchesPluginTest.php +++ b/tests/unit/PatchesPluginTest.php @@ -40,12 +40,13 @@ public function testActivate() */ public function testGetPatchesLockFilePath() { - $path = Patches::getPatchesLockFilePath(); + $plugin = new Patches(); + $path = $plugin->getPatchesLockFilePath(); $filename = pathinfo($path, \PATHINFO_BASENAME); $this->assertEquals('patches.lock.json', $filename); putenv('COMPOSER=mycomposer.json'); - $path = Patches::getPatchesLockFilePath(); + $path = $plugin->getPatchesLockFilePath(); $filename = pathinfo($path, \PATHINFO_BASENAME); $this->assertEquals('mycomposer-patches.lock.json', $filename); } From 50fff6743a5cf1cf7b43215010d5b0c77d2466ca Mon Sep 17 00:00:00 2001 From: TBiesenbeek Date: Tue, 25 Jun 2024 08:12:37 +0200 Subject: [PATCH 2/4] Remove unnecessary default --- src/Plugin/Patches.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Plugin/Patches.php b/src/Plugin/Patches.php index 1098a7e..d4bed5e 100644 --- a/src/Plugin/Patches.php +++ b/src/Plugin/Patches.php @@ -152,9 +152,6 @@ public function getPatchesLockFilePath(): string $base = pathinfo($composer_file, \PATHINFO_FILENAME); $lock_file = $this->getConfig('patches-lock-file'); - if (!is_string($lock_file)) { - $lock_file = 'patches-lock-file'; - } if ($base === 'composer') { return "$dir/$lock_file"; From 18be4f3d4d12ff0155a0646dd77e38796b6e4de0 Mon Sep 17 00:00:00 2001 From: TBiesenbeek Date: Tue, 25 Jun 2024 08:13:09 +0200 Subject: [PATCH 3/4] Fix test by instancing the plugin correctly --- tests/unit/PatchesPluginTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/unit/PatchesPluginTest.php b/tests/unit/PatchesPluginTest.php index 469297a..57326cb 100644 --- a/tests/unit/PatchesPluginTest.php +++ b/tests/unit/PatchesPluginTest.php @@ -40,7 +40,14 @@ public function testActivate() */ public function testGetPatchesLockFilePath() { + $package = new RootPackage('cweagans/composer-patches', '0.0.0.0', '0.0.0'); + $io = new NullIO(); + $composer = new Composer(); + $composer->setPackage($package); + $plugin = new Patches(); + $plugin->activate($composer, $io); + $path = $plugin->getPatchesLockFilePath(); $filename = pathinfo($path, \PATHINFO_BASENAME); $this->assertEquals('patches.lock.json', $filename); From 2fef96190a41920f94bbc53f54dc96ca531ca3af Mon Sep 17 00:00:00 2001 From: TBiesenbeek Date: Tue, 25 Jun 2024 08:13:45 +0200 Subject: [PATCH 4/4] Removed exact file from description --- src/Command/RelockCommand.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Command/RelockCommand.php b/src/Command/RelockCommand.php index c97d8d8..c75140f 100644 --- a/src/Command/RelockCommand.php +++ b/src/Command/RelockCommand.php @@ -13,10 +13,7 @@ class RelockCommand extends PatchesCommandBase protected function configure(): void { $this->setName('patches-relock'); - $plugin = $this->getPatchesPluginInstance(); - - $filename = pathinfo($plugin->getPatchesLockFilePath(), \PATHINFO_BASENAME); - $this->setDescription("Find all patches defined in the project and re-write $filename."); + $this->setDescription("Find all patches defined in the project and re-write the patches lock file."); $this->setAliases(['prl']); } @@ -31,7 +28,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int unlink($plugin->getLockFile()->getPath()); } $plugin->createNewPatchesLock(); - $output->write(" - {$plugin->getPatchesLockFilePath()} has been recreated successfully.", true); + $filename = pathinfo($plugin->getPatchesLockFilePath(), \PATHINFO_BASENAME); + $output->write(" - $filename has been recreated successfully.", true); return 0; } }