Skip to content

Commit 927d212

Browse files
committed
add url for comparison
1 parent ebecd6c commit 927d212

27 files changed

+1071
-79
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ jobs:
5656
run: |
5757
composer req infection/infection
5858
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4
59+
- name: Run phpstan
60+
if: ${{ matrix.php-versions >= 7.1 && matrix.php-versions < 8.0 }}
61+
run: |
62+
composer req phpstan/phpstan
63+
vendor/bin/phpstan analyse src -l 6
5964

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ composer diff # Displays packages changed in current git tree compared with HEAD
6565
- `--no-dev` - ignore dev dependencies (`require-dev`)
6666
- `--no-prod` - ignore prod dependencies (`require`)
6767
- `--with-platform` (`-p`) - include platform dependencies (PHP, extensions, etc.)
68+
- `--with-links` (`-l`) - include compare/release URLs
6869
- `--format` (`-f`) - output format (mdtable, mdlist) - default: `mdtable`
70+
- `--gitlab-domains` - custom gitlab domains for compare/release URLs - default: use composer config
6971

7072
## Advanced usage
7173

src/Command/DiffCommand.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use IonBazan\ComposerDiff\Formatter\MarkdownListFormatter;
88
use IonBazan\ComposerDiff\Formatter\MarkdownTableFormatter;
99
use IonBazan\ComposerDiff\PackageDiff;
10+
use IonBazan\ComposerDiff\Url\GeneratorContainer;
1011
use Symfony\Component\Console\Input\InputInterface;
1112
use Symfony\Component\Console\Input\InputOption;
1213
use Symfony\Component\Console\Output\OutputInterface;
@@ -18,13 +19,25 @@ class DiffCommand extends BaseCommand
1819
*/
1920
protected $packageDiff;
2021

21-
public function __construct(PackageDiff $packageDiff)
22+
/**
23+
* @var string[]
24+
*/
25+
protected $gitlabDomains;
26+
27+
/**
28+
* @param string[] $gitlabDomains
29+
*/
30+
public function __construct(PackageDiff $packageDiff, array $gitlabDomains = array())
2231
{
2332
$this->packageDiff = $packageDiff;
33+
$this->gitlabDomains = $gitlabDomains;
2434

2535
parent::__construct();
2636
}
2737

38+
/**
39+
* @return void
40+
*/
2841
protected function configure()
2942
{
3043
$this->setName('diff')
@@ -34,25 +47,33 @@ protected function configure()
3447
->addOption('no-dev', null, InputOption::VALUE_NONE, 'Ignore dev dependencies')
3548
->addOption('no-prod', null, InputOption::VALUE_NONE, 'Ignore prod dependencies')
3649
->addOption('with-platform', 'p', InputOption::VALUE_NONE, 'Include platform dependencies (PHP version, extensions, etc.)')
50+
->addOption('with-links', 'l', InputOption::VALUE_NONE, 'Include compare/release URLs')
3751
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format (mdtable, mdlist)', 'mdtable')
52+
->addOption('gitlab-domains', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Gitlab domains', array())
3853
;
3954
}
4055

56+
/**
57+
* {@inheritdoc}
58+
*/
4159
protected function execute(InputInterface $input, OutputInterface $output)
4260
{
4361
$base = $input->getOption('base');
4462
$target = $input->getOption('target');
4563
$withPlatform = $input->getOption('with-platform');
64+
$withUrls = $input->getOption('with-links');
65+
$this->gitlabDomains = array_merge($this->gitlabDomains, $input->getOption('gitlab-domains'));
66+
4667
$formatter = $this->getFormatter($input, $output);
4768

4869
if (!$input->getOption('no-prod')) {
4970
$operations = $this->packageDiff->getPackageDiff($base, $target, false, $withPlatform);
50-
$formatter->render($operations, 'Prod Packages', $output);
71+
$formatter->render($operations, 'Prod Packages', $withUrls);
5172
}
5273

5374
if (!$input->getOption('no-dev')) {
5475
$operations = $this->packageDiff->getPackageDiff($base, $target, true, $withPlatform);
55-
$formatter->render($operations, 'Dev Packages', $output);
76+
$formatter->render($operations, 'Dev Packages', $withUrls);
5677
}
5778

5879
return 0;
@@ -63,12 +84,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
6384
*/
6485
private function getFormatter(InputInterface $input, OutputInterface $output)
6586
{
87+
$urlGenerators = new GeneratorContainer($this->gitlabDomains);
88+
6689
switch ($input->getOption('format')) {
6790
case 'mdlist':
68-
return new MarkdownListFormatter($output);
91+
return new MarkdownListFormatter($output, $urlGenerators);
6992
case 'mdtable':
7093
default:
71-
return new MarkdownTableFormatter($output);
94+
return new MarkdownTableFormatter($output, $urlGenerators);
7295
}
7396
}
7497
}

src/Formatter/AbstractFormatter.php

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,94 @@
22

33
namespace IonBazan\ComposerDiff\Formatter;
44

5+
use Composer\DependencyResolver\Operation\InstallOperation;
6+
use Composer\DependencyResolver\Operation\OperationInterface;
7+
use Composer\DependencyResolver\Operation\UninstallOperation;
58
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use Composer\Package\PackageInterface;
610
use Composer\Semver\Semver;
711
use Composer\Semver\VersionParser;
12+
use IonBazan\ComposerDiff\Url\GeneratorContainer;
13+
use Symfony\Component\Console\Output\OutputInterface;
814

915
abstract class AbstractFormatter implements Formatter
1016
{
17+
/**
18+
* @var OutputInterface
19+
*/
20+
protected $output;
21+
22+
/**
23+
* @var GeneratorContainer
24+
*/
25+
protected $generators;
26+
27+
public function __construct(OutputInterface $output, GeneratorContainer $generators)
28+
{
29+
$this->output = $output;
30+
$this->generators = $generators;
31+
}
32+
33+
/**
34+
* @return string|null
35+
*/
36+
public function getUrl(OperationInterface $operation)
37+
{
38+
if ($operation instanceof UpdateOperation) {
39+
return $this->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
40+
}
41+
42+
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
43+
return $this->getReleaseUrl($operation->getPackage());
44+
}
45+
46+
return null;
47+
}
48+
49+
/**
50+
* @return string|null
51+
*/
52+
private function getCompareUrl(PackageInterface $basePackage, PackageInterface $targetPackage)
53+
{
54+
$generator = $this->generators->get($targetPackage);
55+
56+
if (!$generator) {
57+
return null;
58+
}
59+
60+
return $generator->getCompareUrl($basePackage, $targetPackage);
61+
}
62+
63+
/**
64+
* @return string|null
65+
*/
66+
private function getReleaseUrl(PackageInterface $package)
67+
{
68+
$generator = $this->generators->get($package);
69+
70+
if (!$generator) {
71+
return null;
72+
}
73+
74+
return $generator->getReleaseUrl($package);
75+
}
76+
77+
/**
78+
* @param string $url
79+
* @param string $title
80+
*
81+
* @return string
82+
*/
83+
abstract protected function formatUrl($url, $title);
84+
85+
/**
86+
* @return bool
87+
*/
1188
protected static function isUpgrade(UpdateOperation $operation)
1289
{
1390
$versionParser = new VersionParser();
14-
$normalizedFrom = $versionParser->normalize($operation->getInitialPackage()->getFullPrettyVersion());
15-
$normalizedTo = $versionParser->normalize($operation->getTargetPackage()->getFullPrettyVersion());
91+
$normalizedFrom = $versionParser->normalize($operation->getInitialPackage()->getVersion());
92+
$normalizedTo = $versionParser->normalize($operation->getTargetPackage()->getVersion());
1693

1794
$sorted = Semver::sort(array($normalizedTo, $normalizedFrom));
1895

src/Formatter/Formatter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ interface Formatter
99
/**
1010
* @param OperationInterface[] $operations
1111
* @param string $title
12+
* @param bool $withUrls
1213
*
1314
* @return void
1415
*/
15-
public function render(array $operations, $title);
16+
public function render(array $operations, $title, $withUrls);
17+
18+
/**
19+
* @return string|null
20+
*/
21+
public function getUrl(OperationInterface $operation);
1622
}

src/Formatter/Helper/MarkdownTable.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ class MarkdownTable
1111
*/
1212
protected $output;
1313

14+
/**
15+
* @var string[]
16+
*/
1417
protected $headers = array();
1518

19+
/**
20+
* @var string[][]
21+
*/
1622
protected $rows = array();
1723

1824
/**
1925
* Column widths cache.
2026
*
21-
* @var array
27+
* @var int[]
2228
*/
2329
private $columnWidths = array();
2430

@@ -27,13 +33,23 @@ public function __construct(OutputInterface $output)
2733
$this->output = $output;
2834
}
2935

36+
/**
37+
* @param string[] $headers
38+
*
39+
* @return $this
40+
*/
3041
public function setHeaders(array $headers)
3142
{
3243
$this->headers = $headers;
3344

3445
return $this;
3546
}
3647

48+
/**
49+
* @param string[][] $rows
50+
*
51+
* @return $this
52+
*/
3753
public function setRows(array $rows)
3854
{
3955
$this->rows = array();
@@ -45,6 +61,9 @@ public function setRows(array $rows)
4561
return $this;
4662
}
4763

64+
/**
65+
* @return void
66+
*/
4867
public function render()
4968
{
5069
$this->renderRow($this->headers);
@@ -55,11 +74,21 @@ public function render()
5574
}
5675
}
5776

77+
/**
78+
* @param string[] $row
79+
*
80+
* @return void
81+
*/
5882
private function renderRow(array $row)
5983
{
6084
$this->output->writeln(sprintf('| %s |', implode(' | ', $this->prepareRow($row))));
6185
}
6286

87+
/**
88+
* @param string[] $row
89+
*
90+
* @return string[]
91+
*/
6392
private function prepareRow(array $row)
6493
{
6594
$line = array();
@@ -71,6 +100,9 @@ private function prepareRow(array $row)
71100
return $line;
72101
}
73102

103+
/**
104+
* @return void
105+
*/
74106
private function renderHorizontalLine()
75107
{
76108
$line = array();
@@ -82,17 +114,30 @@ private function renderHorizontalLine()
82114
$this->output->writeln(sprintf('|%s|', implode('|', $line)));
83115
}
84116

117+
/**
118+
* @param string[] $row
119+
* @param int $column
120+
*
121+
* @return string
122+
*/
85123
private function prepareCell(array $row, $column)
86124
{
87125
return str_pad($row[$column], $this->getColumnWidth($column));
88126
}
89127

128+
/**
129+
* @param int $column
130+
*
131+
* @return int
132+
*/
90133
private function getColumnWidth($column)
91134
{
92135
if (isset($this->columnWidths[$column])) {
93136
return $this->columnWidths[$column];
94137
}
95138

139+
$lengths = array();
140+
96141
foreach (array_merge(array($this->headers), $this->rows) as $row) {
97142
$lengths[] = strlen($row[$column]);
98143
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Formatter;
4+
5+
abstract class MarkdownFormatter extends AbstractFormatter
6+
{
7+
/**
8+
* @param string|null $url
9+
* @param string $title
10+
*
11+
* @return string
12+
*/
13+
protected function formatUrl($url, $title)
14+
{
15+
return null !== $url ? sprintf('[%s](%s)', $title, $url) : '';
16+
}
17+
}

0 commit comments

Comments
 (0)