-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
230 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Actions; | ||
|
||
use Tempest\Console\CompletesConsoleCommand; | ||
use Tempest\Console\ConsoleCommand; | ||
use Tempest\Console\HasConsole; | ||
use Tempest\Console\Input\ConsoleArgumentBag; | ||
|
||
final readonly class CompleteConsoleCommandArguments | ||
final readonly class CompleteConsoleCommandArguments implements CompletesConsoleCommand | ||
{ | ||
use HasConsole; | ||
|
||
public function __invoke( | ||
public function complete( | ||
ConsoleCommand $command, | ||
ConsoleArgumentBag $argumentBag, | ||
int $current, | ||
): void { | ||
): array { | ||
$definitions = $command->getArgumentDefinitions(); | ||
|
||
$last = $argumentBag->last(); | ||
|
||
if ($last && $last->value === null) { | ||
return []; | ||
} | ||
|
||
$completions = []; | ||
|
||
foreach ($definitions as $definition) { | ||
if ($definition->type !== 'array' && $argumentBag->has($definition->name)) { | ||
continue; | ||
} | ||
|
||
$this->write("--{$definition->name}"); | ||
$argument = "--{$definition->name}"; | ||
|
||
if ($definition->type !== 'bool') { | ||
$this->write('='); | ||
$argument .= '='; | ||
} | ||
|
||
$this->writeln(); | ||
$completions[] = $argument; | ||
} | ||
|
||
return $completions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Actions; | ||
|
||
use Tempest\Console\Console; | ||
use Tempest\Console\ConsoleConfig; | ||
use Tempest\Console\HasConsole; | ||
use Tempest\Console\Input\ConsoleArgumentBag; | ||
|
||
final readonly class CompleteConsoleCommandNames | ||
{ | ||
use HasConsole; | ||
|
||
public function __construct( | ||
private Console $console, | ||
private ConsoleConfig $consoleConfig, | ||
) {} | ||
) { | ||
} | ||
|
||
public function __invoke( | ||
ConsoleArgumentBag $argumentBag, | ||
int $current, | ||
): void | ||
public function complete(ConsoleArgumentBag $argumentBag, int $current): array | ||
{ | ||
$currentCommandName = $argumentBag->getCommandName(); | ||
|
||
$completions = []; | ||
|
||
foreach ($this->consoleConfig->commands as $name => $definition) { | ||
if (! str_starts_with($name, $currentCommandName)) { | ||
continue; | ||
} | ||
|
||
$this->writeln($name); | ||
$completions[] = $name; | ||
} | ||
|
||
return $completions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console; | ||
|
||
use Tempest\Console\Input\ConsoleArgumentBag; | ||
|
||
interface CompletesConsoleCommand | ||
{ | ||
public function complete( | ||
ConsoleCommand $command, | ||
ConsoleArgumentBag $argumentBag, | ||
int $current, | ||
): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Console\Actions; | ||
|
||
use Tests\Tempest\Console\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @small | ||
*/ | ||
class CompleteConsoleCommandArgumentsTest extends TestCase | ||
{ | ||
public function test_arguments_are_printed(): void | ||
{ | ||
$this->console | ||
->complete('completion:test') | ||
->assertSee("--value=" . PHP_EOL) | ||
->assertSee("--flag" . PHP_EOL) | ||
->assertSee("--items=" . PHP_EOL); | ||
} | ||
|
||
public function test_existing_arguments_are_skipped(): void | ||
{ | ||
$this->console | ||
->complete('completion:test --flag') | ||
->assertNotSee('--flag'); | ||
|
||
$this->console | ||
->complete('completion:test --flag=false') | ||
->assertNotSee('--flag'); | ||
|
||
$this->console | ||
->complete('completion:test --value=bar') | ||
->assertNotSee('--value'); | ||
} | ||
|
||
public function test_multiple_array_values_are_allowed(): void | ||
{ | ||
$this->console | ||
->complete('completion:test --items=a') | ||
->assertSee('--items='); | ||
} | ||
|
||
public function test_open_flag_must_first_be_completed(): void | ||
{ | ||
$this->console | ||
->complete('completion:test --items=') | ||
->assertNotSee("--value=" . PHP_EOL) | ||
->assertNotSee("--flag" . PHP_EOL) | ||
->assertNotSee("--items=" . PHP_EOL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Console\Commands; | ||
|
||
use Tests\Tempest\Console\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @small | ||
*/ | ||
class CompleteCommandTest extends TestCase | ||
{ | ||
public function test_complete_commands(): void | ||
{ | ||
$this->console | ||
->complete() | ||
->assertSee('tail:server' . PHP_EOL) | ||
->assertSee('schedule:run' . PHP_EOL); | ||
} | ||
|
||
public function test_complete_arguments(): void | ||
{ | ||
$this->console | ||
->complete('tail:') | ||
->assertSee('tail:server' . PHP_EOL) | ||
->assertSee('tail:project' . PHP_EOL) | ||
->assertSee('tail:debug' . PHP_EOL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Console\Fixtures; | ||
|
||
use Tempest\Console\ConsoleCommand; | ||
|
||
final readonly class CompletionTestCommand | ||
{ | ||
#[ConsoleCommand('completion:test')] | ||
public function __invoke(string $value, bool $flag = false, array $items = []) | ||
{ | ||
// TODO: Implement __invoke() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Console\Input; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Console\Input\ConsoleInputArgument; | ||
|
||
/** | ||
* @internal | ||
* @small | ||
*/ | ||
class ConsoleInputArgumentTest extends TestCase | ||
{ | ||
public function test_parse_named_arguments(): void | ||
{ | ||
$input = ConsoleInputArgument::fromString('--flag=abc'); | ||
$this->assertSame('abc', $input->value); | ||
|
||
$input = ConsoleInputArgument::fromString('--flag'); | ||
$this->assertTrue($input->value); | ||
|
||
$input = ConsoleInputArgument::fromString('--flag=true'); | ||
$this->assertTrue($input->value); | ||
|
||
$input = ConsoleInputArgument::fromString('--flag=false'); | ||
$this->assertFalse($input->value); | ||
|
||
$input = ConsoleInputArgument::fromString('--flag='); | ||
$this->assertNull($input->value); | ||
|
||
$input = ConsoleInputArgument::fromString('--flag="abc"'); | ||
$this->assertSame('abc', $input->value); | ||
} | ||
} |