Skip to content

Commit

Permalink
allow setting patches file location through --patches-file option
Browse files Browse the repository at this point in the history
  • Loading branch information
rmobis committed Sep 25, 2024
1 parent 585e750 commit 17cefb9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
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
{
public 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
);
if ($input->hasOption(self::PATCHES_FILE_OPTION)) {
$patchesFilePath = $input->getOption(self::PATCHES_FILE_OPTION);

$this->composerPatchesConfigurationUpdater->updatePatchesFileJsonAndPrint(
getcwd() . $patchesFilePath,
$composerExtraPatches
);
} else {
$this->composerPatchesConfigurationUpdater->updateComposerJsonAndPrint(
getcwd() . '/composer.json',
$composerExtraPatches
);
}
}

if ($addedPatchFilesByPackageName !== []) {
Expand Down
26 changes: 26 additions & 0 deletions 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 @@ -56,4 +71,15 @@ public function updateComposerJsonAndPrint(string $composerJsonFilePath, array $
$composerJsonFileContents = Json::encode($composerJson, Json::PRETTY);
FileSystem::write($composerJsonFilePath, $composerJsonFileContents);
}

/**
* @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);
}
}
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 17cefb9

Please sign in to comment.