Skip to content

Commit

Permalink
Merge pull request #5 from iamleeg/list-licenses
Browse files Browse the repository at this point in the history
Adds ability to show licenses used
  • Loading branch information
orediggerco committed Jun 22, 2018
2 parents 03c8fa9 + 1b64f21 commit 5cc2fe6
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 35 deletions.
4 changes: 2 additions & 2 deletions bin/php-legal-licenses
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (file_exists(__DIR__.'/../vendor/autoload.php')) {
require __DIR__.'/vendor/autoload.php';
}

$app = new Symfony\Component\Console\Application('PHP Legal Licenses', '1.0.0');
$app = new Symfony\Component\Console\Application('PHP Legal Licenses', '1.1.0');
$app->add(new Comcast\PhpLegalLicenses\Console\GenerateCommand);

$app->add(new Comcast\PhpLegalLicenses\Console\ShowCommand);
$app->run();
50 changes: 50 additions & 0 deletions src/Comcast/PhpLegalLicenses/Command/DependencyLicenseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Comcast\PhpLegalLicenses\Console;

use RuntimeException;
use Symfony\Component\Console\Command\Command;

class DependencyLicenseCommand extends Command
{
/**
* Generates a list of dependencies from a project's composer.lock file.
*
* @return array
*/
protected function getDependencyList()
{
$this->verifyComposerLockFilePresent();
$packages = $this->parseComposerLockFile();
$dependencies = $packages['packages'];

return $dependencies;
}

/**
* Verify that the composer.lock file exists.
*
* @return void
*/
protected function verifyComposerLockFilePresent()
{
if (is_file(getcwd().'/composer.lock')) {
return;
}

throw new RuntimeException('Composer Lock file missing! Please run composer install and try again.');
}

/**
* Parses the composer.lock file to retrieve all installed packages.
*
* @return array
*/
protected function parseComposerLockFile()
{
$path = getcwd().'/composer.lock';
$contents = file_get_contents($path);

return json_decode($contents, true);
}
}
36 changes: 3 additions & 33 deletions src/Comcast/PhpLegalLicenses/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Comcast\PhpLegalLicenses\Console;

use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateCommand extends Command
class GenerateCommand extends DependencyLicenseCommand
{
/**
* Configure the command options.
Expand All @@ -31,42 +29,14 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->verifyComposerLockFilePresent($output);
$packages = $this->parseComposerLockFile();
$dependencies = $this->getDependencyList();

$output->writeln('<info>Generating Licenses file...</info>');
$this->generateLicensesText($packages['packages']);
$this->generateLicensesText($dependencies);

$output->writeln('<info>Done!</info>');
}

/**
* Verify that the composer.lock file exists.
*
* @return void
*/
protected function verifyComposerLockFilePresent()
{
if (is_file(getcwd().'/composer.lock')) {
return;
}

throw new RuntimeException('Composer Lock file missing! Please run composer install and try again.');
}

/**
* Parses the composer.lock file to retrieve all installed packages.
*
* @return array
*/
protected function parseComposerLockFile()
{
$path = getcwd().'/composer.lock';
$contents = file_get_contents($path);

return json_decode($contents, true);
}

/**
* Generates Licenses Text using packages retrieved from composer.lock file.
*
Expand Down
80 changes: 80 additions & 0 deletions src/Comcast/PhpLegalLicenses/Command/ShowCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Comcast\PhpLegalLicenses\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ShowCommand extends DependencyLicenseCommand
{
/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('show')
->setDescription('Show licenses used by project dependencies.');
}

/**
* Execute the command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dependencies = $this->getDependencyList();
$this->outputDependencyLicenses($dependencies, $output);
}

/**
* Generates Licenses list using packages retrieved from composer.lock file.
*
* @param array $dependencies
*
* @return void
*/
protected function outputDependencyLicenses($dependencies, $output)
{
foreach ($dependencies as $dependency) {
$text = $this->getTextForDependency($dependency);
$output->writeln($text);
}
}

/**
* Retrieves text containing version and license information for the specified dependency.
*
* @param array $dependency
*
* @return string
*/
protected function getTextForDependency($dependency)
{
$name = $dependency['name'];
$version = $dependency['version'];
$licenseNames = isset($dependency['license']) ? implode(', ', $dependency['license']) : 'Not configured.';

return $this->generateDependencyText($name, $version, $licenseNames);
}

/**
* Generate formatted line detailing the version and license information for a particular dependency.
*
* @param string $name
* @param string $version
* @param string $licenceNames
*
* @return string
*/
protected function generateDependencyText($name, $version, $licenseNames)
{
return "$name@$version [$licenseNames]";
}
}

0 comments on commit 5cc2fe6

Please sign in to comment.