From 4639de2c60611ba16d530c5a28039e10543c51ad Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 7 Jun 2026 20:02:41 +0100 Subject: [PATCH] add list command to print available commands --- .../.github/workflows/bare_run.yaml | 3 +- src/Command/BreakPointCommand.php | 11 ++---- src/Command/ListCommand.php | 37 +++++++++++++++++++ src/DependencyInjection/ContainerFactory.php | 8 ++++ tests/Command/ListCommand/ListCommandTest.php | 30 +++++++++++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/Command/ListCommand.php create mode 100644 tests/Command/ListCommand/ListCommandTest.php diff --git a/build/target-repository/.github/workflows/bare_run.yaml b/build/target-repository/.github/workflows/bare_run.yaml index 3e120a3..f2a277c 100644 --- a/build/target-repository/.github/workflows/bare_run.yaml +++ b/build/target-repository/.github/workflows/bare_run.yaml @@ -20,4 +20,5 @@ jobs: php-version: ${{ matrix.php_version }} coverage: none - - run: php bin/jack --help + # no --ansi here, the entropy console does not support it + - run: php bin/jack list diff --git a/src/Command/BreakPointCommand.php b/src/Command/BreakPointCommand.php index b9a16f7..c295257 100644 --- a/src/Command/BreakPointCommand.php +++ b/src/Command/BreakPointCommand.php @@ -4,6 +4,7 @@ namespace Rector\Jack\Command; +use DateTimeImmutable; use Entropy\Console\Contract\CommandInterface; use Entropy\Console\Enum\ExitCode; use Entropy\Console\Output\OutputPrinter; @@ -41,7 +42,7 @@ public function run(bool $dev = false, int $limit = 5, int $minDays = 0, array $ } $composerJsonFilePath = getcwd() . '/composer.json'; - $now = new \DateTimeImmutable(); + $now = new DateTimeImmutable(); $outdatedComposer = $this->outdatedComposerFactory->createOutdatedComposer( array_filter( $responseJson[ComposerKey::INSTALLED_KEY], @@ -56,12 +57,8 @@ static function (array $package) use ($ignore, $minDays, $now): bool { return true; } - $pageAgeInDays = (new \DateTimeImmutable($package['latest-release-date']))->diff($now)->days; - if ($pageAgeInDays < $minDays) { - return false; - } - - return true; + $pageAgeInDays = new DateTimeImmutable($package['latest-release-date'])->diff($now)->days; + return $pageAgeInDays >= $minDays; } ), $composerJsonFilePath diff --git a/src/Command/ListCommand.php b/src/Command/ListCommand.php new file mode 100644 index 0000000..d3dbf0d --- /dev/null +++ b/src/Command/ListCommand.php @@ -0,0 +1,37 @@ +container->make(HelpPrinter::class); + $helpPrinter->print(); + + return ExitCode::SUCCESS; + } + + public function getName(): string + { + return 'list'; + } + + public function getDescription(): string + { + return 'List available commands'; + } +} diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 86911dd..742b78f 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -5,12 +5,20 @@ namespace Rector\Jack\DependencyInjection; use Entropy\Container\Container; +use Rector\Jack\Command\ListCommand; final class ContainerFactory { public function create(): Container { $container = new Container(); + + // register with container itself, so the help printer can be resolved lazily without circular dependency + $container->service( + ListCommand::class, + static fn (Container $container): ListCommand => new ListCommand($container) + ); + $container->autodiscover(__DIR__ . '/../../src'); return $container; diff --git a/tests/Command/ListCommand/ListCommandTest.php b/tests/Command/ListCommand/ListCommandTest.php new file mode 100644 index 0000000..96f0d6c --- /dev/null +++ b/tests/Command/ListCommand/ListCommandTest.php @@ -0,0 +1,30 @@ +listCommand = $this->make(ListCommand::class); + } + + public function test(): void + { + $this->assertSame('list', $this->listCommand->getName()); + + // output is silenced during unit tests, see OutputPrinter::isSilent + $exitCode = $this->listCommand->run(); + $this->assertSame(ExitCode::SUCCESS, $exitCode); + } +}