1
0
Fork 0

Add ability to reinstall packages by type (#12114)

Fixes #11364
pull/12129/head
Jordi Boggiano 2024-09-21 13:37:55 +02:00 committed by GitHub
parent 58905ffe4e
commit 3a2a18175d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 87 additions and 20 deletions

View File

@ -117,6 +117,36 @@ trait CompletionTrait
};
}
/**
* Suggest package names from installed.
*/
private function suggestInstalledPackageTypes(bool $includeRootPackage = true): \Closure
{
return function (CompletionInput $input) use ($includeRootPackage): array {
$composer = $this->requireComposer();
$installedRepos = [];
if ($includeRootPackage) {
$installedRepos[] = new RootPackageRepository(clone $composer->getPackage());
}
$locker = $composer->getLocker();
if ($locker->isLocked()) {
$installedRepos[] = $locker->getLockedRepository(true);
} else {
$installedRepos[] = $composer->getRepositoryManager()->getLocalRepository();
}
$installedRepo = new InstalledRepository($installedRepos);
return array_values(array_unique(
array_map(static function (PackageInterface $package) {
return $package->getType();
}, $installedRepo->getPackages())
));
};
}
/**
* Suggest package names available on all configured repositories.
*/

View File

@ -51,7 +51,8 @@ class ReinstallCommand extends BaseCommand
new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader'),
new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'),
new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'),
new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of package names to reinstall, can include a wildcard (*) to match any substring.', null, $this->suggestInstalledPackage(false)),
new InputOption('type', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Filter packages to reinstall by type(s)', null, $this->suggestInstalledPackageTypes(false)),
new InputArgument('packages', InputArgument::IS_ARRAY, 'List of package names to reinstall, can include a wildcard (*) to match any substring.', null, $this->suggestInstalledPackage(false)),
])
->setHelp(
<<<EOT
@ -77,6 +78,20 @@ EOT
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$packagesToReinstall = [];
$packageNamesToReinstall = [];
if (\count($input->getOption('type')) > 0) {
if (\count($input->getArgument('packages')) > 0) {
throw new \InvalidArgumentException('You cannot specify package names and filter by type at the same time.');
}
foreach ($localRepo->getCanonicalPackages() as $package) {
if (in_array($package->getType(), $input->getOption('type'), true)) {
$packagesToReinstall[] = $package;
$packageNamesToReinstall[] = $package->getName();
}
}
} else {
if (\count($input->getArgument('packages')) === 0) {
throw new \InvalidArgumentException('You must pass one or more package names to be reinstalled.');
}
foreach ($input->getArgument('packages') as $pattern) {
$patternRegexp = BasePackage::packageNameToRegexp($pattern);
$matched = false;
@ -92,8 +107,9 @@ EOT
$io->writeError('<warning>Pattern "' . $pattern . '" does not match any currently installed packages.</warning>');
}
}
}
if (!$packagesToReinstall) {
if (0 === \count($packagesToReinstall)) {
$io->writeError('<warning>Found no packages to reinstall, aborting.</warning>');
return 1;

View File

@ -19,49 +19,70 @@ class ReinstallCommandTest extends TestCase
{
/**
* @dataProvider caseProvider
* @param array<string> $packages
* @param array<mixed> $options
* @param string $expected
*/
public function testReinstallCommand(array $packages, string $expected): void
public function testReinstallCommand(array $options, string $expected): void
{
$this->initTempComposer([
'require' => [
'root/req' => '1.*',
'root/anotherreq' => '2.*'
],
'require-dev' => [
'root/anotherreq' => '2.*',
'root/anotherreq2' => '2.*',
'root/lala' => '2.*',
]
]);
$rootReqPackage = self::getPackage('root/req');
$anotherReqPackage = self::getPackage('root/anotherreq');
$anotherReqPackage2 = self::getPackage('root/anotherreq2');
$anotherReqPackage3 = self::getPackage('root/lala');
$rootReqPackage->setType('metapackage');
$anotherReqPackage->setType('metapackage');
$anotherReqPackage2->setType('metapackage');
$anotherReqPackage3->setType('metapackage');
$this->createComposerLock([$rootReqPackage], [$anotherReqPackage]);
$this->createInstalledJson([$rootReqPackage], [$anotherReqPackage]);
$this->createComposerLock([$rootReqPackage], [$anotherReqPackage, $anotherReqPackage2, $anotherReqPackage3]);
$this->createInstalledJson([$rootReqPackage], [$anotherReqPackage, $anotherReqPackage2, $anotherReqPackage3]);
$appTester = $this->getApplicationTester();
$appTester->run([
$appTester->run(array_merge([
'command' => 'reinstall',
'--no-progress' => true,
'--no-plugins' => true,
'packages' => $packages
]);
], $options));
self::assertSame($expected, trim($appTester->getDisplay(true)));
}
public function caseProvider(): Generator
{
yield 'reinstall a package' => [
['root/req', 'root/anotherreq'],
yield 'reinstall a package by name' => [
['packages' => ['root/req', 'root/anotherreq*']],
'- Removing root/req (1.0.0)
- Removing root/anotherreq2 (1.0.0)
- Removing root/anotherreq (1.0.0)
- Installing root/anotherreq (1.0.0)
- Installing root/anotherreq2 (1.0.0)
- Installing root/req (1.0.0)'
];
yield 'reinstall packages by type' => [
['--type' => ['metapackage']],
'- Removing root/req (1.0.0)
- Removing root/lala (1.0.0)
- Removing root/anotherreq2 (1.0.0)
- Removing root/anotherreq (1.0.0)
- Installing root/anotherreq (1.0.0)
- Installing root/anotherreq2 (1.0.0)
- Installing root/lala (1.0.0)
- Installing root/req (1.0.0)'
];
yield 'reinstall a package that is not installed' => [
['root/unknownreq'],
['packages' => ['root/unknownreq']],
'<warning>Pattern "root/unknownreq" does not match any currently installed packages.</warning>
<warning>Found no packages to reinstall, aborting.</warning>'
];