Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting patches file location through --patches-file option (fixes #25) #26

Merged
merged 6 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
]
}
}
Loading