1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
<?php
namespace Studip\Cli\Commands\Composer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
final class GenerateUpdateList extends Command
{
protected static $defaultName = 'composer:outdated';
protected function configure(): void
{
$this->setDescription('Generate markdown list of outdated packages');
$this->setHelp('This command will create a markdown list of all outdated packages.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$process = new Process(['composer', 'outdated', '-D', '--locked', '--format=json']);
$process->mustRun();
$json = $process->getOutput();
if ($json) {
$output->writeln($json, OutputInterface::VERBOSITY_VERBOSE);
}
} catch (ProcessFailedException $e) {
$output->writeln("<error>Could not execute shell command</error>");
$output->writeln($e->getmessage());
return Command::FAILURE;
}
$list = json_decode($json, true);
$packages = [
'major' => [
'title' => 'Major updates',
'items' => [],
],
'minor' => [
'title' => 'Minor updates',
'items' => [],
],
'abandoned' => [
'title' => 'Abandoned packages',
'items' => [],
],
];
foreach ($list['locked'] as $package) {
if ($package['abandoned']) {
$packages['abandoned']['items'][] = $package;
} elseif ($package['latest-status'] === 'semver-safe-update') {
$packages['minor']['items'][] = $package;
} else {
$packages['major']['items'][] = $package;
}
}
foreach ($packages as $p) {
$this->outputMarkdownListOfPackages($output, $p);
}
return Command::SUCCESS;
}
private function outputMarkdownListOfPackages(
OutputInterface $output,
array $packages
): void
{
if (count($packages['items']) === 0) {
return;
}
$output->writeln("# {$packages['title']}");
$output->writeln('');
$headers = [
'issue' => 'Issue',
'name' => 'Package',
'version' => 'Installiert',
'latest' => 'Verfügbar',
];
$rows = [];
foreach ($packages['items'] as $package) {
$name = $package['name'];
if ($package['homepage']) {
$name = "[{$name}]({$package['homepage']})";
} elseif ($package['source']) {
$name = "[{$name}]({$package['source']})";
}
$row = [
'issue' => '',
'name' => $name,
'version' => $package['version'],
'latest' => $package['latest'],
];
$rows[] = $row;
}
$pad_sizes = $this->getPadSizes($headers, ...$rows);
// Output headers
$this->outputTableRow($output, $headers, $pad_sizes);
// Output dividers
$this->outputTableRow($output, array_fill_keys(array_keys($headers), ''), $pad_sizes, '-');
// Output all rows
foreach ($rows as $row) {
$this->outputTableRow($output, $row, $pad_sizes);
}
$output->writeln('');
}
private function outputTableRow(OutputInterface $output, array $row, array $pad_sizes = [], string $pad = ' '): void
{
$items = [];
foreach ($row as $key => $value) {
$items[] = str_pad($value, $pad_sizes[$key] ?? 0, $pad);
}
$output->writeln('| ' . implode(' | ', $items) . ' |');
}
private function getPadSizes(array ...$rows): array
{
$sizes = [];
foreach ($rows as $row) {
foreach ($row as $key => $value) {
if (!isset($sizes[$key])) {
$sizes[$key] = mb_strlen($value);
} else {
$sizes[$key] = max($sizes[$key], mb_strlen($value));
}
}
}
return $sizes;
}
}
|