Skip to content

Commit 117ce53

Browse files
committed
add markdown list formatter
1 parent e015891 commit 117ce53

File tree

13 files changed

+366
-106
lines changed

13 files changed

+366
-106
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ composer diff # Displays packages changed in current git tree compared with HEAD
6464
- `--no-dev` - ignore dev dependencies (`require-dev`)
6565
- `--no-prod` - ignore prod dependencies (`require`)
6666
- `--with-platform` (`-p`) - include platform dependencies (PHP, extensions, etc.)
67+
- `--format` (`-f`) - output format (mdtable, mdlist) - default: `mdtable`
6768

6869
## Advanced usage
6970

7071
```shell script
7172
composer diff -b master:composer.lock -t develop:composer.lock -p # Compare master and develop branches, including platform dependencies
7273
composer diff --no-dev # ignore dev dependencies
7374
composer diff -p # include platform dependencies
75+
composer diff -f mdlist # Output as Markdown list instead of table
7476
```
7577

7678

src/Command/DiffCommand.php

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace IonBazan\ComposerDiff\Command;
44

55
use Composer\Command\BaseCommand;
6-
use Composer\DependencyResolver\Operation\InstallOperation;
7-
use Composer\DependencyResolver\Operation\OperationInterface;
8-
use Composer\DependencyResolver\Operation\UninstallOperation;
9-
use Composer\DependencyResolver\Operation\UpdateOperation;
10-
use IonBazan\ComposerDiff\MarkdownTable;
6+
use IonBazan\ComposerDiff\Formatter\Formatter;
7+
use IonBazan\ComposerDiff\Formatter\MarkdownListFormatter;
8+
use IonBazan\ComposerDiff\Formatter\MarkdownTableFormatter;
119
use IonBazan\ComposerDiff\PackageDiff;
1210
use Symfony\Component\Console\Input\InputInterface;
1311
use Symfony\Component\Console\Input\InputOption;
@@ -36,6 +34,7 @@ protected function configure()
3634
->addOption('no-dev', null, InputOption::VALUE_NONE, 'Ignore dev dependencies')
3735
->addOption('no-prod', null, InputOption::VALUE_NONE, 'Ignore prod dependencies')
3836
->addOption('with-platform', 'p', InputOption::VALUE_NONE, 'Include platform dependencies (PHP version, extensions, etc.)')
37+
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format (mdtable, mdlist)', 'mdtable')
3938
;
4039
}
4140

@@ -44,63 +43,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
4443
$base = $input->getOption('base');
4544
$target = $input->getOption('target');
4645
$withPlatform = $input->getOption('with-platform');
46+
$formatter = $this->getFormatter($input, $output);
4747

4848
if (!$input->getOption('no-prod')) {
4949
$operations = $this->packageDiff->getPackageDiff($base, $target, false, $withPlatform);
50-
$this->displayTable($operations, 'Prod Packages', $output);
50+
$formatter->render($operations, 'Prod Packages', $output);
5151
}
5252

5353
if (!$input->getOption('no-dev')) {
5454
$operations = $this->packageDiff->getPackageDiff($base, $target, true, $withPlatform);
55-
$this->displayTable($operations, 'Dev Packages', $output);
55+
$formatter->render($operations, 'Dev Packages', $output);
5656
}
5757

5858
return 0;
5959
}
6060

61-
protected function displayTable(array $operations, $header, OutputInterface $output)
62-
{
63-
if (!\count($operations)) {
64-
return;
65-
}
66-
67-
$rows = array();
68-
69-
foreach ($operations as $operation) {
70-
$rows[] = $this->getTableRow($operation);
71-
}
72-
73-
$table = new MarkdownTable($output);
74-
$table->setHeaders(array($header, 'Base', 'Target'))->setRows($rows)->render();
75-
$output->writeln('');
76-
}
77-
78-
protected function getTableRow(OperationInterface $operation)
61+
/**
62+
* @return Formatter
63+
*/
64+
private function getFormatter(InputInterface $input, OutputInterface $output)
7965
{
80-
if ($operation instanceof InstallOperation) {
81-
return array(
82-
$operation->getPackage()->getName(),
83-
'New',
84-
$operation->getPackage()->getFullPrettyVersion(),
85-
);
86-
}
87-
88-
if ($operation instanceof UpdateOperation) {
89-
return array(
90-
$operation->getInitialPackage()->getName(),
91-
$operation->getInitialPackage()->getFullPrettyVersion(),
92-
$operation->getTargetPackage()->getFullPrettyVersion(),
93-
);
66+
switch ($input->getOption('format')) {
67+
case 'mdlist':
68+
return new MarkdownListFormatter($output);
69+
case 'mdtable':
70+
default:
71+
return new MarkdownTableFormatter($output);
9472
}
95-
96-
if ($operation instanceof UninstallOperation) {
97-
return array(
98-
$operation->getPackage()->getName(),
99-
$operation->getPackage()->getFullPrettyVersion(),
100-
'Removed',
101-
);
102-
}
103-
104-
throw new \InvalidArgumentException('Invalid operation');
10573
}
10674
}

src/Formatter/Formatter.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Formatter;
4+
5+
use Composer\DependencyResolver\Operation\OperationInterface;
6+
7+
interface Formatter
8+
{
9+
/**
10+
* @param OperationInterface[] $operations
11+
* @param string $title
12+
*
13+
* @return void
14+
*/
15+
public function render(array $operations, $title);
16+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace IonBazan\ComposerDiff;
3+
namespace IonBazan\ComposerDiff\Formatter\Helper;
44

55
use Symfony\Component\Console\Output\OutputInterface;
66

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Formatter;
4+
5+
use Composer\DependencyResolver\Operation\InstallOperation;
6+
use Composer\DependencyResolver\Operation\OperationInterface;
7+
use Composer\DependencyResolver\Operation\UninstallOperation;
8+
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class MarkdownListFormatter implements Formatter
12+
{
13+
/**
14+
* @var OutputInterface
15+
*/
16+
protected $output;
17+
18+
public function __construct(OutputInterface $output)
19+
{
20+
$this->output = $output;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function render(array $operations, $title)
27+
{
28+
if (!\count($operations)) {
29+
return;
30+
}
31+
32+
$this->output->writeln($title);
33+
$this->output->writeln(str_repeat('=', strlen($title)));
34+
$this->output->writeln('');
35+
36+
foreach ($operations as $operation) {
37+
$this->output->writeln($this->getRow($operation));
38+
}
39+
40+
$this->output->writeln('');
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
protected function getRow(OperationInterface $operation)
47+
{
48+
if ($operation instanceof InstallOperation) {
49+
return sprintf(
50+
' - Install %s (%s)',
51+
$operation->getPackage()->getName(),
52+
$operation->getPackage()->getFullPrettyVersion()
53+
);
54+
}
55+
56+
if ($operation instanceof UpdateOperation) {
57+
return sprintf(
58+
' - Update %s (%s => %s)',
59+
$operation->getInitialPackage()->getName(),
60+
$operation->getInitialPackage()->getFullPrettyVersion(),
61+
$operation->getTargetPackage()->getFullPrettyVersion()
62+
);
63+
}
64+
65+
if ($operation instanceof UninstallOperation) {
66+
return sprintf(
67+
' - Remove %s (%s)',
68+
$operation->getPackage()->getName(),
69+
$operation->getPackage()->getFullPrettyVersion()
70+
);
71+
}
72+
73+
throw new \InvalidArgumentException('Invalid operation');
74+
}
75+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Formatter;
4+
5+
use Composer\DependencyResolver\Operation\InstallOperation;
6+
use Composer\DependencyResolver\Operation\OperationInterface;
7+
use Composer\DependencyResolver\Operation\UninstallOperation;
8+
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use IonBazan\ComposerDiff\Formatter\Helper\MarkdownTable;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class MarkdownTableFormatter implements Formatter
13+
{
14+
/**
15+
* @var OutputInterface
16+
*/
17+
protected $output;
18+
19+
public function __construct(OutputInterface $output)
20+
{
21+
$this->output = $output;
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function render(array $operations, $title)
28+
{
29+
if (!\count($operations)) {
30+
return;
31+
}
32+
33+
$rows = array();
34+
35+
foreach ($operations as $operation) {
36+
$rows[] = $this->getTableRow($operation);
37+
}
38+
39+
$table = new MarkdownTable($this->output);
40+
$table->setHeaders(array($title, 'Base', 'Target'))->setRows($rows)->render();
41+
$this->output->writeln('');
42+
}
43+
44+
protected function getTableRow(OperationInterface $operation)
45+
{
46+
if ($operation instanceof InstallOperation) {
47+
return array(
48+
$operation->getPackage()->getName(),
49+
'New',
50+
$operation->getPackage()->getFullPrettyVersion(),
51+
);
52+
}
53+
54+
if ($operation instanceof UpdateOperation) {
55+
return array(
56+
$operation->getInitialPackage()->getName(),
57+
$operation->getInitialPackage()->getFullPrettyVersion(),
58+
$operation->getTargetPackage()->getFullPrettyVersion(),
59+
);
60+
}
61+
62+
if ($operation instanceof UninstallOperation) {
63+
return array(
64+
$operation->getPackage()->getName(),
65+
$operation->getPackage()->getFullPrettyVersion(),
66+
'Removed',
67+
);
68+
}
69+
70+
throw new \InvalidArgumentException('Invalid operation');
71+
}
72+
}

0 commit comments

Comments
 (0)