44
55namespace Rector \Jack \Command ;
66
7+ use Entropy \Console \Contract \CommandInterface ;
8+ use Entropy \Console \Enum \ExitCode ;
9+ use Entropy \Console \Output \OutputPrinter ;
710use Nette \Utils \Json ;
811use Rector \Jack \Composer \ComposerOutdatedResponseProvider ;
912use Rector \Jack \Enum \ComposerKey ;
1013use Rector \Jack \OutdatedComposerFactory ;
11- use Symfony \Component \Console \Command \Command ;
12- use Symfony \Component \Console \Input \InputInterface ;
13- use Symfony \Component \Console \Input \InputOption ;
14- use Symfony \Component \Console \Output \OutputInterface ;
15- use Symfony \Component \Console \Style \SymfonyStyle ;
1614
17- final class BreakPointCommand extends Command
15+ final readonly class BreakPointCommand implements CommandInterface
1816{
1917 public function __construct (
20- private readonly OutdatedComposerFactory $ outdatedComposerFactory ,
21- private readonly ComposerOutdatedResponseProvider $ composerOutdatedResponseProvider
18+ private OutdatedComposerFactory $ outdatedComposerFactory ,
19+ private ComposerOutdatedResponseProvider $ composerOutdatedResponseProvider ,
20+ private OutputPrinter $ outputPrinter ,
2221 ) {
23- parent ::__construct ();
2422 }
2523
26- protected function configure (): void
24+ /**
25+ * @param bool $dev Focus on dev packages only
26+ * @param int $limit Maximum number of outdated major version packages
27+ * @param string[] $ignore Ignore packages by name, e.g. "symfony/" or "symfony/console"
28+ */
29+ public function run (bool $ dev = false , int $ limit = 5 , array $ ignore = []): int
2730 {
28- $ this ->setName ('breakpoint ' );
29-
30- $ this ->setDescription ('Let your CI tell you, if there is too many major-version outdated packages ' );
31- $ this ->addOption ('dev ' , null , InputOption::VALUE_NONE , 'Focus on dev packages only ' );
32-
33- $ this ->addOption (
34- 'limit ' ,
35- null ,
36- InputOption::VALUE_REQUIRED ,
37- 'Maximum number of outdated major version packages ' ,
38- 5
39- );
40-
41- $ this ->addOption (
42- 'ignore ' ,
43- null ,
44- InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL ,
45- 'Ignore packages by name, e.g. "symfony/" or "symfony/console" ' ,
46- );
47- }
48-
49- protected function execute (InputInterface $ input , OutputInterface $ output ): int
50- {
51- $ symfonyStyle = new SymfonyStyle ($ input , $ output );
52-
53- $ maxOutdatePackages = (int ) $ input ->getOption ('limit ' );
54- $ onlyDev = (bool ) $ input ->getOption ('dev ' );
55-
56- $ symfonyStyle ->writeln ('<fg=green>Analyzing "composer.json" for major outdated packages</> ' );
31+ $ this ->outputPrinter ->green ('Analyzing "composer.json" for major outdated packages ' );
5732
5833 $ responseJsonContents = $ this ->composerOutdatedResponseProvider ->provide ();
5934
6035 $ responseJson = Json::decode ($ responseJsonContents , true );
6136 if (! isset ($ responseJson [ComposerKey::INSTALLED_KEY ])) {
62- $ symfonyStyle -> success ('All packages are up to date ' );
37+ $ this -> outputPrinter -> green ('All packages are up to date ' );
6338
64- return self ::SUCCESS ;
39+ return ExitCode ::SUCCESS ;
6540 }
6641
6742 $ composerJsonFilePath = getcwd () . '/composer.json ' ;
6843 $ outdatedComposer = $ this ->outdatedComposerFactory ->createOutdatedComposer (
6944 array_filter (
7045 $ responseJson [ComposerKey::INSTALLED_KEY ],
71- static function (array $ package ) use ($ input ): bool {
72- foreach ($ input -> getOption ( ' ignore ' ) as $ ignoredPackage ) {
46+ static function (array $ package ) use ($ ignore ): bool {
47+ foreach ($ ignore as $ ignoredPackage ) {
7348 if (str_contains ((string ) $ package ['name ' ], $ ignoredPackage )) {
7449 return false ;
7550 }
@@ -82,70 +57,81 @@ static function (array $package) use ($input): bool {
8257 );
8358
8459 if ($ outdatedComposer ->count () === 0 ) {
85- $ symfonyStyle -> success ('All packages are up to date ' );
86- return self ::SUCCESS ;
60+ $ this -> outputPrinter -> greenBackground ('All packages are up to date ' );
61+ return ExitCode ::SUCCESS ;
8762 }
8863
89- $ symfonyStyle -> title (
64+ $ this -> outputPrinter -> yellow (
9065 sprintf (
9166 'Found %d outdated package%s ' ,
92- $ outdatedComposer ->count ($ onlyDev ),
93- $ outdatedComposer ->count ($ onlyDev ) > 1 ? 's ' : ''
67+ $ outdatedComposer ->count ($ dev ),
68+ $ outdatedComposer ->count ($ dev ) > 1 ? 's ' : ''
9469 )
9570 );
71+ $ this ->outputPrinter ->newline ();
9672
97- foreach ($ outdatedComposer ->getPackages ($ onlyDev ) as $ outdatedPackage ) {
98- $ symfonyStyle ->writeln (
73+ foreach ($ outdatedComposer ->getPackages ($ dev ) as $ outdatedPackage ) {
74+ $ this -> outputPrinter ->writeln (
9975 sprintf ('The "<fg=green>%s</>" package is outdated ' , $ outdatedPackage ->getName ())
10076 );
10177
10278 if ($ outdatedPackage ->getCurrentVersionAge ()) {
103- $ symfonyStyle ->writeln (sprintf (
79+ $ this -> outputPrinter ->writeln (sprintf (
10480 ' * Your version %s is <fg=%s>%s</> ' ,
10581 $ outdatedPackage ->getCurrentVersion (),
10682 $ outdatedPackage ->isVeryOld () ? 'red ' : 'yellow ' ,
10783 $ outdatedPackage ->getCurrentVersionAge (),
10884 ));
10985 } else {
11086 // composer 2.7- compatible
111- $ symfonyStyle ->writeln (sprintf (' * Your version is %s ' , $ outdatedPackage ->getCurrentVersion ()));
87+ $ this -> outputPrinter ->writeln (sprintf (' * Your version is %s ' , $ outdatedPackage ->getCurrentVersion ()));
11288 }
11389
114- $ symfonyStyle ->writeln (sprintf (' * Bump to %s ' , $ outdatedPackage ->getLatestVersion ()));
115- $ symfonyStyle ->newLine ();
90+ $ this -> outputPrinter ->writeln (sprintf (' * Bump to %s ' , $ outdatedPackage ->getLatestVersion ()));
91+ $ this -> outputPrinter ->newLine ();
11692 }
11793
118- $ symfonyStyle ->newLine ();
119- if ($ outdatedComposer ->count () >= $ maxOutdatePackages ) {
94+ $ this -> outputPrinter ->newLine ();
95+ if ($ outdatedComposer ->count () >= $ limit ) {
12096 // to much → fail
121- $ symfonyStyle -> error (sprintf (
97+ $ this -> outputPrinter -> redBackground (sprintf (
12298 'There %s %d outdated package%s. Update couple of them to get under %d limit ' ,
12399 $ outdatedComposer ->count () > 1 ? 'are ' : 'is ' ,
124100 $ outdatedComposer ->count (),
125101 $ outdatedComposer ->count () > 1 ? 's ' : '' ,
126- $ maxOutdatePackages
102+ $ limit
127103 ));
128104
129- return self :: FAILURE ;
105+ return ExitCode:: ERROR ;
130106 }
131107
132- if ($ outdatedComposer ->count () > max (1 , $ maxOutdatePackages - 5 )) {
108+ if ($ outdatedComposer ->count () > max (1 , $ limit - 5 )) {
133109 // to much → fail
134- $ symfonyStyle -> warning (sprintf (
110+ $ this -> outputPrinter -> orangeBackground (sprintf (
135111 'There are %d outdated packages. Soon, the count will go over %d limit and this job will fail.%sUpgrade in time ' ,
136112 $ outdatedComposer ->count (),
137- $ maxOutdatePackages ,
113+ $ limit ,
138114 PHP_EOL
139115 ));
140116
141- return self ::SUCCESS ;
117+ return ExitCode ::SUCCESS ;
142118 }
143119
144120 // to many → fail
145- $ symfonyStyle -> success (
146- sprintf ('Still far away from limit %d. Good job keeping your project up to date! ' , $ maxOutdatePackages )
121+ $ this -> outputPrinter -> greenBackground (
122+ sprintf ('Still far away from limit %d. Good job keeping your project up to date! ' , $ limit )
147123 );
148124
149- return self ::SUCCESS ;
125+ return ExitCode::SUCCESS ;
126+ }
127+
128+ public function getName (): string
129+ {
130+ return 'breakpoint ' ;
131+ }
132+
133+ public function getDescription (): string
134+ {
135+ return 'Let your CI tell you, if there are too many major-version outdated packages ' ;
150136 }
151137}
0 commit comments