-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from iamleeg/list-licenses
Adds ability to show licenses used
- Loading branch information
Showing
4 changed files
with
135 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Comcast/PhpLegalLicenses/Command/DependencyLicenseCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"; | ||
} | ||
} |