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

Made patches.lock.json configurable #533 #584

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/Command/RelockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class RelockCommand extends PatchesCommandBase
protected function configure(): void
{
$this->setName('patches-relock');
$filename = pathinfo(Patches::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']);
}

Expand All @@ -29,7 +28,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
unlink($plugin->getLockFile()->getPath());
}
$plugin->createNewPatchesLock();
$output->write(' - <info>patches.lock.json</info> has been recreated successfully.', true);
$filename = pathinfo($plugin->getPatchesLockFilePath(), \PATHINFO_BASENAME);
$output->write(" - <info>$filename</info> has been recreated successfully.", true);
return 0;
}
}
52 changes: 29 additions & 23 deletions src/Plugin/Patches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember why this was a static function. Might be worth checking if this breaks any of the tests.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By not calling the method while registering, its fixed.
Parsing extra's just for the description is not worth it?

{
$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
*
Expand All @@ -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',
Expand All @@ -146,12 +123,41 @@ 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 ($base === 'composer') {
return "$dir/$lock_file";
}

return "$dir/$base-$lock_file";
Comment on lines +156 to +160
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, if you set COMPOSER=composer-foo.json and run any of the composer patches commands, you'll get composer-foo-patches.lock.json. If we set patches-lock-file to asdf.lock.json and also set COMPOSER=composer-foo.json, I think we'll get composer-foo-asdf.lock.json.

This isn't incorrect, but it is something that docs and tests will need to account for. Just flagging for visibility.

}

/**
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/PatchesPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ public function testActivate()
*/
public function testGetPatchesLockFilePath()
{
$path = Patches::getPatchesLockFilePath();
$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);

putenv('COMPOSER=mycomposer.json');
$path = Patches::getPatchesLockFilePath();
$path = $plugin->getPatchesLockFilePath();
$filename = pathinfo($path, \PATHINFO_BASENAME);
$this->assertEquals('mycomposer-patches.lock.json', $filename);
}
Expand Down