1
0
Fork 0

Add support for combining show --self with --installed or --locked (#11785)

pull/11786/head
Jordi Boggiano 2024-01-08 14:14:44 +01:00 committed by GitHub
parent 3be0ca8467
commit 534bc20beb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 5 deletions

View File

@ -27,6 +27,7 @@ use Composer\Package\Version\VersionSelector;
use Composer\Pcre\Preg; use Composer\Pcre\Preg;
use Composer\Plugin\CommandEvent; use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginEvents;
use Composer\Repository\ArrayRepository;
use Composer\Repository\InstalledArrayRepository; use Composer\Repository\InstalledArrayRepository;
use Composer\Repository\ComposerRepository; use Composer\Repository\ComposerRepository;
use Composer\Repository\CompositeRepository; use Composer\Repository\CompositeRepository;
@ -142,7 +143,7 @@ EOT
$composer = $this->tryComposer(); $composer = $this->tryComposer();
$io = $this->getIO(); $io = $this->getIO();
if ($input->getOption('installed')) { if ($input->getOption('installed') && !$input->getOption('self')) {
$io->writeError('<warning>You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.</warning>'); $io->writeError('<warning>You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.</warning>');
} }
@ -199,7 +200,7 @@ EOT
$platformRepo = new PlatformRepository([], $platformOverrides); $platformRepo = new PlatformRepository([], $platformOverrides);
$lockedRepo = null; $lockedRepo = null;
if ($input->getOption('self')) { if ($input->getOption('self') && !$input->getOption('installed') && !$input->getOption('locked')) {
$package = clone $this->requireComposer()->getPackage(); $package = clone $this->requireComposer()->getPackage();
if ($input->getOption('name-only')) { if ($input->getOption('name-only')) {
$io->write($package->getName()); $io->write($package->getName());
@ -243,6 +244,9 @@ EOT
} }
$locker = $composer->getLocker(); $locker = $composer->getLocker();
$lockedRepo = $locker->getLockedRepository(!$input->getOption('no-dev')); $lockedRepo = $locker->getLockedRepository(!$input->getOption('no-dev'));
if ($input->getOption('self')) {
$lockedRepo->addPackage(clone $composer->getPackage());
}
$repos = $installedRepo = new InstalledRepository([$lockedRepo]); $repos = $installedRepo = new InstalledRepository([$lockedRepo]);
} else { } else {
// --installed / default case // --installed / default case
@ -250,13 +254,18 @@ EOT
$composer = $this->requireComposer(); $composer = $this->requireComposer();
} }
$rootPkg = $composer->getPackage(); $rootPkg = $composer->getPackage();
$repos = $installedRepo = new InstalledRepository([$composer->getRepositoryManager()->getLocalRepository()]);
$rootRepo = new InstalledArrayRepository();
if ($input->getOption('self')) {
$rootRepo = new RootPackageRepository(clone $rootPkg);
}
if ($input->getOption('no-dev')) { if ($input->getOption('no-dev')) {
$packages = RepositoryUtils::filterRequiredPackages($installedRepo->getPackages(), $rootPkg); $packages = RepositoryUtils::filterRequiredPackages($composer->getRepositoryManager()->getLocalRepository()->getPackages(), $rootPkg);
$repos = $installedRepo = new InstalledRepository([new InstalledArrayRepository(array_map(static function ($pkg): PackageInterface { $repos = $installedRepo = new InstalledRepository([$rootRepo, new InstalledArrayRepository(array_map(static function ($pkg): PackageInterface {
return clone $pkg; return clone $pkg;
}, $packages))]); }, $packages))]);
} else {
$repos = $installedRepo = new InstalledRepository([$rootRepo, $composer->getRepositoryManager()->getLocalRepository()]);
} }
if (!$installedRepo->getPackages() && ($rootPkg->getRequires() || $rootPkg->getDevRequires())) { if (!$installedRepo->getPackages() && ($rootPkg->getRequires() || $rootPkg->getDevRequires())) {

View File

@ -29,6 +29,8 @@ class ShowCommandTest extends TestCase
public function testShow(array $command, string $expected, array $requires = []): void public function testShow(array $command, string $expected, array $requires = []): void
{ {
$this->initTempComposer([ $this->initTempComposer([
'name' => 'root/pkg',
'version' => '1.2.3',
'repositories' => [ 'repositories' => [
'packages' => [ 'packages' => [
'type' => 'package', 'type' => 'package',
@ -65,6 +67,12 @@ class ShowCommandTest extends TestCase
$this->createInstalledJson([$pkg, $major, $minor, $patch]); $this->createInstalledJson([$pkg, $major, $minor, $patch]);
$pkg = self::getPackage('vendor/locked', '3.0.0');
$pkg->setDescription('description of locked package');
$this->createComposerLock([
$pkg,
]);
$appTester = $this->getApplicationTester(); $appTester = $this->getApplicationTester();
$appTester->run(array_merge(['command' => 'show'], $command)); $appTester->run(array_merge(['command' => 'show'], $command));
self::assertSame(trim($expected), trim($appTester->getDisplay(true))); self::assertSame(trim($expected), trim($appTester->getDisplay(true)));
@ -80,6 +88,21 @@ outdated/patch 1.0.0
vendor/package 1.0.0 description of installed package', vendor/package 1.0.0 description of installed package',
]; ];
yield 'with -s and --installed shows list of installed + self package' => [
['--installed' => true, '--self' => true],
'outdated/major 1.0.0
outdated/minor 1.0.0
outdated/patch 1.0.0
root/pkg 1.2.3
vendor/package 1.0.0 description of installed package',
];
yield 'with -s and --locked shows list of installed + self package' => [
['--locked' => true, '--self' => true],
'root/pkg 1.2.3
vendor/locked 3.0.0 description of locked package',
];
yield 'with -a show available packages with description but no version' => [ yield 'with -a show available packages with description but no version' => [
['-a' => true], ['-a' => true],
'outdated/major outdated/major v2.0.0 description 'outdated/major outdated/major v2.0.0 description