diff --git a/src/Composer/Command/LicensesCommand.php b/src/Composer/Command/LicensesCommand.php new file mode 100644 index 000000000..28f584c66 --- /dev/null +++ b/src/Composer/Command/LicensesCommand.php @@ -0,0 +1,98 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Command; + +use Composer\Package\PackageInterface; +use Composer\Package\Version\VersionParser; +use Symfony\Component\Console\Helper\TableHelper; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @author BenoƮt Merlet + */ +class LicensesCommand extends Command +{ + protected function configure() + { + $this + ->setName('licenses') + ->setDescription('Show information about licenses of dependencies') + ->setDefinition(array( + new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'), + )) + ->setHelp(<<getComposer()->getPackage(); + $repo = $this->getComposer()->getRepositoryManager()->getLocalRepository(); + + $versionParser = new VersionParser; + + foreach ($repo->getPackages() as $package) { + $packages[$package->getName()] = $package; + } + + ksort($packages); + + switch ($format = $input->getOption('format')) { + case 'text': + $output->writeln('Name: '.$root->getPrettyName().''); + $output->writeln('Version: '.$versionParser->formatVersion($root).''); + $output->writeln('Licenses: '.(implode(', ', $root->getLicense()) ?: 'none').''); + $output->writeln('Dependencies:'); + + $table = $this->getHelperSet()->get('table'); + $table->setLayout(TableHelper::LAYOUT_BORDERLESS); + $table->setHorizontalBorderChar(''); + foreach ($packages as $package) { + $table->addRow(array( + $package->getPrettyName(), + $versionParser->formatVersion($package), + implode(', ', $package->getLicense()) ?: 'none', + )); + } + $table->render($output); + break; + + case 'json': + foreach ($packages as $package) { + $dependencies[$package->getPrettyName()] = array( + 'version' => $versionParser->formatVersion($package), + 'license' => $package->getLicense(), + ); + } + + $output->writeln(json_encode(array( + 'name' => $root->getPrettyName(), + 'version' => $versionParser->formatVersion($root), + 'license' => $root->getLicense(), + 'dependencies' => $dependencies, + ))); + break; + + default: + throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format)); + } + } +} diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index eaef4029d..f4021c958 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -224,6 +224,7 @@ class Application extends BaseApplication $commands[] = new Command\ArchiveCommand(); $commands[] = new Command\DiagnoseCommand(); $commands[] = new Command\RunScriptCommand(); + $commands[] = new Command\LicensesCommand(); if ('phar:' === substr(__FILE__, 0, 5)) { $commands[] = new Command\SelfUpdateCommand();