Skip to content

Commit

Permalink
enable --csv option to output csv format
Browse files Browse the repository at this point in the history
  • Loading branch information
shooding authored and orediggerco committed Jan 12, 2021
1 parent f3eb2fc commit a9b4bc4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ You can also hide dependency version with `--hide-version` option:
$ php-legal-licenses generate --hide-version
```

You can output csv file with `--csv` option:

```
$ php-legal-licenses generate --csv
```

Or use both option:

```
$ php-legal-licenses generate --hide-version --csv
```

## Example Output
Here is a snippet of the licenses file that would be generated for this utility itself:
Expand Down
36 changes: 31 additions & 5 deletions src/Comcast/PhpLegalLicenses/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class GenerateCommand extends DependencyLicenseCommand
* @var bool
*/
private $hideVersion = false;
private $toCsv = false;

/**
* Configure the command options.
Expand All @@ -23,7 +24,8 @@ protected function configure()
$this
->setName('generate')
->setDescription('Generate Licenses file from project dependencies.')
->addOption('hide-version', 'hv', InputOption::VALUE_NONE, 'Hide dependency version');
->addOption('hide-version', 'hv', InputOption::VALUE_NONE, 'Hide dependency version')
->addOption('csv', null, InputOption::VALUE_NONE, 'Output csv format');
}

/**
Expand All @@ -37,11 +39,17 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->hideVersion = $input->getOption('hide-version');
$this->toCsv = $input->getOption('csv');
$dependencies = $this->getDependencyList();

$output->writeln('<info>Generating Licenses file...</info>');
$this->generateLicensesText($dependencies);
if ($this->toCsv) {

$this->generateLicensesCSV($dependencies);
} else {

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

return 0;
Expand All @@ -65,6 +73,24 @@ protected function generateLicensesText($dependencies)
file_put_contents('licenses.md', $text);
}

protected function generateLicensesCSV($dependencies)
{
$fp = fopen('licenses.csv', 'w');
$title = ['name', 'version', 'source', 'license description'];

fputcsv($fp, $title);
foreach ($dependencies as $dependency) {
$dependencyLists = [
$dependency['name'],
$this->hideVersion ? '' : $dependency['version'],
$dependency['source']['url'],
$this->getTextForDependency($dependency)
];
fputcsv($fp, $dependencyLists);
}
fclose($fp);
}

/**
* Returns Boilerplate text for the Licences File.
*
Expand Down Expand Up @@ -108,11 +134,11 @@ protected function getTextForDependency($dependency)
*/
protected function getFullLicenseText($name)
{
$path = getcwd()."/vendor/$name/";
$path = getcwd() . "/vendor/$name/";
$filenames = ['LICENSE.txt', 'LICENSE.md', 'LICENSE', 'license.txt', 'license.md', 'license', 'LICENSE-2.0.txt'];

foreach ($filenames as $filename) {
$text = @file_get_contents($path.$filename);
$text = @file_get_contents($path . $filename);
if ($text) {
return $text;
}
Expand All @@ -136,7 +162,7 @@ protected function getFullLicenseText($name)
*/
protected function generateDependencyText($name, $description, $version, $homepage, $sha, $licenseNames, $license)
{
return "### $name ".($this->hideVersion ? '' : "(Version $version | $sha)")."
return "### $name " . ($this->hideVersion ? '' : "(Version $version | $sha)") . "
$description
Homepage: $homepage
Licenses Used: $licenseNames
Expand Down

0 comments on commit a9b4bc4

Please sign in to comment.