From 4aa4af73c51f8b3dddd434c533aee47d7029f4a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Tue, 26 Mar 2013 12:14:41 +0100 Subject: [PATCH 1/5] proof of concept regarding licenses --- src/Composer/Command/LicensesCommand.php | 103 +++++++++++++++++++++++ src/Composer/Console/Application.php | 1 + 2 files changed, 104 insertions(+) create mode 100644 src/Composer/Command/LicensesCommand.php diff --git a/src/Composer/Command/LicensesCommand.php b/src/Composer/Command/LicensesCommand.php new file mode 100644 index 000000000..bfaa45db2 --- /dev/null +++ b/src/Composer/Command/LicensesCommand.php @@ -0,0 +1,103 @@ + + * 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\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: flat or json', 'flat'), + )) + ->setHelp(<<getComposer()->getPackage(); + $repo = $this->getComposer()->getRepositoryManager()->getLocalRepository(); + + $versionParser = new VersionParser; + + $nameLength = strlen($root->getPrettyName()); + $versionLength = strlen($versionParser->formatVersion($root)); + + foreach ($repo->getPackages() as $package) { + $packages[$package->getName()] = $package; + + $nameLength = max($nameLength, strlen($package->getPrettyName())); + $versionLength = max($versionLength, strlen($versionParser->formatVersion($package))); + } + + ksort($packages); + + switch ($format = $input->getOption('format')) { + case 'flat': + $formatRowCallback = function (PackageInterface $package) use ($versionParser, $nameLength, $versionLength) { + return sprintf( + ' %s %s %s', + str_pad($package->getPrettyName(), $nameLength, ' '), + str_pad($versionParser->formatVersion($package), $versionLength, ' '), + implode(', ', $package->getLicense()) ?: 'none' + ); + }; + + $output->writeln('Root Package:'); + $output->writeln($formatRowCallback($root)); + $output->writeln('Dependencies:'); + foreach ($packages as $package) { + $output->writeln($formatRowCallback($package)); + } + 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: + $output->writeln(sprintf('Unsupported format "%s". See help for supported formats.', $format)); + break; + } + } +} diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index eaef4029d..f4021c958 100755 --- 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(); From bf0b42efaad323ee04bcc339f651a0e7305a6f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Thu, 11 Jul 2013 17:59:57 +0200 Subject: [PATCH 2/5] renamed flat format to text --- src/Composer/Command/LicensesCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Composer/Command/LicensesCommand.php b/src/Composer/Command/LicensesCommand.php index bfaa45db2..735f24201 100644 --- a/src/Composer/Command/LicensesCommand.php +++ b/src/Composer/Command/LicensesCommand.php @@ -30,7 +30,7 @@ class LicensesCommand extends Command ->setName('licenses') ->setDescription('Show information about licenses of dependencies') ->setDefinition(array( - new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: flat or json', 'flat'), + new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'), )) ->setHelp(<<getOption('format')) { - case 'flat': + case 'text': $formatRowCallback = function (PackageInterface $package) use ($versionParser, $nameLength, $versionLength) { return sprintf( ' %s %s %s', From fbbd6f8999ca5b5b6e5ac5dceea4021fdbb003f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Fri, 12 Jul 2013 15:40:09 +0200 Subject: [PATCH 3/5] refactored alignment of text output --- src/Composer/Command/LicensesCommand.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Composer/Command/LicensesCommand.php b/src/Composer/Command/LicensesCommand.php index 735f24201..bd91641be 100644 --- a/src/Composer/Command/LicensesCommand.php +++ b/src/Composer/Command/LicensesCommand.php @@ -48,20 +48,22 @@ EOT $versionParser = new VersionParser; - $nameLength = strlen($root->getPrettyName()); - $versionLength = strlen($versionParser->formatVersion($root)); - foreach ($repo->getPackages() as $package) { $packages[$package->getName()] = $package; - - $nameLength = max($nameLength, strlen($package->getPrettyName())); - $versionLength = max($versionLength, strlen($versionParser->formatVersion($package))); } ksort($packages); switch ($format = $input->getOption('format')) { case 'text': + $nameLength = strlen($root->getPrettyName()); + $versionLength = strlen($versionParser->formatVersion($root)); + + foreach ($packages as $package) { + $nameLength = max($nameLength, strlen($package->getPrettyName())); + $versionLength = max($versionLength, strlen($versionParser->formatVersion($package))); + } + $formatRowCallback = function (PackageInterface $package) use ($versionParser, $nameLength, $versionLength) { return sprintf( ' %s %s %s', From 7591c064f59e2eeb72f220757a80006b220858cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Mon, 15 Jul 2013 14:54:47 +0200 Subject: [PATCH 4/5] used TableHelper for text output --- src/Composer/Command/LicensesCommand.php | 34 ++++++++++-------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/Composer/Command/LicensesCommand.php b/src/Composer/Command/LicensesCommand.php index bd91641be..73adb4451 100644 --- a/src/Composer/Command/LicensesCommand.php +++ b/src/Composer/Command/LicensesCommand.php @@ -14,6 +14,7 @@ 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; @@ -56,29 +57,22 @@ EOT switch ($format = $input->getOption('format')) { case 'text': - $nameLength = strlen($root->getPrettyName()); - $versionLength = strlen($versionParser->formatVersion($root)); - - foreach ($packages as $package) { - $nameLength = max($nameLength, strlen($package->getPrettyName())); - $versionLength = max($versionLength, strlen($versionParser->formatVersion($package))); - } - - $formatRowCallback = function (PackageInterface $package) use ($versionParser, $nameLength, $versionLength) { - return sprintf( - ' %s %s %s', - str_pad($package->getPrettyName(), $nameLength, ' '), - str_pad($versionParser->formatVersion($package), $versionLength, ' '), - implode(', ', $package->getLicense()) ?: 'none' - ); - }; - - $output->writeln('Root Package:'); - $output->writeln($formatRowCallback($root)); + $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) { - $output->writeln($formatRowCallback($package)); + $table->addRow(array( + $package->getPrettyName(), + $versionParser->formatVersion($package), + implode(', ', $package->getLicense()) ?: 'none', + )); } + $table->render($output); break; case 'json': From 4a3c419e74916a601eb64db783cae57b0804d752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Mon, 15 Jul 2013 15:24:47 +0200 Subject: [PATCH 5/5] thrown exception when using an unsupported format --- src/Composer/Command/LicensesCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Composer/Command/LicensesCommand.php b/src/Composer/Command/LicensesCommand.php index 73adb4451..28f584c66 100644 --- a/src/Composer/Command/LicensesCommand.php +++ b/src/Composer/Command/LicensesCommand.php @@ -92,8 +92,7 @@ EOT break; default: - $output->writeln(sprintf('Unsupported format "%s". See help for supported formats.', $format)); - break; + throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format)); } } }