-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBreakPointCommand.php
More file actions
128 lines (104 loc) · 4.5 KB
/
BreakPointCommand.php
File metadata and controls
128 lines (104 loc) · 4.5 KB
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
<?php
declare(strict_types=1);
namespace Rector\Jack\Command;
use Nette\Utils\Json;
use Rector\Jack\Composer\ComposerOutdatedResponseProvider;
use Rector\Jack\Enum\ComposerKey;
use Rector\Jack\OutdatedComposerFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
final class BreakPointCommand extends Command
{
public function __construct(
private readonly OutdatedComposerFactory $outdatedComposerFactory,
private readonly ComposerOutdatedResponseProvider $composerOutdatedResponseProvider
) {
parent::__construct();
}
protected function configure(): void
{
$this->setName('breakpoint');
$this->setDescription('Let your CI tell you, if there is too many major-version outdated packages');
$this->addOption('dev', null, InputOption::VALUE_NONE, 'Focus on dev packages only');
$this->addOption(
'limit',
null,
InputOption::VALUE_REQUIRED,
'Maximum number of outdated major version packages',
5
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$symfonyStyle = new SymfonyStyle($input, $output);
$maxOutdatePackages = (int) $input->getOption('limit');
$onlyDev = (bool) $input->getOption('dev');
$symfonyStyle->writeln('<fg=green>Analyzing "composer.json" for major outdated packages</>');
$responseJsonContents = $this->composerOutdatedResponseProvider->provide();
$responseJson = Json::decode($responseJsonContents, true);
if (! isset($responseJson[ComposerKey::INSTALLED_KEY])) {
$symfonyStyle->success('All packages are up to date');
return self::SUCCESS;
}
$composerJsonFilePath = getcwd() . '/composer.json';
$outdatedComposer = $this->outdatedComposerFactory->createOutdatedComposer(
$responseJson[ComposerKey::INSTALLED_KEY],
$composerJsonFilePath
);
$symfonyStyle->title(
sprintf(
'Found %d outdated package%s',
$outdatedComposer->count($onlyDev),
$outdatedComposer->count($onlyDev) > 1 ? 's' : ''
)
);
foreach ($outdatedComposer->getPackages($onlyDev) as $outdatedPackage) {
$symfonyStyle->writeln(
sprintf('The "<fg=green>%s</>" package is outdated', $outdatedPackage->getName())
);
if ($outdatedPackage->getCurrentVersionAge()) {
$symfonyStyle->writeln(sprintf(
' * Your version %s is <fg=%s>%s</>',
$outdatedPackage->getCurrentVersion(),
$outdatedPackage->isVeryOld() ? 'red' : 'yellow',
$outdatedPackage->getCurrentVersionAge(),
));
} else {
// composer 2.7- compatible
$symfonyStyle->writeln(sprintf(' * Your version is %s', $outdatedPackage->getCurrentVersion()));
}
$symfonyStyle->writeln(sprintf(' * Bump to %s', $outdatedPackage->getLatestVersion()));
$symfonyStyle->newLine();
}
$symfonyStyle->newLine();
if ($outdatedComposer->count() >= $maxOutdatePackages) {
// to much → fail
$symfonyStyle->error(sprintf(
'There %s %d outdated package%s. Update couple of them to get under %d limit',
$outdatedComposer->count() > 1 ? 'are' : 'is',
$outdatedComposer->count(),
$outdatedComposer->count() > 1 ? 's' : '',
$maxOutdatePackages
));
return self::FAILURE;
}
if ($outdatedComposer->count() > max(1, $maxOutdatePackages - 5)) {
// to much → fail
$symfonyStyle->warning(sprintf(
'There are %d outdated packages. Soon, the count will go over %d limit and this job will fail.%sUpgrade in time',
$outdatedComposer->count(),
$maxOutdatePackages,
PHP_EOL
));
return self::SUCCESS;
}
// to many → fail
$symfonyStyle->success(
sprintf('Still far away from limit %d. Good job keeping your project up to date!', $maxOutdatePackages)
);
return self::SUCCESS;
}
}