Skip to content

Commit b337dc8

Browse files
authored
Add option to show license information (#38)
1 parent b9184cd commit b337dc8

14 files changed

+302
-174
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ composer diff --help # Display detailed usage instructions
7171
- `--no-prod` - ignore prod dependencies (`require`)
7272
- `--with-platform` (`-p`) - include platform dependencies (PHP, extensions, etc.)
7373
- `--with-links` (`-l`) - include compare/release URLs
74+
- `--with-licenses` (`-c`) - include license information
7475
- `--format` (`-f`) - output format (mdtable, mdlist, json, github) - default: `mdtable`
7576
- `--gitlab-domains` - custom gitlab domains for compare/release URLs - default: use composer config
7677

src/Command/DiffCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function configure()
6565
->addOption('no-prod', null, InputOption::VALUE_NONE, 'Ignore prod dependencies')
6666
->addOption('with-platform', 'p', InputOption::VALUE_NONE, 'Include platform dependencies (PHP version, extensions, etc.)')
6767
->addOption('with-links', 'l', InputOption::VALUE_NONE, 'Include compare/release URLs')
68+
->addOption('with-licenses', 'c', InputOption::VALUE_NONE, 'Include licenses')
6869
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format (mdtable, mdlist, json, github)', 'mdtable')
6970
->addOption('gitlab-domains', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Extra Gitlab domains (inherited from Composer config by default)', array())
7071
->addOption('strict', 's', InputOption::VALUE_NONE, 'Return non-zero exit code if there are any changes')
@@ -136,6 +137,7 @@ protected function handle(InputInterface $input, OutputInterface $output)
136137
$target = null !== $input->getArgument('target') ? $input->getArgument('target') : $input->getOption('target');
137138
$withPlatform = $input->getOption('with-platform');
138139
$withUrls = $input->getOption('with-links');
140+
$withLicenses = $input->getOption('with-licenses');
139141
$this->gitlabDomains = array_merge($this->gitlabDomains, $input->getOption('gitlab-domains'));
140142

141143
$urlGenerators = new GeneratorContainer($this->gitlabDomains);
@@ -153,7 +155,7 @@ protected function handle(InputInterface $input, OutputInterface $output)
153155
$devOperations = $this->packageDiff->getPackageDiff($base, $target, true, $withPlatform);
154156
}
155157

156-
$formatter->render($prodOperations, $devOperations, $withUrls);
158+
$formatter->render($prodOperations, $devOperations, $withUrls, $withLicenses);
157159

158160
return $input->getOption('strict') ? $this->getExitCode($prodOperations, $devOperations) : 0;
159161
}

src/Formatter/AbstractFormatter.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Composer\DependencyResolver\Operation\OperationInterface;
77
use Composer\DependencyResolver\Operation\UninstallOperation;
88
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use Composer\Package\CompletePackageInterface;
910
use IonBazan\ComposerDiff\Diff\DiffEntry;
1011
use IonBazan\ComposerDiff\Url\GeneratorContainer;
1112
use Symfony\Component\Console\Output\OutputInterface;
@@ -46,6 +47,24 @@ public function getUrl(DiffEntry $entry)
4647
return null;
4748
}
4849

50+
/**
51+
* @return string|null
52+
*/
53+
public function getLicenses(DiffEntry $entry)
54+
{
55+
if (!$entry->getPackage() instanceof CompletePackageInterface) {
56+
return null;
57+
}
58+
59+
$licenses = $entry->getPackage()->getLicense();
60+
61+
if (empty($licenses)) {
62+
return null;
63+
}
64+
65+
return implode(', ', $licenses);
66+
}
67+
4968
/**
5069
* @return string|null
5170
*/

src/Formatter/Formatter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@ interface Formatter
99
{
1010
/**
1111
* @param bool $withUrls
12+
* @param bool $withLicenses
1213
*
1314
* @return void
1415
*/
15-
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls);
16+
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses);
1617

1718
/**
1819
* @param string $title
1920
* @param bool $withUrls
21+
* @param bool $withLicenses
2022
*
2123
* @return void
2224
*/
23-
public function renderSingle(DiffEntries $entries, $title, $withUrls);
25+
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses);
2426

2527
/**
2628
* @return string|null
2729
*/
2830
public function getUrl(DiffEntry $entry);
31+
32+
/**
33+
* @return string|null
34+
*/
35+
public function getLicenses(DiffEntry $entry);
2936
}

src/Formatter/GitHubFormatter.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,78 +13,85 @@ class GitHubFormatter extends AbstractFormatter
1313
/**
1414
* {@inheritdoc}
1515
*/
16-
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
16+
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
1717
{
18-
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls);
19-
$this->renderSingle($devEntries, 'Dev Packages', $withUrls);
18+
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls, $withLicenses);
19+
$this->renderSingle($devEntries, 'Dev Packages', $withUrls, $withLicenses);
2020
}
2121

2222
/**
2323
* {@inheritdoc}
2424
*/
25-
public function renderSingle(DiffEntries $entries, $title, $withUrls)
25+
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
2626
{
2727
if (!\count($entries)) {
2828
return;
2929
}
3030

31-
$message = str_replace("\n", '%0A', implode("\n", $this->transformEntries($entries, $withUrls)));
31+
$message = str_replace("\n", '%0A', implode("\n", $this->transformEntries($entries, $withUrls, $withLicenses)));
3232
$this->output->writeln(sprintf('::notice title=%s::%s', $title, $message));
3333
}
3434

3535
/**
3636
* @param bool $withUrls
37+
* @param bool $withLicenses
3738
*
3839
* @return string[]
3940
*/
40-
private function transformEntries(DiffEntries $entries, $withUrls)
41+
private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses)
4142
{
4243
$rows = array();
4344

4445
foreach ($entries as $entry) {
45-
$rows[] = $this->transformEntry($entry, $withUrls);
46+
$rows[] = $this->transformEntry($entry, $withUrls, $withLicenses);
4647
}
4748

4849
return $rows;
4950
}
5051

5152
/**
5253
* @param bool $withUrls
54+
* @param bool $withLicenses
5355
*
5456
* @return string
5557
*/
56-
private function transformEntry(DiffEntry $entry, $withUrls)
58+
private function transformEntry(DiffEntry $entry, $withUrls, $withLicenses)
5759
{
5860
$operation = $entry->getOperation();
5961
$url = $withUrls ? $this->getUrl($entry) : null;
6062
$url = (null !== $url) ? ' '.$url : '';
63+
$licenses = $withLicenses ? $this->getLicenses($entry) : null;
64+
$licenses = (null !== $licenses) ? ' (License: '.$licenses.')' : '';
6165

6266
if ($operation instanceof InstallOperation) {
6367
return sprintf(
64-
' - Install %s (%s)%s',
68+
' - Install %s (%s)%s%s',
6569
$operation->getPackage()->getName(),
6670
$operation->getPackage()->getFullPrettyVersion(),
67-
$url
71+
$url,
72+
$licenses
6873
);
6974
}
7075

7176
if ($operation instanceof UpdateOperation) {
7277
return sprintf(
73-
' - %s %s (%s => %s)%s',
78+
' - %s %s (%s => %s)%s%s',
7479
ucfirst($entry->getType()),
7580
$operation->getInitialPackage()->getName(),
7681
$operation->getInitialPackage()->getFullPrettyVersion(),
7782
$operation->getTargetPackage()->getFullPrettyVersion(),
78-
$url
83+
$url,
84+
$licenses
7985
);
8086
}
8187

8288
if ($operation instanceof UninstallOperation) {
8389
return sprintf(
84-
' - Uninstall %s (%s)%s',
90+
' - Uninstall %s (%s)%s%s',
8591
$operation->getPackage()->getName(),
8692
$operation->getPackage()->getFullPrettyVersion(),
87-
$url
93+
$url,
94+
$licenses
8895
);
8996
}
9097

src/Formatter/JsonFormatter.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ class JsonFormatter extends AbstractFormatter
1313
/**
1414
* {@inheritdoc}
1515
*/
16-
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
16+
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
1717
{
1818
$this->format(array(
19-
'packages' => $this->transformEntries($prodEntries, $withUrls),
20-
'packages-dev' => $this->transformEntries($devEntries, $withUrls),
19+
'packages' => $this->transformEntries($prodEntries, $withUrls, $withLicenses),
20+
'packages-dev' => $this->transformEntries($devEntries, $withUrls, $withLicenses),
2121
));
2222
}
2323

2424
/**
2525
* {@inheritdoc}
2626
*/
27-
public function renderSingle(DiffEntries $entries, $title, $withUrls)
27+
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
2828
{
29-
$this->format($this->transformEntries($entries, $withUrls));
29+
$this->format($this->transformEntries($entries, $withUrls, $withLicenses));
3030
}
3131

3232
/**
@@ -42,10 +42,11 @@ private function format(array $data)
4242

4343
/**
4444
* @param bool $withUrls
45+
* @param bool $withLicenses
4546
*
4647
* @return array<array<string, string|null>>
4748
*/
48-
private function transformEntries(DiffEntries $entries, $withUrls)
49+
private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses)
4950
{
5051
$rows = array();
5152

@@ -57,6 +58,10 @@ private function transformEntries(DiffEntries $entries, $withUrls)
5758
$row['link'] = $this->getProjectUrl($entry->getOperation());
5859
}
5960

61+
if ($withLicenses) {
62+
$row['license'] = $this->getLicenses($entry);
63+
}
64+
6065
$rows[$row['name']] = $row;
6166
}
6267

src/Formatter/MarkdownListFormatter.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ class MarkdownListFormatter extends MarkdownFormatter
1313
/**
1414
* {@inheritdoc}
1515
*/
16-
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
16+
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
1717
{
18-
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls);
19-
$this->renderSingle($devEntries, 'Dev Packages', $withUrls);
18+
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls, $withLicenses);
19+
$this->renderSingle($devEntries, 'Dev Packages', $withUrls, $withLicenses);
2020
}
2121

2222
/**
2323
* {@inheritdoc}
2424
*/
25-
public function renderSingle(DiffEntries $entries, $title, $withUrls)
25+
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
2626
{
2727
if (!\count($entries)) {
2828
return;
@@ -33,32 +33,36 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
3333
$this->output->writeln('');
3434

3535
foreach ($entries as $entry) {
36-
$this->output->writeln($this->getRow($entry, $withUrls));
36+
$this->output->writeln($this->getRow($entry, $withUrls, $withLicenses));
3737
}
3838

3939
$this->output->writeln('');
4040
}
4141

4242
/**
4343
* @param bool $withUrls
44+
* @param bool $withLicenses
4445
*
4546
* @return string
4647
*/
47-
private function getRow(DiffEntry $entry, $withUrls)
48+
private function getRow(DiffEntry $entry, $withUrls, $withLicenses)
4849
{
4950
$url = $withUrls ? $this->formatUrl($this->getUrl($entry), 'Compare') : null;
50-
$url = (null !== $url) ? ' '.$url : '';
51+
$url = (null !== $url && '' !== $url) ? ' '.$url : '';
52+
$licenses = $withLicenses ? $this->getLicenses($entry) : null;
53+
$licenses = (null !== $licenses) ? ' (License: '.$licenses.')' : '';
5154
$operation = $entry->getOperation();
5255

5356
if ($operation instanceof InstallOperation) {
5457
$packageName = $operation->getPackage()->getName();
5558
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
5659

5760
return sprintf(
58-
' - Install <fg=green>%s</> (<fg=yellow>%s</>)%s',
61+
' - Install <fg=green>%s</> (<fg=yellow>%s</>)%s%s',
5962
$packageUrl ?: $packageName,
6063
$operation->getPackage()->getFullPrettyVersion(),
61-
$url
64+
$url,
65+
$licenses
6266
);
6367
}
6468

@@ -67,12 +71,13 @@ private function getRow(DiffEntry $entry, $withUrls)
6771
$projectUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
6872

6973
return sprintf(
70-
' - %s <fg=green>%s</> (<fg=yellow>%s</> => <fg=yellow>%s</>)%s',
74+
' - %s <fg=green>%s</> (<fg=yellow>%s</> => <fg=yellow>%s</>)%s%s',
7175
ucfirst($entry->getType()),
7276
$projectUrl ?: $packageName,
7377
$operation->getInitialPackage()->getFullPrettyVersion(),
7478
$operation->getTargetPackage()->getFullPrettyVersion(),
75-
$url
79+
$url,
80+
$licenses
7681
);
7782
}
7883

@@ -81,10 +86,11 @@ private function getRow(DiffEntry $entry, $withUrls)
8186
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
8287

8388
return sprintf(
84-
' - Uninstall <fg=green>%s</> (<fg=yellow>%s</>)%s',
89+
' - Uninstall <fg=green>%s</> (<fg=yellow>%s</>)%s%s',
8590
$packageUrl ?: $packageName,
8691
$operation->getPackage()->getFullPrettyVersion(),
87-
$url
92+
$url,
93+
$licenses
8894
);
8995
}
9096

src/Formatter/MarkdownTableFormatter.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ class MarkdownTableFormatter extends MarkdownFormatter
1414
/**
1515
* {@inheritdoc}
1616
*/
17-
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
17+
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
1818
{
19-
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls);
20-
$this->renderSingle($devEntries, 'Dev Packages', $withUrls);
19+
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls, $withLicenses);
20+
$this->renderSingle($devEntries, 'Dev Packages', $withUrls, $withLicenses);
2121
}
2222

2323
/**
2424
* {@inheritdoc}
2525
*/
26-
public function renderSingle(DiffEntries $entries, $title, $withUrls)
26+
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
2727
{
2828
if (!\count($entries)) {
2929
return;
@@ -38,6 +38,10 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
3838
$row[] = $this->formatUrl($this->getUrl($entry), 'Compare');
3939
}
4040

41+
if ($withLicenses) {
42+
$row[] = $this->getLicenses($entry);
43+
}
44+
4145
$rows[] = $row;
4246
}
4347

@@ -48,6 +52,10 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
4852
$headers[] = 'Link';
4953
}
5054

55+
if ($withLicenses) {
56+
$headers[] = 'License';
57+
}
58+
5159
$table->setHeaders($headers)->setRows($rows)->render();
5260
$this->output->writeln('');
5361
}

0 commit comments

Comments
 (0)