Skip to content

Commit

Permalink
Add option to show license information (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
giggsey authored Jan 31, 2025
1 parent b9184cd commit b337dc8
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 174 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ composer diff --help # Display detailed usage instructions
- `--no-prod` - ignore prod dependencies (`require`)
- `--with-platform` (`-p`) - include platform dependencies (PHP, extensions, etc.)
- `--with-links` (`-l`) - include compare/release URLs
- `--with-licenses` (`-c`) - include license information
- `--format` (`-f`) - output format (mdtable, mdlist, json, github) - default: `mdtable`
- `--gitlab-domains` - custom gitlab domains for compare/release URLs - default: use composer config

Expand Down
4 changes: 3 additions & 1 deletion src/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function configure()
->addOption('no-prod', null, InputOption::VALUE_NONE, 'Ignore prod dependencies')
->addOption('with-platform', 'p', InputOption::VALUE_NONE, 'Include platform dependencies (PHP version, extensions, etc.)')
->addOption('with-links', 'l', InputOption::VALUE_NONE, 'Include compare/release URLs')
->addOption('with-licenses', 'c', InputOption::VALUE_NONE, 'Include licenses')
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format (mdtable, mdlist, json, github)', 'mdtable')
->addOption('gitlab-domains', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Extra Gitlab domains (inherited from Composer config by default)', array())
->addOption('strict', 's', InputOption::VALUE_NONE, 'Return non-zero exit code if there are any changes')
Expand Down Expand Up @@ -136,6 +137,7 @@ protected function handle(InputInterface $input, OutputInterface $output)
$target = null !== $input->getArgument('target') ? $input->getArgument('target') : $input->getOption('target');
$withPlatform = $input->getOption('with-platform');
$withUrls = $input->getOption('with-links');
$withLicenses = $input->getOption('with-licenses');
$this->gitlabDomains = array_merge($this->gitlabDomains, $input->getOption('gitlab-domains'));

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

$formatter->render($prodOperations, $devOperations, $withUrls);
$formatter->render($prodOperations, $devOperations, $withUrls, $withLicenses);

return $input->getOption('strict') ? $this->getExitCode($prodOperations, $devOperations) : 0;
}
Expand Down
19 changes: 19 additions & 0 deletions src/Formatter/AbstractFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Package\CompletePackageInterface;
use IonBazan\ComposerDiff\Diff\DiffEntry;
use IonBazan\ComposerDiff\Url\GeneratorContainer;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -46,6 +47,24 @@ public function getUrl(DiffEntry $entry)
return null;
}

/**
* @return string|null
*/
public function getLicenses(DiffEntry $entry)
{
if (!$entry->getPackage() instanceof CompletePackageInterface) {
return null;
}

$licenses = $entry->getPackage()->getLicense();

if (empty($licenses)) {
return null;
}

return implode(', ', $licenses);
}

/**
* @return string|null
*/
Expand Down
11 changes: 9 additions & 2 deletions src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ interface Formatter
{
/**
* @param bool $withUrls
* @param bool $withLicenses
*
* @return void
*/
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls);
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses);

/**
* @param string $title
* @param bool $withUrls
* @param bool $withLicenses
*
* @return void
*/
public function renderSingle(DiffEntries $entries, $title, $withUrls);
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses);

/**
* @return string|null
*/
public function getUrl(DiffEntry $entry);

/**
* @return string|null
*/
public function getLicenses(DiffEntry $entry);
}
35 changes: 21 additions & 14 deletions src/Formatter/GitHubFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,85 @@ class GitHubFormatter extends AbstractFormatter
/**
* {@inheritdoc}
*/
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
{
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls);
$this->renderSingle($devEntries, 'Dev Packages', $withUrls);
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls, $withLicenses);
$this->renderSingle($devEntries, 'Dev Packages', $withUrls, $withLicenses);
}

/**
* {@inheritdoc}
*/
public function renderSingle(DiffEntries $entries, $title, $withUrls)
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
{
if (!\count($entries)) {
return;
}

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

/**
* @param bool $withUrls
* @param bool $withLicenses
*
* @return string[]
*/
private function transformEntries(DiffEntries $entries, $withUrls)
private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses)
{
$rows = array();

foreach ($entries as $entry) {
$rows[] = $this->transformEntry($entry, $withUrls);
$rows[] = $this->transformEntry($entry, $withUrls, $withLicenses);
}

return $rows;
}

/**
* @param bool $withUrls
* @param bool $withLicenses
*
* @return string
*/
private function transformEntry(DiffEntry $entry, $withUrls)
private function transformEntry(DiffEntry $entry, $withUrls, $withLicenses)
{
$operation = $entry->getOperation();
$url = $withUrls ? $this->getUrl($entry) : null;
$url = (null !== $url) ? ' '.$url : '';
$licenses = $withLicenses ? $this->getLicenses($entry) : null;
$licenses = (null !== $licenses) ? ' (License: '.$licenses.')' : '';

if ($operation instanceof InstallOperation) {
return sprintf(
' - Install %s (%s)%s',
' - Install %s (%s)%s%s',
$operation->getPackage()->getName(),
$operation->getPackage()->getFullPrettyVersion(),
$url
$url,
$licenses
);
}

if ($operation instanceof UpdateOperation) {
return sprintf(
' - %s %s (%s => %s)%s',
' - %s %s (%s => %s)%s%s',
ucfirst($entry->getType()),
$operation->getInitialPackage()->getName(),
$operation->getInitialPackage()->getFullPrettyVersion(),
$operation->getTargetPackage()->getFullPrettyVersion(),
$url
$url,
$licenses
);
}

if ($operation instanceof UninstallOperation) {
return sprintf(
' - Uninstall %s (%s)%s',
' - Uninstall %s (%s)%s%s',
$operation->getPackage()->getName(),
$operation->getPackage()->getFullPrettyVersion(),
$url
$url,
$licenses
);
}

Expand Down
17 changes: 11 additions & 6 deletions src/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ class JsonFormatter extends AbstractFormatter
/**
* {@inheritdoc}
*/
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
{
$this->format(array(
'packages' => $this->transformEntries($prodEntries, $withUrls),
'packages-dev' => $this->transformEntries($devEntries, $withUrls),
'packages' => $this->transformEntries($prodEntries, $withUrls, $withLicenses),
'packages-dev' => $this->transformEntries($devEntries, $withUrls, $withLicenses),
));
}

/**
* {@inheritdoc}
*/
public function renderSingle(DiffEntries $entries, $title, $withUrls)
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
{
$this->format($this->transformEntries($entries, $withUrls));
$this->format($this->transformEntries($entries, $withUrls, $withLicenses));
}

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

/**
* @param bool $withUrls
* @param bool $withLicenses
*
* @return array<array<string, string|null>>
*/
private function transformEntries(DiffEntries $entries, $withUrls)
private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses)
{
$rows = array();

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

if ($withLicenses) {
$row['license'] = $this->getLicenses($entry);
}

$rows[$row['name']] = $row;
}

Expand Down
32 changes: 19 additions & 13 deletions src/Formatter/MarkdownListFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class MarkdownListFormatter extends MarkdownFormatter
/**
* {@inheritdoc}
*/
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
{
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls);
$this->renderSingle($devEntries, 'Dev Packages', $withUrls);
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls, $withLicenses);
$this->renderSingle($devEntries, 'Dev Packages', $withUrls, $withLicenses);
}

/**
* {@inheritdoc}
*/
public function renderSingle(DiffEntries $entries, $title, $withUrls)
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
{
if (!\count($entries)) {
return;
Expand All @@ -33,32 +33,36 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
$this->output->writeln('');

foreach ($entries as $entry) {
$this->output->writeln($this->getRow($entry, $withUrls));
$this->output->writeln($this->getRow($entry, $withUrls, $withLicenses));
}

$this->output->writeln('');
}

/**
* @param bool $withUrls
* @param bool $withLicenses
*
* @return string
*/
private function getRow(DiffEntry $entry, $withUrls)
private function getRow(DiffEntry $entry, $withUrls, $withLicenses)
{
$url = $withUrls ? $this->formatUrl($this->getUrl($entry), 'Compare') : null;
$url = (null !== $url) ? ' '.$url : '';
$url = (null !== $url && '' !== $url) ? ' '.$url : '';
$licenses = $withLicenses ? $this->getLicenses($entry) : null;
$licenses = (null !== $licenses) ? ' (License: '.$licenses.')' : '';
$operation = $entry->getOperation();

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

return sprintf(
' - Install <fg=green>%s</> (<fg=yellow>%s</>)%s',
' - Install <fg=green>%s</> (<fg=yellow>%s</>)%s%s',
$packageUrl ?: $packageName,
$operation->getPackage()->getFullPrettyVersion(),
$url
$url,
$licenses
);
}

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

return sprintf(
' - %s <fg=green>%s</> (<fg=yellow>%s</> => <fg=yellow>%s</>)%s',
' - %s <fg=green>%s</> (<fg=yellow>%s</> => <fg=yellow>%s</>)%s%s',
ucfirst($entry->getType()),
$projectUrl ?: $packageName,
$operation->getInitialPackage()->getFullPrettyVersion(),
$operation->getTargetPackage()->getFullPrettyVersion(),
$url
$url,
$licenses
);
}

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

return sprintf(
' - Uninstall <fg=green>%s</> (<fg=yellow>%s</>)%s',
' - Uninstall <fg=green>%s</> (<fg=yellow>%s</>)%s%s',
$packageUrl ?: $packageName,
$operation->getPackage()->getFullPrettyVersion(),
$url
$url,
$licenses
);
}

Expand Down
16 changes: 12 additions & 4 deletions src/Formatter/MarkdownTableFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ class MarkdownTableFormatter extends MarkdownFormatter
/**
* {@inheritdoc}
*/
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls, $withLicenses)
{
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls);
$this->renderSingle($devEntries, 'Dev Packages', $withUrls);
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls, $withLicenses);
$this->renderSingle($devEntries, 'Dev Packages', $withUrls, $withLicenses);
}

/**
* {@inheritdoc}
*/
public function renderSingle(DiffEntries $entries, $title, $withUrls)
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses)
{
if (!\count($entries)) {
return;
Expand All @@ -38,6 +38,10 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
$row[] = $this->formatUrl($this->getUrl($entry), 'Compare');
}

if ($withLicenses) {
$row[] = $this->getLicenses($entry);
}

$rows[] = $row;
}

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

if ($withLicenses) {
$headers[] = 'License';
}

$table->setHeaders($headers)->setRows($rows)->render();
$this->output->writeln('');
}
Expand Down
Loading

0 comments on commit b337dc8

Please sign in to comment.