-
-
Notifications
You must be signed in to change notification settings - Fork 146
refactor(console): improve command completion #1967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
82d68e0
chore: add completion-helper ownership to CODEOWNERS
xHeaven 3633d47
feat(console): add CompletionRuntime class
xHeaven ce5c69c
feat(console): add completion metadata and helper actions
xHeaven 911e09c
feat(console): rewrite shell completion scripts
xHeaven ef128d9
refactor(console): update Shell enum for native completion
xHeaven edea7d0
feat(console): update completion commands for native helper
xHeaven 1554e02
feat(console): add Rust completion helper crate and build script
xHeaven d7aafae
ci: add workflow to build completion binaries on release
xHeaven 7260705
refactor(console): download completion binary from GitHub releases
xHeaven 15e1973
feat(console): add completion:update-bin command
xHeaven f47a2de
docs(console): update shell completion documentation
xHeaven 9816dd9
refactor(console): drop macOS x86_64 completion support
xHeaven ac5b72b
ci: drop x86_64 macOS from completion binary builds
xHeaven 1847f8a
docs(console): note Apple Silicon requirement for macOS
xHeaven c618fcf
fix(console): update ShellTest for unified completion directory
xHeaven a49877f
style(console): fix formatting
xHeaven ab82da0
Merge branch '3.x' into completion
xHeaven dd9018d
refactor(console): skip tests on windows
xHeaven 23fb7ef
refactor(console): drop static keywords
xHeaven bb1bcac
fix(console): force binary download in update-bin command
xHeaven c9569e6
refactor(console): make CompletionRuntime an injectable singleton
xHeaven 9f59692
feat(console): add PHP completion helper
xHeaven 65fecbb
refactor(console): remove Rust completion helper
xHeaven e44e06a
refactor(console): simplify completion commands and runtime
xHeaven 5f811ec
test(console): update tests for PHP completion helper
xHeaven fad862c
style(console): add spaces
xHeaven 679f5cb
chore(console): update gitignore
xHeaven 187b92d
docs: update completion documentation
xHeaven 04bbf5d
test(console): skip windows
xHeaven 85c7b89
Merge branch '3.x' into completion
xHeaven 401f3e5
fix(console): static analysis issues
xHeaven 22000b9
chore: add codeowner entry for completion
xHeaven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 +1,3 @@ | ||
| console.log | ||
| debug.log | ||
| tempest.log | ||
| tempest.log |
This file contains hidden or 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,30 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Tempest\Console\Completion\CompletionApplication; | ||
|
|
||
| $metadataPath = $_SERVER['argv'][1] ?? null; | ||
|
|
||
| if (! $metadataPath) { | ||
| return; | ||
| } | ||
|
|
||
| require_once dirname($metadataPath, 3) . '/vendor/autoload.php'; | ||
|
|
||
| if (! class_exists(CompletionApplication::class)) { | ||
| return; | ||
| } | ||
|
|
||
| if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') { | ||
| return; | ||
| } | ||
|
|
||
| $script = $_SERVER['SCRIPT_FILENAME'] ?? null; | ||
|
|
||
| if (! is_string($script) || realpath($script) !== __FILE__) { | ||
| return; | ||
| } | ||
|
|
||
| new CompletionApplication()->run(array_slice($_SERVER['argv'] ?? [], 1)); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| } | ||
| }, | ||
| "bin": [ | ||
| "bin/tempest" | ||
| "bin/tempest", | ||
| "bin/tempest-complete" | ||
| ] | ||
| } | ||
103 changes: 103 additions & 0 deletions
103
packages/console/src/Actions/BuildCompletionMetadata.php
This file contains hidden or 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,103 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Console\Actions; | ||
|
|
||
| use BackedEnum; | ||
| use Tempest\Console\ConsoleConfig; | ||
| use Tempest\Console\Input\ConsoleArgumentDefinition; | ||
|
|
||
| use function Tempest\Support\str; | ||
|
|
||
| final readonly class BuildCompletionMetadata | ||
| { | ||
| public function __construct( | ||
| private ConsoleConfig $consoleConfig, | ||
| ) {} | ||
|
|
||
| public function __invoke(): array | ||
| { | ||
| $commands = []; | ||
|
|
||
| foreach ($this->consoleConfig->commands as $name => $command) { | ||
| $flags = array_map( | ||
| fn (ConsoleArgumentDefinition $definition): array => [ | ||
| 'name' => $definition->name, | ||
| 'flag' => $this->buildFlagNotation($definition), | ||
| 'aliases' => $this->buildFlagAliases($definition), | ||
| 'description' => $definition->description, | ||
| 'value_options' => $this->buildValueOptions($definition), | ||
| 'repeatable' => $definition->type === 'array' || $definition->isVariadic, | ||
| 'requires_value' => $definition->type !== 'bool', | ||
| ], | ||
| $command->getArgumentDefinitions(), | ||
| ); | ||
|
|
||
| usort($flags, static fn (array $a, array $b): int => $a['flag'] <=> $b['flag']); | ||
|
|
||
| $commands[$name] = [ | ||
| 'hidden' => $command->hidden, | ||
| 'description' => $command->description, | ||
| 'flags' => $flags, | ||
| ]; | ||
| } | ||
|
|
||
| ksort($commands); | ||
|
|
||
| return [ | ||
| 'version' => 1, | ||
| 'commands' => $commands, | ||
| ]; | ||
| } | ||
|
|
||
| private function buildFlagNotation(ConsoleArgumentDefinition $definition): string | ||
| { | ||
| $flag = "--{$definition->name}"; | ||
|
|
||
| if ($definition->type !== 'bool') { | ||
| $flag .= '='; | ||
| } | ||
|
|
||
| return $flag; | ||
| } | ||
|
|
||
| private function buildFlagAliases(ConsoleArgumentDefinition $definition): array | ||
| { | ||
| $aliases = array_values(array_filter(array_map(static function (string $alias): ?string { | ||
| $normalized = ltrim(str($alias)->trim()->kebab()->toString(), '-'); | ||
|
|
||
| if ($normalized === '') { | ||
| return null; | ||
| } | ||
|
|
||
| return match (strlen($normalized)) { | ||
| 1 => "-{$normalized}", | ||
| default => "--{$normalized}", | ||
| }; | ||
| }, $definition->aliases))); | ||
|
|
||
| sort($aliases); | ||
|
|
||
| return $aliases; | ||
| } | ||
|
|
||
| private function buildValueOptions(ConsoleArgumentDefinition $definition): array | ||
| { | ||
| if (! $definition->isBackedEnum()) { | ||
| return []; | ||
| } | ||
|
|
||
| /** @var class-string<BackedEnum> $type */ | ||
| $type = $definition->type; | ||
|
|
||
| $options = array_map( | ||
| static fn (BackedEnum $case): string => (string) $case->value, | ||
| $type::cases(), | ||
| ); | ||
|
|
||
| sort($options); | ||
|
|
||
| return $options; | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
packages/console/src/Commands/CompletionGenerateCommand.php
This file contains hidden or 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,48 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Console\Commands; | ||
|
|
||
| use Tempest\Console\Actions\BuildCompletionMetadata; | ||
| use Tempest\Console\CompletionRuntime; | ||
| use Tempest\Console\Console; | ||
| use Tempest\Console\ConsoleArgument; | ||
| use Tempest\Console\ConsoleCommand; | ||
| use Tempest\Console\ExitCode; | ||
| use Tempest\Support\Filesystem; | ||
|
|
||
| final readonly class CompletionGenerateCommand | ||
| { | ||
| public function __construct( | ||
| private Console $console, | ||
| private CompletionRuntime $completionRuntime, | ||
| private BuildCompletionMetadata $buildCompletionMetadata, | ||
| ) {} | ||
|
|
||
| #[ConsoleCommand( | ||
| name: 'completion:generate', | ||
| description: 'Generate shell completion metadata as JSON', | ||
| )] | ||
| public function __invoke( | ||
| #[ConsoleArgument( | ||
| description: 'Optional output path for the completion metadata JSON', | ||
| aliases: ['-p'], | ||
| )] | ||
| ?string $path = null, | ||
| ): ExitCode { | ||
| if (! $this->completionRuntime->isSupportedPlatform()) { | ||
| $this->console->error($this->completionRuntime->getUnsupportedPlatformMessage()); | ||
|
|
||
| return ExitCode::ERROR; | ||
| } | ||
|
|
||
| $path ??= $this->completionRuntime->getMetadataPath(); | ||
|
|
||
| Filesystem\write_json($path, ($this->buildCompletionMetadata)(), pretty: false); | ||
|
|
||
| $this->console->success("Wrote completion metadata to: {$path}"); | ||
|
|
||
| return ExitCode::SUCCESS; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.