diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index baa8cbc8c..d6b909251 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -14,6 +14,7 @@ namespace Composer\Command; use Composer\DependencyResolver\Pool; use Composer\DependencyResolver\DefaultPolicy; +use Composer\Json\JsonFile; use Composer\Package\CompletePackageInterface; use Composer\Package\Version\VersionParser; 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('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'), )) ->setHelp(<<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 $platformOverrides = array(); if ($composer) { @@ -378,63 +387,67 @@ EOT } } - foreach ($viewData as $type => $packages) { - $nameLength = $viewMetaData[$type]['nameLength']; - $versionLength = $viewMetaData[$type]['versionLength']; - $latestLength = $viewMetaData[$type]['latestLength']; + if ('json' === $format) { + $io->write(JsonFile::encode($viewData)); + } else { + foreach ($viewData as $type => $packages) { + $nameLength = $viewMetaData[$type]['nameLength']; + $versionLength = $viewMetaData[$type]['versionLength']; + $latestLength = $viewMetaData[$type]['latestLength']; - $writeVersion = $nameLength + $versionLength + 3 <= $width; - $writeLatest = $nameLength + $versionLength + $latestLength + 3 <= $width; - $writeDescription = $nameLength + $versionLength + $latestLength + 24 <= $width; + $writeVersion = $nameLength + $versionLength + 3 <= $width; + $writeLatest = $nameLength + $versionLength + $latestLength + 3 <= $width; + $writeDescription = $nameLength + $versionLength + $latestLength + 24 <= $width; - if ($writeLatest && !$io->isDecorated()) { - $latestLength += 2; - } - - if ($showAllTypes) { - if ('available' === $type) { - $io->write('' . $type . ':'); - } else { - $io->write('' . $type . ':'); + if ($writeLatest && !$io->isDecorated()) { + $latestLength += 2; } - } - foreach ($packages as $package) { - $io->write($indent . str_pad($package['name'], $nameLength, ' '), false); - if (isset($package['version']) && $writeVersion) { - $io->write(' ' . str_pad($package['version'], $versionLength, ' '), false); - } - if (isset($package['latest']) && $writeLatest) { - $latestVersion = $package['latest']; - $style = $package['latestStyle']; - if (!$io->isDecorated()) { - $latestVersion = str_replace(array('info', 'highlight', 'comment'), array('=', '!', '~'), $style) . ' ' . $latestVersion; + if ($showAllTypes) { + if ('available' === $type) { + $io->write('' . $type . ':'); + } else { + $io->write('' . $type . ':'); } - $io->write(' <'.$style.'>' . str_pad($latestVersion, $latestLength, ' ') . '', false); } - if (isset($package['description']) && $writeDescription) { - $description = strtok($package['description'], "\r\n"); - $remaining = $width - $nameLength - $versionLength - 4; - if ($writeLatest) { - $remaining -= $latestLength; - } - if (strlen($description) > $remaining) { - $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('' . $package['warning'] . '', false); - } - $io->write(''); - } - if ($showAllTypes) { - $io->write(''); + foreach ($packages as $package) { + $io->write($indent . str_pad($package['name'], $nameLength, ' '), false); + if (isset($package['version']) && $writeVersion) { + $io->write(' ' . str_pad($package['version'], $versionLength, ' '), false); + } + 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, ' ') . '', false); + } + if (isset($package['description']) && $writeDescription) { + $description = strtok($package['description'], "\r\n"); + $remaining = $width - $nameLength - $versionLength - 4; + if ($writeLatest) { + $remaining -= $latestLength; + } + if (strlen($description) > $remaining) { + $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('' . $package['warning'] . '', false); + } + $io->write(''); + } + + if ($showAllTypes) { + $io->write(''); + } } }