Browse Source

proof of concept regarding licenses

Benoît Merlet 12 years ago
parent
commit
4aa4af73c5
2 changed files with 104 additions and 0 deletions
  1. 103 0
      src/Composer/Command/LicensesCommand.php
  2. 1 0
      src/Composer/Console/Application.php

+ 103 - 0
src/Composer/Command/LicensesCommand.php

@@ -0,0 +1,103 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * 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 <benoit.merlet@gmail.com>
+ */
+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(<<<EOT
+The license command displays detailed information about the licenses of
+the installed dependencies.
+
+EOT
+            )
+        ;
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $root = $this->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;
+        }
+    }
+}

+ 1 - 0
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();