From 107f8b869e4486d24ef8a3873c7c8048acca246c Mon Sep 17 00:00:00 2001 From: Enzo Innocenzi Date: Sat, 7 Dec 2024 11:28:13 -0800 Subject: [PATCH] feat(console): provide command suggestions when using `:` shorthands (#814) --- src/Tempest/Cache/src/CacheClearCommand.php | 2 +- src/Tempest/Cache/src/CacheStatusCommand.php | 2 +- .../Console/src/Commands/TailDebugLogCommand.php | 2 +- .../src/Commands/TailProjectLogCommand.php | 2 +- .../src/Commands/TailServerLogCommand.php | 2 +- .../src/Middleware/ResolveOrRescueMiddleware.php | 16 +++++++++++++++- .../Core/src/Commands/DiscoveryClearCommand.php | 6 +----- .../src/Commands/DiscoveryGenerateCommand.php | 6 +----- .../Core/src/Commands/DiscoveryStatusCommand.php | 6 +----- .../Framework/Commands/ConfigShowCommand.php | 6 +----- .../Middleware/ResolveOrRescueMiddlewareTest.php | 4 ++++ 11 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/Tempest/Cache/src/CacheClearCommand.php b/src/Tempest/Cache/src/CacheClearCommand.php index a1e41f764..2e80bcc33 100644 --- a/src/Tempest/Cache/src/CacheClearCommand.php +++ b/src/Tempest/Cache/src/CacheClearCommand.php @@ -18,7 +18,7 @@ public function __construct( ) { } - #[ConsoleCommand(name: 'cache:clear', description: 'Clears all or specified caches', aliases: ['cc'])] + #[ConsoleCommand(name: 'cache:clear', description: 'Clears all or specified caches')] public function __invoke(bool $all = false): void { $caches = $this->cacheConfig->caches; diff --git a/src/Tempest/Cache/src/CacheStatusCommand.php b/src/Tempest/Cache/src/CacheStatusCommand.php index 220f2a25c..79b10787f 100644 --- a/src/Tempest/Cache/src/CacheStatusCommand.php +++ b/src/Tempest/Cache/src/CacheStatusCommand.php @@ -20,7 +20,7 @@ public function __construct( ) { } - #[ConsoleCommand(name: 'cache:status', description: 'Shows which caches are enabled', aliases: ['cs'])] + #[ConsoleCommand(name: 'cache:status', description: 'Shows which caches are enabled')] public function __invoke(): void { $caches = $this->cacheConfig->caches; diff --git a/src/Tempest/Console/src/Commands/TailDebugLogCommand.php b/src/Tempest/Console/src/Commands/TailDebugLogCommand.php index ee0643647..9f6620a8d 100644 --- a/src/Tempest/Console/src/Commands/TailDebugLogCommand.php +++ b/src/Tempest/Console/src/Commands/TailDebugLogCommand.php @@ -22,7 +22,7 @@ public function __construct( ) { } - #[ConsoleCommand('tail:debug', description: 'Tails the debug log', aliases: ['td'])] + #[ConsoleCommand('tail:debug', description: 'Tails the debug log')] public function __invoke(): void { $debugLogPath = $this->logConfig->debugLogPath; diff --git a/src/Tempest/Console/src/Commands/TailProjectLogCommand.php b/src/Tempest/Console/src/Commands/TailProjectLogCommand.php index c64d74857..c7c0ad350 100644 --- a/src/Tempest/Console/src/Commands/TailProjectLogCommand.php +++ b/src/Tempest/Console/src/Commands/TailProjectLogCommand.php @@ -23,7 +23,7 @@ public function __construct( ) { } - #[ConsoleCommand('tail:project', description: 'Tails the project log', aliases: ['tp'])] + #[ConsoleCommand('tail:project', description: 'Tails the project log')] public function __invoke(): void { $appendLogChannel = null; diff --git a/src/Tempest/Console/src/Commands/TailServerLogCommand.php b/src/Tempest/Console/src/Commands/TailServerLogCommand.php index ff537d88d..29b3404ef 100644 --- a/src/Tempest/Console/src/Commands/TailServerLogCommand.php +++ b/src/Tempest/Console/src/Commands/TailServerLogCommand.php @@ -22,7 +22,7 @@ public function __construct( ) { } - #[ConsoleCommand('tail:server', description: 'Tails the server log', aliases: ['ts'])] + #[ConsoleCommand('tail:server', description: 'Tails the server log')] public function __invoke(): void { $serverLogPath = $this->logConfig->serverLogPath; diff --git a/src/Tempest/Console/src/Middleware/ResolveOrRescueMiddleware.php b/src/Tempest/Console/src/Middleware/ResolveOrRescueMiddleware.php index f38824cad..4d77658e4 100644 --- a/src/Tempest/Console/src/Middleware/ResolveOrRescueMiddleware.php +++ b/src/Tempest/Console/src/Middleware/ResolveOrRescueMiddleware.php @@ -7,11 +7,13 @@ use Tempest\Console\Actions\ExecuteConsoleCommand; use Tempest\Console\Actions\ResolveConsoleCommand; use Tempest\Console\Console; +use Tempest\Console\ConsoleCommand; use Tempest\Console\ConsoleConfig; use Tempest\Console\ConsoleMiddleware; use Tempest\Console\ConsoleMiddlewareCallable; use Tempest\Console\ExitCode; use Tempest\Console\Initializers\Invocation; +use Tempest\Support\ArrayHelper; use Throwable; final readonly class ResolveOrRescueMiddleware implements ConsoleMiddleware @@ -70,11 +72,23 @@ private function getSimilarCommands(string $name): array { $similarCommands = []; + /** @var ConsoleCommand $consoleCommand */ foreach ($this->consoleConfig->commands as $consoleCommand) { if (in_array($consoleCommand->getName(), $similarCommands, strict: true)) { continue; } + if (str_contains($name, ':')) { + $wantedParts = ArrayHelper::explode($name, separator: ':'); + $currentParts = ArrayHelper::explode($consoleCommand->getName(), separator: ':'); + + if ($wantedParts->count() === $currentParts->count() && $wantedParts->every(fn (string $part, int $index) => str_starts_with($currentParts[$index], $part))) { + $similarCommands[] = $consoleCommand->getName(); + + continue; + } + } + if (str_starts_with($consoleCommand->getName(), $name)) { $similarCommands[] = $consoleCommand->getName(); @@ -83,7 +97,7 @@ private function getSimilarCommands(string $name): array $levenshtein = levenshtein($name, $consoleCommand->getName()); - if ($levenshtein <= 3) { + if ($levenshtein <= 2) { $similarCommands[] = $consoleCommand->getName(); } } diff --git a/src/Tempest/Core/src/Commands/DiscoveryClearCommand.php b/src/Tempest/Core/src/Commands/DiscoveryClearCommand.php index b34940277..a3056567b 100644 --- a/src/Tempest/Core/src/Commands/DiscoveryClearCommand.php +++ b/src/Tempest/Core/src/Commands/DiscoveryClearCommand.php @@ -16,11 +16,7 @@ public function __construct( ) { } - #[ConsoleCommand( - name: 'discovery:clear', - description: 'Clears all cached discovery files', - aliases: ['dc'], - )] + #[ConsoleCommand(name: 'discovery:clear', description: 'Clears all cached discovery files')] public function __invoke(): void { $this->discoveryCache->clear(); diff --git a/src/Tempest/Core/src/Commands/DiscoveryGenerateCommand.php b/src/Tempest/Core/src/Commands/DiscoveryGenerateCommand.php index 1fa0ce591..b5a2e4892 100644 --- a/src/Tempest/Core/src/Commands/DiscoveryGenerateCommand.php +++ b/src/Tempest/Core/src/Commands/DiscoveryGenerateCommand.php @@ -24,11 +24,7 @@ public function __construct( ) { } - #[ConsoleCommand( - name: 'discovery:generate', - description: 'Compile and cache all discovery according to the configured discovery caching strategy', - aliases: ['dg'], - )] + #[ConsoleCommand(name: 'discovery:generate', description: 'Compile and cache all discovery according to the configured discovery caching strategy')] public function __invoke(): void { $strategy = $this->resolveDiscoveryCacheStrategy(); diff --git a/src/Tempest/Core/src/Commands/DiscoveryStatusCommand.php b/src/Tempest/Core/src/Commands/DiscoveryStatusCommand.php index c72dc2d0b..5cb934135 100644 --- a/src/Tempest/Core/src/Commands/DiscoveryStatusCommand.php +++ b/src/Tempest/Core/src/Commands/DiscoveryStatusCommand.php @@ -18,11 +18,7 @@ public function __construct( ) { } - #[ConsoleCommand( - name: 'discovery:status', - description: 'Lists all discovery locations and discovery classes', - aliases: ['ds'], - )] + #[ConsoleCommand(name: 'discovery:status', description: 'Lists all discovery locations and discovery classes')] public function __invoke(): void { $this->console->writeln('

Registered discovery classes

'); diff --git a/src/Tempest/Framework/Commands/ConfigShowCommand.php b/src/Tempest/Framework/Commands/ConfigShowCommand.php index 4b2c37530..1416a63a2 100644 --- a/src/Tempest/Framework/Commands/ConfigShowCommand.php +++ b/src/Tempest/Framework/Commands/ConfigShowCommand.php @@ -31,11 +31,7 @@ public function __construct( ) { } - #[ConsoleCommand( - name: 'config:show', - description: 'Show resolved configuration', - aliases: ['config'], - )] + #[ConsoleCommand(name: 'config:show', description: 'Show resolved configuration', aliases: ['config'])] public function __invoke( ConfigShowFormat $format = ConfigShowFormat::PRETTY, ?bool $search = false, diff --git a/tests/Integration/Console/Middleware/ResolveOrRescueMiddlewareTest.php b/tests/Integration/Console/Middleware/ResolveOrRescueMiddlewareTest.php index 5e5ccdc63..62648f311 100644 --- a/tests/Integration/Console/Middleware/ResolveOrRescueMiddlewareTest.php +++ b/tests/Integration/Console/Middleware/ResolveOrRescueMiddlewareTest.php @@ -23,6 +23,10 @@ public function it_can_find_a_single_similar_command(): void $this->console ->call('bascovery:status') ->assertSee('Did you mean discovery:status?'); + + $this->console + ->call('c:cl') + ->assertSee('Did you mean cache:clear?'); } #[Test]