1
0
Fork 0

Merge remote-tracking branch 'curry684/sort-suggestions'

pull/4964/head
Jordi Boggiano 2016-02-25 18:47:19 +00:00
commit 44cfad36b7
2 changed files with 87 additions and 29 deletions

View File

@ -320,8 +320,13 @@ Lists all packages suggested by currently installed set of packages. You can
optionally pass one or multiple package names in the format of `vendor/package` optionally pass one or multiple package names in the format of `vendor/package`
to limit output to suggestions made by those packages only. to limit output to suggestions made by those packages only.
Use the `--by-package` or `--by-suggestion` flags to group the output by
the package offering the suggestions or the suggested packages respectively.
### Options ### Options
* **--by-package:** Groups output by suggesting package.
* **--by-suggestion:** Groups output by suggested package.
* **--no-dev:** Excludes suggestions from `require-dev` packages. * **--no-dev:** Excludes suggestions from `require-dev` packages.
* **--verbose (-v):** Increased verbosity adds suggesting package name and * **--verbose (-v):** Increased verbosity adds suggesting package name and
reason for suggestion. reason for suggestion.

View File

@ -12,6 +12,7 @@
namespace Composer\Command; namespace Composer\Command;
use Composer\Repository\PlatformRepository;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
@ -25,14 +26,16 @@ class SuggestsCommand extends BaseCommand
->setName('suggests') ->setName('suggests')
->setDescription('Show package suggestions') ->setDescription('Show package suggestions')
->setDefinition(array( ->setDefinition(array(
new InputOption('by-package', null, InputOption::VALUE_NONE, 'Groups output by suggesting package'),
new InputOption('by-suggestion', null, InputOption::VALUE_NONE, 'Groups output by suggested package'),
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Exclude suggestions from require-dev packages'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Exclude suggestions from require-dev packages'),
new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that you want to list suggestions from.'), new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that you want to list suggestions from.'),
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
The <info>%command.name%</info> command shows suggested packages. The <info>%command.name%</info> command shows a sorted list of suggested packages.
With <info>-v</info> you also see which package suggested it and why. Enabling <info>-v</info> implies <info>--by-package --by-suggestion</info>, showing both lists.
EOT EOT
) )
@ -55,44 +58,94 @@ EOT
$filter = $input->getArgument('packages'); $filter = $input->getArgument('packages');
foreach ($packages as $package) { // First assemble lookup list of packages that are installed, replaced or provided
if (empty($package['suggest'])) { $installed = array();
continue; foreach($packages as $package) {
$installed[] = $package['name'];
if (!empty($package['provide'])) {
$installed = array_merge($installed, array_keys($package['provide']));
} }
if (!empty($filter) && !in_array($package['name'], $filter)) { if (!empty($package['replace'])) {
continue; $installed = array_merge($installed, array_keys($package['replace']));
} }
$this->printSuggestions($packages, $package['name'], $package['suggest']);
} }
}
protected function printSuggestions($installed, $source, $suggestions) // Undub and sort the install list into a sorted lookup array
{ $installed = array_flip($installed);
foreach ($suggestions as $suggestion => $reason) { ksort($installed);
foreach ($installed as $package) {
if ($package['name'] === $suggestion) { // Init platform repo
continue 2; $platform = new PlatformRepository(array(), $this->getComposer()->getConfig()->get('platform') ?: array());
// Next gather all suggestions that are not in that list
$suggesters = array();
$suggested = array();
foreach ($packages as $package) {
$packageName = $package['name'];
if ((!empty($filter) && !in_array($packageName, $filter)) || empty($package['suggest'])) {
continue;
}
foreach ($package['suggest'] as $suggestion => $reason) {
if (false === strpos('/', $suggestion) && !is_null($platform->findPackage($suggestion, '*')))
continue;
if (!isset($installed[$suggestion])) {
$suggesters[$packageName][$suggestion] = $reason;
$suggested[$suggestion][$packageName] = $reason;
} }
} }
if (empty($reason)) {
$reason = '*';
}
$this->printSuggestion($source, $suggestion, $reason);
} }
} ksort($suggesters);
ksort($suggested);
protected function printSuggestion($package, $suggestion, $reason) // Determine output mode
{ $mode = 0;
$io = $this->getIO(); $io = $this->getIO();
if ($input->getOption('by-package')) {
$mode |= 1;
}
if ($input->getOption('by-suggestion')) {
$mode |= 2;
}
if ($io->isVerbose()) { if ($io->isVerbose()) {
$io->write(sprintf('<comment>%s</comment> suggests <info>%s</info>: %s', $package, $suggestion, $reason)); $mode = ~0;
} else { }
$io->write(sprintf('<info>%s</info>', $suggestion));
// Simple mode
if ($mode === 0) {
foreach (array_keys($suggested) as $suggestion) {
$io->write(sprintf('<info>%s</info>', $suggestion));
}
return;
}
// Grouped by package
if ($mode & 1) {
foreach ($suggesters as $suggester => $suggestions) {
$io->write(sprintf('<comment>%s</comment> suggests:', $suggester));
foreach ($suggestions as $suggestion => $reason) {
$io->write(sprintf(' - <info>%s</info>: %s', $suggestion, $reason ?: '*'));
}
$io->write('');
}
}
// Grouped by suggestion
if ($mode & 2) {
// Improve readability in full mode
if ($mode & 1) {
$io->write(str_repeat('-', 78));
}
foreach ($suggested as $suggestion => $suggesters) {
$io->write(sprintf('<comment>%s</comment> is suggested by:', $suggestion));
foreach ($suggesters as $suggester => $reason) {
$io->write(sprintf(' - <info>%s</info>: %s', $suggester, $reason ?: '*'));
}
$io->write('');
}
} }
} }
} }