Add json output for show command

main
Alexander Schwenn 7 years ago
parent 818687849d
commit c70aaa421d

@ -14,6 +14,7 @@ namespace Composer\Command;
use Composer\DependencyResolver\Pool; use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\DefaultPolicy; use Composer\DependencyResolver\DefaultPolicy;
use Composer\Json\JsonFile;
use Composer\Package\CompletePackageInterface; use Composer\Package\CompletePackageInterface;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
use Composer\Package\BasePackage; use Composer\Package\BasePackage;
@ -74,6 +75,7 @@ class ShowCommand extends BaseCommand
new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --outdated option.'), new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --outdated option.'),
new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'), 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('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'),
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
The show command displays detailed information about a package, or The show command displays detailed information about a package, or
@ -114,6 +116,13 @@ EOT
return 1; return 1;
} }
$format = $input->getOption('format');
if (!in_array($format, array('text', 'json'))) {
$io->writeError(sprintf('Unsupported format "%s". See help for supported formats.', $format));
return 1;
}
// init repos // init repos
$platformOverrides = array(); $platformOverrides = array();
if ($composer) { if ($composer) {
@ -378,63 +387,67 @@ EOT
} }
} }
foreach ($viewData as $type => $packages) { if ('json' === $format) {
$nameLength = $viewMetaData[$type]['nameLength']; $io->write(JsonFile::encode($viewData));
$versionLength = $viewMetaData[$type]['versionLength']; } else {
$latestLength = $viewMetaData[$type]['latestLength']; foreach ($viewData as $type => $packages) {
$nameLength = $viewMetaData[$type]['nameLength'];
$versionLength = $viewMetaData[$type]['versionLength'];
$latestLength = $viewMetaData[$type]['latestLength'];
$writeVersion = $nameLength + $versionLength + 3 <= $width; $writeVersion = $nameLength + $versionLength + 3 <= $width;
$writeLatest = $nameLength + $versionLength + $latestLength + 3 <= $width; $writeLatest = $nameLength + $versionLength + $latestLength + 3 <= $width;
$writeDescription = $nameLength + $versionLength + $latestLength + 24 <= $width; $writeDescription = $nameLength + $versionLength + $latestLength + 24 <= $width;
if ($writeLatest && !$io->isDecorated()) { if ($writeLatest && !$io->isDecorated()) {
$latestLength += 2; $latestLength += 2;
}
if ($showAllTypes) {
if ('available' === $type) {
$io->write('<comment>' . $type . '</comment>:');
} else {
$io->write('<info>' . $type . '</info>:');
} }
}
foreach ($packages as $package) { if ($showAllTypes) {
$io->write($indent . str_pad($package['name'], $nameLength, ' '), false); if ('available' === $type) {
if (isset($package['version']) && $writeVersion) { $io->write('<comment>' . $type . '</comment>:');
$io->write(' ' . str_pad($package['version'], $versionLength, ' '), false); } else {
} $io->write('<info>' . $type . '</info>:');
if (isset($package['latest']) && $writeLatest) {
$latestVersion = $package['latest'];
$style = $package['latestStyle'];
if (!$io->isDecorated()) {
$latestVersion = str_replace(array('info', 'highlight', 'comment'), array('=', '!', '~'), $style) . ' ' . $latestVersion;
} }
$io->write(' <'.$style.'>' . str_pad($latestVersion, $latestLength, ' ') . '</'.$style.'>', false);
} }
if (isset($package['description']) && $writeDescription) {
$description = strtok($package['description'], "\r\n"); foreach ($packages as $package) {
$remaining = $width - $nameLength - $versionLength - 4; $io->write($indent . str_pad($package['name'], $nameLength, ' '), false);
if ($writeLatest) { if (isset($package['version']) && $writeVersion) {
$remaining -= $latestLength; $io->write(' ' . str_pad($package['version'], $versionLength, ' '), false);
} }
if (strlen($description) > $remaining) { if (isset($package['latest']) && $writeLatest) {
$description = substr($description, 0, $remaining - 3) . '...'; $latestVersion = $package['latest'];
$style = $package['latestStyle'];
if (!$io->isDecorated()) {
$latestVersion = str_replace(array('info', 'highlight', 'comment'), array('=', '!', '~'), $style) . ' ' . $latestVersion;
}
$io->write(' <' . $style . '>' . str_pad($latestVersion, $latestLength, ' ') . '</' . $style . '>', false);
} }
$io->write(' ' . $description, false); if (isset($package['description']) && $writeDescription) {
} $description = strtok($package['description'], "\r\n");
if (isset($package['path'])) { $remaining = $width - $nameLength - $versionLength - 4;
$io->write(' ' . $package['path'], false); if ($writeLatest) {
} $remaining -= $latestLength;
if (isset($package['warning'])) { }
$io->writeError(''); if (strlen($description) > $remaining) {
$io->writeError('<warning>' . $package['warning'] . '</warning>', false); $description = substr($description, 0, $remaining - 3) . '...';
}
$io->write(' ' . $description, false);
}
if (isset($package['path'])) {
$io->write(' ' . $package['path'], false);
}
if (isset($package['warning'])) {
$io->writeError('');
$io->writeError('<warning>' . $package['warning'] . '</warning>', false);
}
$io->write('');
} }
$io->write('');
}
if ($showAllTypes) { if ($showAllTypes) {
$io->write(''); $io->write('');
}
} }
} }

Loading…
Cancel
Save