Skip to content

Commit

Permalink
allow setting patches file location through --patches-file option (f…
Browse files Browse the repository at this point in the history
…ixes #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
  • Loading branch information
rmobis authored Nov 17, 2024
1 parent 585e750 commit 085f53b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

<br>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 22 additions & 4 deletions src/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 !== []) {
Expand Down
28 changes: 27 additions & 1 deletion src/Composer/ComposerPatchesConfigurationUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"patches": {
"symfony/console": [
"patches/symfony-console-style-symfonystyle-php.patch"
]
}
}

0 comments on commit 085f53b

Please sign in to comment.