Skip to content

Commit a9b4bc4

Browse files
shoodingorediggerco
authored andcommitted
enable --csv option to output csv format
1 parent f3eb2fc commit a9b4bc4

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ You can also hide dependency version with `--hide-version` option:
3838
$ php-legal-licenses generate --hide-version
3939
```
4040

41+
You can output csv file with `--csv` option:
4142

43+
```
44+
$ php-legal-licenses generate --csv
45+
```
46+
47+
Or use both option:
48+
49+
```
50+
$ php-legal-licenses generate --hide-version --csv
51+
```
4252

4353
## Example Output
4454
Here is a snippet of the licenses file that would be generated for this utility itself:

src/Comcast/PhpLegalLicenses/Command/GenerateCommand.php

+31-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class GenerateCommand extends DependencyLicenseCommand
1212
* @var bool
1313
*/
1414
private $hideVersion = false;
15+
private $toCsv = false;
1516

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

2931
/**
@@ -37,11 +39,17 @@ protected function configure()
3739
protected function execute(InputInterface $input, OutputInterface $output)
3840
{
3941
$this->hideVersion = $input->getOption('hide-version');
42+
$this->toCsv = $input->getOption('csv');
4043
$dependencies = $this->getDependencyList();
4144

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

48+
$this->generateLicensesCSV($dependencies);
49+
} else {
50+
51+
$this->generateLicensesText($dependencies);
52+
}
4553
$output->writeln('<info>Done!</info>');
4654

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

76+
protected function generateLicensesCSV($dependencies)
77+
{
78+
$fp = fopen('licenses.csv', 'w');
79+
$title = ['name', 'version', 'source', 'license description'];
80+
81+
fputcsv($fp, $title);
82+
foreach ($dependencies as $dependency) {
83+
$dependencyLists = [
84+
$dependency['name'],
85+
$this->hideVersion ? '' : $dependency['version'],
86+
$dependency['source']['url'],
87+
$this->getTextForDependency($dependency)
88+
];
89+
fputcsv($fp, $dependencyLists);
90+
}
91+
fclose($fp);
92+
}
93+
6894
/**
6995
* Returns Boilerplate text for the Licences File.
7096
*
@@ -108,11 +134,11 @@ protected function getTextForDependency($dependency)
108134
*/
109135
protected function getFullLicenseText($name)
110136
{
111-
$path = getcwd()."/vendor/$name/";
137+
$path = getcwd() . "/vendor/$name/";
112138
$filenames = ['LICENSE.txt', 'LICENSE.md', 'LICENSE', 'license.txt', 'license.md', 'license', 'LICENSE-2.0.txt'];
113139

114140
foreach ($filenames as $filename) {
115-
$text = @file_get_contents($path.$filename);
141+
$text = @file_get_contents($path . $filename);
116142
if ($text) {
117143
return $text;
118144
}
@@ -136,7 +162,7 @@ protected function getFullLicenseText($name)
136162
*/
137163
protected function generateDependencyText($name, $description, $version, $homepage, $sha, $licenseNames, $license)
138164
{
139-
return "### $name ".($this->hideVersion ? '' : "(Version $version | $sha)")."
165+
return "### $name " . ($this->hideVersion ? '' : "(Version $version | $sha)") . "
140166
$description
141167
Homepage: $homepage
142168
Licenses Used: $licenseNames

0 commit comments

Comments
 (0)