Skip to content

Commit

Permalink
feat(console): provide command suggestions when using : shorthands (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi authored Dec 7, 2024
1 parent 88bd85d commit 107f8b8
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Tempest/Cache/src/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Cache/src/CacheStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Console/src/Commands/TailDebugLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Console/src/Commands/TailProjectLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Console/src/Commands/TailServerLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 15 additions & 1 deletion src/Tempest/Console/src/Middleware/ResolveOrRescueMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -83,7 +97,7 @@ private function getSimilarCommands(string $name): array

$levenshtein = levenshtein($name, $consoleCommand->getName());

if ($levenshtein <= 3) {
if ($levenshtein <= 2) {
$similarCommands[] = $consoleCommand->getName();
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/Tempest/Core/src/Commands/DiscoveryClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions src/Tempest/Core/src/Commands/DiscoveryGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions src/Tempest/Core/src/Commands/DiscoveryStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<h2>Registered discovery classes</h2>');
Expand Down
6 changes: 1 addition & 5 deletions src/Tempest/Framework/Commands/ConfigShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 107f8b8

Please sign in to comment.