-
-
Notifications
You must be signed in to change notification settings - Fork 144
feat(console): add native command completion for zsh and bash #1851
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 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b9b7a17
feat(console): implement completion commands
xHeaven 7f14a43
feat(tests): test completion commands
xHeaven 8d01be8
feat(console): implement shell detector
xHeaven 433e6fb
feat(tests): test shell detector
xHeaven fd9eef2
feat(console): implement completion scripts for zsh and bash
xHeaven 4ebdf3f
feat(docs): add documentation for shell completion
xHeaven e6de0a2
chore(console): remove old script
xHeaven 5a245fb
refactor(console): use enum directly
xHeaven 5c66148
refactor(tests): update completion tests to reflect code changes
xHeaven f62e02d
refactor(console): error if no acceptable shell detected
xHeaven c8b41a7
refactor(console): extract resolveShell method into action class
xHeaven 5342697
refactor(console): simplify shell resolver
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
132 changes: 132 additions & 0 deletions
132
packages/console/src/Commands/CompletionInstallCommand.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,132 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Console\Commands; | ||
|
|
||
| use Symfony\Component\Filesystem\Path; | ||
| use Tempest\Console\Console; | ||
| use Tempest\Console\ConsoleArgument; | ||
| use Tempest\Console\ConsoleCommand; | ||
| use Tempest\Console\Enums\Shell; | ||
| use Tempest\Console\ExitCode; | ||
| use Tempest\Support\Filesystem; | ||
|
|
||
| use function Tempest\Support\path; | ||
|
|
||
| final readonly class CompletionInstallCommand | ||
| { | ||
| public function __construct( | ||
| private Console $console, | ||
| ) {} | ||
|
|
||
| #[ConsoleCommand( | ||
| name: 'completion:install', | ||
| description: 'Install shell completion for Tempest', | ||
| )] | ||
| public function __invoke( | ||
| #[ConsoleArgument( | ||
| description: 'The shell to install completions for (zsh, bash)', | ||
| aliases: ['-s'], | ||
| )] | ||
| ?string $shell = null, | ||
| #[ConsoleArgument( | ||
| description: 'Skip confirmation prompts', | ||
| aliases: ['-f'], | ||
| )] | ||
| bool $force = false, | ||
| ): ExitCode { | ||
| $shellEnum = $this->resolveShell($shell); | ||
|
|
||
| if ($shellEnum === null) { | ||
| $this->console->error('Could not determine shell. Please specify with --shell=zsh or --shell=bash'); | ||
|
|
||
| return ExitCode::ERROR; | ||
| } | ||
|
|
||
| $sourcePath = $this->getSourcePath($shellEnum); | ||
| $targetDir = $shellEnum->getCompletionsDirectory(); | ||
| $targetPath = $shellEnum->getInstalledCompletionPath(); | ||
|
|
||
| if (! Filesystem\is_file($sourcePath)) { | ||
| $this->console->error("Completion script not found: {$sourcePath}"); | ||
|
|
||
| return ExitCode::ERROR; | ||
| } | ||
|
|
||
| if (! $force) { | ||
| $this->console->info("Installing {$shellEnum->value} completions"); | ||
| $this->console->keyValue('Source', $sourcePath); | ||
| $this->console->keyValue('Target', $targetPath); | ||
| $this->console->writeln(); | ||
|
|
||
| if (! $this->console->confirm('Proceed with installation?', default: true)) { | ||
| $this->console->warning('Installation cancelled.'); | ||
|
|
||
| return ExitCode::CANCELLED; | ||
| } | ||
| } | ||
|
|
||
| if (! Filesystem\is_directory($targetDir)) { | ||
xHeaven marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Filesystem\create_directory($targetDir); | ||
| $this->console->success("Created directory: {$targetDir}"); | ||
| } | ||
|
|
||
| if (Filesystem\is_file($targetPath)) { | ||
| if (! $force && ! $this->console->confirm('Completion file already exists. Overwrite?', default: false)) { | ||
| $this->console->warning('Installation cancelled.'); | ||
|
|
||
| return ExitCode::CANCELLED; | ||
| } | ||
| } | ||
|
|
||
| Filesystem\copy_file($sourcePath, $targetPath, overwrite: true); | ||
| $this->console->success("Installed completion script to: {$targetPath}"); | ||
|
|
||
| $this->console->writeln(); | ||
| $this->console->info('Next steps:'); | ||
| $this->console->instructions($shellEnum->getPostInstallInstructions()); | ||
|
|
||
| return ExitCode::SUCCESS; | ||
| } | ||
|
|
||
| private function resolveShell(?string $shell): ?Shell | ||
| { | ||
| if ($shell !== null) { | ||
| return Shell::tryFrom(strtolower($shell)); | ||
| } | ||
|
|
||
| $detected = Shell::detect(); | ||
|
|
||
| if ($this->console->supportsPrompting()) { | ||
| $options = []; | ||
|
|
||
| foreach (Shell::cases() as $shellCase) { | ||
| $label = $shellCase->value; | ||
|
|
||
| if ($shellCase === $detected) { | ||
| $label .= ' (current)'; | ||
| } | ||
|
|
||
| $options[$shellCase->value] = $label; | ||
| } | ||
|
|
||
| $choice = $this->console->ask( | ||
| question: 'Which shell do you want to install completions for?', | ||
| options: $options, | ||
| default: $detected?->value, | ||
| ); | ||
|
|
||
| return Shell::tryFrom($choice); | ||
| } | ||
|
|
||
| return $detected; | ||
| } | ||
|
|
||
| private function getSourcePath(Shell $shell): string | ||
| { | ||
| return Path::canonicalize( | ||
| path(__DIR__, '..', $shell->getSourceFilename())->toString(), | ||
| ); | ||
| } | ||
| } | ||
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,94 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Console\Commands; | ||
|
|
||
| use Symfony\Component\Filesystem\Path; | ||
| use Tempest\Console\Console; | ||
| use Tempest\Console\ConsoleArgument; | ||
| use Tempest\Console\ConsoleCommand; | ||
| use Tempest\Console\Enums\Shell; | ||
| use Tempest\Console\ExitCode; | ||
| use Tempest\Support\Filesystem; | ||
|
|
||
| use function Tempest\Support\path; | ||
|
|
||
| final readonly class CompletionShowCommand | ||
| { | ||
| public function __construct( | ||
| private Console $console, | ||
| ) {} | ||
|
|
||
| #[ConsoleCommand( | ||
| name: 'completion:show', | ||
| description: 'Output the shell completion script to stdout', | ||
| )] | ||
| public function __invoke( | ||
| #[ConsoleArgument( | ||
| description: 'The shell to show completions for (zsh, bash)', | ||
| aliases: ['-s'], | ||
| )] | ||
| ?string $shell = null, | ||
| ): ExitCode { | ||
| $shellEnum = $this->resolveShell($shell); | ||
|
|
||
| if ($shellEnum === null) { | ||
| $this->console->error('Could not determine shell. Please specify with --shell=zsh or --shell=bash'); | ||
|
|
||
| return ExitCode::ERROR; | ||
| } | ||
|
|
||
| $sourcePath = $this->getSourcePath($shellEnum); | ||
|
|
||
| if (! Filesystem\is_file($sourcePath)) { | ||
| $this->console->error("Completion script not found: {$sourcePath}"); | ||
|
|
||
| return ExitCode::ERROR; | ||
| } | ||
|
|
||
| $this->console->writeRaw(Filesystem\read_file($sourcePath)); | ||
|
|
||
| return ExitCode::SUCCESS; | ||
| } | ||
|
|
||
| private function resolveShell(?string $shell): ?Shell | ||
| { | ||
| if ($shell !== null) { | ||
| return Shell::tryFrom(strtolower($shell)); | ||
| } | ||
|
|
||
| $detected = Shell::detect(); | ||
|
|
||
| if ($this->console->supportsPrompting()) { | ||
| $options = []; | ||
|
|
||
| foreach (Shell::cases() as $shellCase) { | ||
| $label = $shellCase->value; | ||
|
|
||
| if ($shellCase === $detected) { | ||
| $label .= ' (current)'; | ||
| } | ||
|
|
||
| $options[$shellCase->value] = $label; | ||
| } | ||
|
|
||
| $choice = $this->console->ask( | ||
| question: 'Which shell completion script do you want to see?', | ||
| options: $options, | ||
| default: $detected?->value, | ||
| ); | ||
|
|
||
| return Shell::tryFrom($choice); | ||
| } | ||
|
|
||
| return $detected; | ||
| } | ||
|
|
||
| private function getSourcePath(Shell $shell): string | ||
| { | ||
| return Path::canonicalize( | ||
| path(__DIR__, '..', $shell->getSourceFilename())->toString(), | ||
| ); | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
packages/console/src/Commands/CompletionUninstallCommand.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,107 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Console\Commands; | ||
|
|
||
| use Tempest\Console\Console; | ||
| use Tempest\Console\ConsoleArgument; | ||
| use Tempest\Console\ConsoleCommand; | ||
| use Tempest\Console\Enums\Shell; | ||
| use Tempest\Console\ExitCode; | ||
| use Tempest\Support\Filesystem; | ||
|
|
||
| final readonly class CompletionUninstallCommand | ||
| { | ||
| public function __construct( | ||
| private Console $console, | ||
| ) {} | ||
|
|
||
| #[ConsoleCommand( | ||
| name: 'completion:uninstall', | ||
| description: 'Uninstall shell completion for Tempest', | ||
| )] | ||
| public function __invoke( | ||
| #[ConsoleArgument( | ||
| description: 'The shell to uninstall completions for (zsh, bash)', | ||
| aliases: ['-s'], | ||
| )] | ||
| ?string $shell = null, | ||
| #[ConsoleArgument( | ||
| description: 'Skip confirmation prompts', | ||
| aliases: ['-f'], | ||
| )] | ||
| bool $force = false, | ||
| ): ExitCode { | ||
| $shellEnum = $this->resolveShell($shell); | ||
|
|
||
| if ($shellEnum === null) { | ||
| $this->console->error('Could not determine shell. Please specify with --shell=zsh or --shell=bash'); | ||
|
|
||
| return ExitCode::ERROR; | ||
| } | ||
|
|
||
| $targetPath = $shellEnum->getInstalledCompletionPath(); | ||
|
|
||
| if (! Filesystem\is_file($targetPath)) { | ||
| $this->console->warning("Completion file not found: {$targetPath}"); | ||
| $this->console->info('Nothing to uninstall.'); | ||
|
|
||
| return ExitCode::SUCCESS; | ||
| } | ||
|
|
||
| if (! $force) { | ||
| $this->console->info("Uninstalling {$shellEnum->value} completions"); | ||
| $this->console->keyValue('File', $targetPath); | ||
| $this->console->writeln(); | ||
|
|
||
| if (! $this->console->confirm('Proceed with uninstallation?', default: true)) { | ||
| $this->console->warning('Uninstallation cancelled.'); | ||
|
|
||
| return ExitCode::CANCELLED; | ||
| } | ||
| } | ||
|
|
||
| Filesystem\delete_file($targetPath); | ||
| $this->console->success("Removed completion script: {$targetPath}"); | ||
|
|
||
| $this->console->writeln(); | ||
| $this->console->info('Remember to remove any related lines from your shell configuration:'); | ||
| $this->console->keyValue('Config file', $shellEnum->getRcFile()); | ||
|
|
||
| return ExitCode::SUCCESS; | ||
| } | ||
|
|
||
| private function resolveShell(?string $shell): ?Shell | ||
| { | ||
| if ($shell !== null) { | ||
| return Shell::tryFrom(strtolower($shell)); | ||
| } | ||
|
|
||
| $detected = Shell::detect(); | ||
|
|
||
| if ($this->console->supportsPrompting()) { | ||
| $options = []; | ||
|
|
||
| foreach (Shell::cases() as $shellCase) { | ||
| $label = $shellCase->value; | ||
|
|
||
| if ($shellCase === $detected) { | ||
| $label .= ' (current)'; | ||
| } | ||
|
|
||
| $options[$shellCase->value] = $label; | ||
| } | ||
|
|
||
| $choice = $this->console->ask( | ||
| question: 'Which shell completions do you want to uninstall?', | ||
| options: $options, | ||
| default: $detected?->value, | ||
| ); | ||
|
|
||
| return Shell::tryFrom($choice); | ||
| } | ||
|
|
||
| return $detected; | ||
| } | ||
| } |
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.