From caa5545478603e18d639143abf9eddf62cd9b929 Mon Sep 17 00:00:00 2001 From: Thomas Lamy Date: Fri, 5 Jun 2020 16:48:10 +0200 Subject: [PATCH 1/3] #8945 Add "--no-dev" to outdated/show commands --- src/Composer/Command/OutdatedCommand.php | 4 ++ src/Composer/Command/ShowCommand.php | 62 +++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Composer/Command/OutdatedCommand.php b/src/Composer/Command/OutdatedCommand.php index 599087246..b08c3cb46 100644 --- a/src/Composer/Command/OutdatedCommand.php +++ b/src/Composer/Command/OutdatedCommand.php @@ -37,6 +37,7 @@ class OutdatedCommand extends ShowCommand new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --outdated option.'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'), new InputOption('ignore', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore specified package(s). Use it with the --outdated option if you don\'t want to be informed about new versions of some packages.'), + new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'), )) ->setHelp( <<getOption('minor-only')) { $args['--minor-only'] = true; } + if ($input->getOption('no-dev')) { + $args['--no-dev'] = true; + } $args['--format'] = $input->getOption('format'); $args['--ignore'] = $input->getOption('ignore'); diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index b928765f9..02b709c2d 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -81,6 +81,7 @@ class ShowCommand extends BaseCommand new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'), new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code when there are outdated packages'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'), + new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'), )) ->setHelp( <<getRepositoryManager()->getRepositories())); } else { $repos = $installedRepo = new InstalledRepository(array($this->getComposer()->getRepositoryManager()->getLocalRepository())); + $root = $composer->getPackage(); + $repo = $composer->getRepositoryManager()->getLocalRepository(); + + if ($input->getOption('no-dev')) { + $packages = $this->filterRequiredPackages($repo, $root); + } else { + $packages = $this->appendPackages($repo->getPackages(), array()); + } + $packageNames = array_keys($packages); $rootPkg = $this->getComposer()->getPackage(); if (!$installedRepo->getPackages() && ($rootPkg->getRequires() || $rootPkg->getDevRequires())) { $io->writeError('No dependencies installed. Try running composer install or update.'); @@ -348,7 +358,13 @@ EOT } } } else { - foreach ($repo->getPackages() as $package) { + $root = $composer->getPackage(); + if ($input->getOption('no-dev')) { + $packageList = $this->filterRequiredPackages($repo, $root); + } else { + $packageList = $this->appendPackages($repo->getPackages(), array()); + } + foreach ($packageList as $package) { if (!isset($packages[$type][$package->getName()]) || !is_object($packages[$type][$package->getName()]) || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<') @@ -1223,4 +1239,48 @@ EOT return $this->repositorySet; } + /** + * Find package requires and child requires + * + * @param RepositoryInterface $repo + * @param PackageInterface $package + * @param array $bucket + * @return array + */ + private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, $bucket = array()) + { + $requires = array_keys($package->getRequires()); + + $packageListNames = array_keys($bucket); + $packages = array_filter( + $repo->getPackages(), + function ($package) use ($requires, $packageListNames) { + return in_array($package->getName(), $requires, true) && ! in_array($package->getName(), $packageListNames, true); + } + ); + + $bucket = $this->appendPackages($packages, $bucket); + + foreach ($packages as $requiredPackage) { + $bucket = $this->filterRequiredPackages($repo, $requiredPackage, $bucket); + } + + return $bucket; + } + + /** + * Adds packages to the package list + * + * @param array $packages the list of packages to add + * @param array $bucket the list to add packages to + * @return array + */ + public function appendPackages(array $packages, array $bucket) + { + foreach ($packages as $package) { + $bucket[$package->getName()] = $package; + } + + return $bucket; + } } From 23cae5939c635729fbc4b19ef208e2c2b37485fd Mon Sep 17 00:00:00 2001 From: Thomas Lamy Date: Wed, 17 Jun 2020 13:15:11 +0200 Subject: [PATCH 2/3] #8945 Add "--no-dev" to outdated/show commands docs --- doc/03-cli.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/03-cli.md b/doc/03-cli.md index edfaaa9ed..6737ab605 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -408,6 +408,7 @@ php composer.phar show monolog/monolog 1.0.2 * **--tree (-t):** List your dependencies as a tree. If you pass a package name it will show the dependency tree for that package. * **--latest (-l):** List all installed packages including their latest version. * **--outdated (-o):** Implies --latest, but this lists *only* packages that have a newer version available. +* **--no-dev:** Filters dev dependencies from the package list. * **--minor-only (-m):** Use with --latest. Only shows packages that have minor SemVer-compatible updates. * **--direct (-D):** Restricts the list of packages to your direct dependencies. * **--strict:** Return a non-zero exit code when there are outdated packages. @@ -433,6 +434,7 @@ The color coding is as such: * **--strict:** Returns non-zero exit code if any package is outdated. * **--minor-only (-m):** Only shows packages that have minor SemVer-compatible updates. * **--format (-f):** Lets you pick between text (default) or json output format. +* **--no-dev:** Do not show outdated dev dependencies. ## browse / home From 6869a1a5cb8793303738f874d15549e961de794b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 16 Jul 2020 15:36:17 +0200 Subject: [PATCH 3/3] Clean up a little to make impl less invasive and to handle replacers/providers --- src/Composer/Command/ShowCommand.php | 65 ++++++++++------------------ 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index 02b709c2d..08b53cef6 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -22,6 +22,7 @@ use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionSelector; use Composer\Plugin\CommandEvent; use Composer\Plugin\PluginEvents; +use Composer\Repository\InstalledArrayRepository; use Composer\Repository\ComposerRepository; use Composer\Repository\CompositeRepository; use Composer\Repository\PlatformRepository; @@ -196,17 +197,18 @@ EOT $installedRepo = new InstalledRepository(array($lockedRepo)); $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories())); } else { - $repos = $installedRepo = new InstalledRepository(array($this->getComposer()->getRepositoryManager()->getLocalRepository())); - $root = $composer->getPackage(); - $repo = $composer->getRepositoryManager()->getLocalRepository(); + // --installed / default case + if (!$composer) { + $composer = $this->getComposer(); + } + $rootPkg = $composer->getPackage(); + $repos = $installedRepo = new InstalledRepository(array($composer->getRepositoryManager()->getLocalRepository())); if ($input->getOption('no-dev')) { - $packages = $this->filterRequiredPackages($repo, $root); - } else { - $packages = $this->appendPackages($repo->getPackages(), array()); + $packages = $this->filterRequiredPackages($installedRepo, $rootPkg); + $repos = $installedRepo = new InstalledRepository(array(new InstalledArrayRepository(array_map(function ($pkg) { return clone $pkg; }, $packages)))); } - $packageNames = array_keys($packages); - $rootPkg = $this->getComposer()->getPackage(); + if (!$installedRepo->getPackages() && ($rootPkg->getRequires() || $rootPkg->getDevRequires())) { $io->writeError('No dependencies installed. Try running composer install or update.'); } @@ -358,13 +360,7 @@ EOT } } } else { - $root = $composer->getPackage(); - if ($input->getOption('no-dev')) { - $packageList = $this->filterRequiredPackages($repo, $root); - } else { - $packageList = $this->appendPackages($repo->getPackages(), array()); - } - foreach ($packageList as $package) { + foreach ($repo->getPackages() as $package) { if (!isset($packages[$type][$package->getName()]) || !is_object($packages[$type][$package->getName()]) || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<') @@ -1239,6 +1235,7 @@ EOT return $this->repositorySet; } + /** * Find package requires and child requires * @@ -1249,36 +1246,18 @@ EOT */ private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, $bucket = array()) { - $requires = array_keys($package->getRequires()); + $requires = $package->getRequires(); - $packageListNames = array_keys($bucket); - $packages = array_filter( - $repo->getPackages(), - function ($package) use ($requires, $packageListNames) { - return in_array($package->getName(), $requires, true) && ! in_array($package->getName(), $packageListNames, true); + foreach ($repo->getPackages() as $candidate) { + foreach ($candidate->getNames() as $name) { + if (isset($requires[$name])) { + if (!in_array($candidate, $bucket, true)) { + $bucket[] = $candidate; + $bucket = $this->filterRequiredPackages($repo, $candidate, $bucket); + } + break; + } } - ); - - $bucket = $this->appendPackages($packages, $bucket); - - foreach ($packages as $requiredPackage) { - $bucket = $this->filterRequiredPackages($repo, $requiredPackage, $bucket); - } - - return $bucket; - } - - /** - * Adds packages to the package list - * - * @param array $packages the list of packages to add - * @param array $bucket the list to add packages to - * @return array - */ - public function appendPackages(array $packages, array $bucket) - { - foreach ($packages as $package) { - $bucket[$package->getName()] = $package; } return $bucket;