From 085f53b6d8135dccc481db13179af5da57ab9b73 Mon Sep 17 00:00:00 2001 From: Raphael Mobis Tacla Date: Sun, 17 Nov 2024 08:56:35 -0300 Subject: [PATCH] allow setting patches file location through `--patches-file` option (fixes #25) (#26) * allow setting patches file location through --patches-file option * update rector to ^1.2.5 * keep permissions when overwriting composer.json/patches file * fix incorrectly checking if --patches-file option was passed * make PATCHES_FILE_OPTION constant private * add `--patches-file` info on README.md --- README.md | 6 ++++ composer.json | 2 +- src/Command/GenerateCommand.php | 26 ++++++++++++++--- .../ComposerPatchesConfigurationUpdater.php | 28 ++++++++++++++++++- ...omposerPatchesConfigurationUpdaterTest.php | 23 +++++++++++++-- ...json => composer.already_has_patches.json} | 0 .../patches_file.already_has_patches.json | 7 +++++ 7 files changed, 84 insertions(+), 8 deletions(-) rename tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/{already_has_patches.json => composer.already_has_patches.json} (100%) create mode 100644 tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/patches_file.already_has_patches.json diff --git a/README.md b/README.md index 30bb2981..f8da51e3 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,12 @@ Also, it will add configuration for `cweagans/composer-patches` to your `compose } ``` +Optionally, if you use a [patches file](https://docs.cweagans.net/composer-patches/usage/defining-patches/#patches-file) you can specify its path using the `--patches-file` option: + +```bash +vendor/bin/vendor-patches generate --patches-file=patches.json +``` + That's it!
diff --git a/composer.json b/composer.json index 39313225..ae4b9423 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "require-dev": { "phpunit/phpunit": "^10.5", - "rector/rector": "^0.18.13", + "rector/rector": "^1.2.5", "phpstan/phpstan": "^1.10.50", "symplify/easy-coding-standard": "^12.0", "symplify/phpstan-extensions": "^11.1", diff --git a/src/Command/GenerateCommand.php b/src/Command/GenerateCommand.php index 6ce2739f..73a24eb2 100644 --- a/src/Command/GenerateCommand.php +++ b/src/Command/GenerateCommand.php @@ -7,6 +7,7 @@ use Nette\Utils\FileSystem; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symplify\VendorPatches\Composer\ComposerPatchesConfigurationUpdater; @@ -18,6 +19,8 @@ final class GenerateCommand extends Command { + private const PATCHES_FILE_OPTION = 'patches-file'; + public function __construct( private readonly OldToNewFilesFinder $oldToNewFilesFinder, private readonly PatchDiffer $patchDiffer, @@ -33,6 +36,12 @@ protected function configure(): void { $this->setName('generate'); $this->setDescription('Generate patches from /vendor directory'); + $this->addOption( + self::PATCHES_FILE_OPTION, + null, + InputOption::VALUE_OPTIONAL, + 'Path to the patches file, relative to project root' + ); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -76,10 +85,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if ($composerExtraPatches !== []) { - $this->composerPatchesConfigurationUpdater->updateComposerJsonAndPrint( - getcwd() . '/composer.json', - $composerExtraPatches - ); + $patchesFilePath = $input->getOption(self::PATCHES_FILE_OPTION); + + if (is_string($patchesFilePath)) { + $this->composerPatchesConfigurationUpdater->updatePatchesFileJsonAndPrint( + FileSystem::joinPaths(getcwd(), $patchesFilePath), + $composerExtraPatches + ); + } else { + $this->composerPatchesConfigurationUpdater->updateComposerJsonAndPrint( + getcwd() . '/composer.json', + $composerExtraPatches + ); + } } if ($addedPatchFilesByPackageName !== []) { diff --git a/src/Composer/ComposerPatchesConfigurationUpdater.php b/src/Composer/ComposerPatchesConfigurationUpdater.php index 0814f35b..f0c071e8 100644 --- a/src/Composer/ComposerPatchesConfigurationUpdater.php +++ b/src/Composer/ComposerPatchesConfigurationUpdater.php @@ -18,6 +18,21 @@ public function __construct( ) { } + /** + * @api + * @param mixed[] $composerExtraPatches + * @return mixed[] + */ + public function updatePatchesFileJson(string $patchesFilePath, array $composerExtraPatches): array + { + $patchesFileContents = FileSystem::read($patchesFilePath); + $patchesFileJson = Json::decode($patchesFileContents, Json::FORCE_ARRAY); + + return $this->parametersMerger->merge($patchesFileJson, [ + 'patches' => $composerExtraPatches, + ]); + } + /** * @api * @param mixed[] $composerExtraPatches @@ -54,6 +69,17 @@ public function updateComposerJsonAndPrint(string $composerJsonFilePath, array $ // print composer.json $composerJsonFileContents = Json::encode($composerJson, Json::PRETTY); - FileSystem::write($composerJsonFilePath, $composerJsonFileContents); + FileSystem::write($composerJsonFilePath, $composerJsonFileContents, null); + } + + /** + * @param mixed[] $composerExtraPatches + */ + public function updatePatchesFileJsonAndPrint(string $patchesFilePath, array $composerExtraPatches): void + { + $patchesFileJson = $this->updatePatchesFileJson($patchesFilePath, $composerExtraPatches); + + $patchesFileContents = Json::encode($patchesFileJson, Json::PRETTY); + FileSystem::write($patchesFilePath, $patchesFileContents, null); } } diff --git a/tests/Composer/ComposerPatchesConfigurationUpdater/ComposerPatchesConfigurationUpdaterTest.php b/tests/Composer/ComposerPatchesConfigurationUpdater/ComposerPatchesConfigurationUpdaterTest.php index bb15b695..1361e159 100644 --- a/tests/Composer/ComposerPatchesConfigurationUpdater/ComposerPatchesConfigurationUpdaterTest.php +++ b/tests/Composer/ComposerPatchesConfigurationUpdater/ComposerPatchesConfigurationUpdaterTest.php @@ -9,12 +9,12 @@ final class ComposerPatchesConfigurationUpdaterTest extends AbstractTestCase { - public function test(): void + public function testComposerJson(): void { $composerPatchesConfigurationUpdater = $this->make(ComposerPatchesConfigurationUpdater::class); $composerJson = $composerPatchesConfigurationUpdater->updateComposerJson( - __DIR__ . '/Fixture/already_has_patches.json', + __DIR__ . '/Fixture/composer.already_has_patches.json', [ 'some_package' => ['some.patch'], ] @@ -27,4 +27,23 @@ public function test(): void ], ], $composerJson['extra']); } + + public function testPatchesFile(): void + { + $composerPatchesConfigurationUpdater = $this->make(ComposerPatchesConfigurationUpdater::class); + + $patchesFileJson = $composerPatchesConfigurationUpdater->updatePatchesFileJson( + __DIR__ . '/Fixture/patches_file.already_has_patches.json', + [ + 'some_package' => ['some.patch'], + ] + ); + + $this->assertSame([ + 'patches' => [ + 'some_package' => ['some.patch'], + 'symfony/console' => ['patches/symfony-console-style-symfonystyle-php.patch'], + ], + ], $patchesFileJson); + } } diff --git a/tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/already_has_patches.json b/tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/composer.already_has_patches.json similarity index 100% rename from tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/already_has_patches.json rename to tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/composer.already_has_patches.json diff --git a/tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/patches_file.already_has_patches.json b/tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/patches_file.already_has_patches.json new file mode 100644 index 00000000..29ea88f1 --- /dev/null +++ b/tests/Composer/ComposerPatchesConfigurationUpdater/Fixture/patches_file.already_has_patches.json @@ -0,0 +1,7 @@ +{ + "patches": { + "symfony/console": [ + "patches/symfony-console-style-symfonystyle-php.patch" + ] + } +}