|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Console\Commands; |
| 6 | + |
| 7 | +use Symfony\Component\Filesystem\Path; |
| 8 | +use Tempest\Console\Actions\ResolveShell; |
| 9 | +use Tempest\Console\Console; |
| 10 | +use Tempest\Console\ConsoleArgument; |
| 11 | +use Tempest\Console\ConsoleCommand; |
| 12 | +use Tempest\Console\Enums\Shell; |
| 13 | +use Tempest\Console\ExitCode; |
| 14 | +use Tempest\Support\Filesystem; |
| 15 | + |
| 16 | +use function Tempest\Support\path; |
| 17 | + |
| 18 | +final readonly class CompletionInstallCommand |
| 19 | +{ |
| 20 | + public function __construct( |
| 21 | + private Console $console, |
| 22 | + private ResolveShell $resolveShell, |
| 23 | + ) {} |
| 24 | + |
| 25 | + #[ConsoleCommand( |
| 26 | + name: 'completion:install', |
| 27 | + description: 'Install shell completion for Tempest', |
| 28 | + )] |
| 29 | + public function __invoke( |
| 30 | + #[ConsoleArgument( |
| 31 | + description: 'The shell to install completions for (zsh, bash)', |
| 32 | + aliases: ['-s'], |
| 33 | + )] |
| 34 | + ?Shell $shell = null, |
| 35 | + #[ConsoleArgument( |
| 36 | + description: 'Skip confirmation prompts', |
| 37 | + aliases: ['-f'], |
| 38 | + )] |
| 39 | + bool $force = false, |
| 40 | + ): ExitCode { |
| 41 | + $shell ??= ($this->resolveShell)('Which shell do you want to install completions for?'); |
| 42 | + |
| 43 | + if ($shell === null) { |
| 44 | + $this->console->error('Could not detect shell. Please specify one using the --shell option. Possible values are: zsh, bash.'); |
| 45 | + |
| 46 | + return ExitCode::ERROR; |
| 47 | + } |
| 48 | + |
| 49 | + $sourcePath = $this->getSourcePath($shell); |
| 50 | + $targetDir = $shell->getCompletionsDirectory(); |
| 51 | + $targetPath = $shell->getInstalledCompletionPath(); |
| 52 | + |
| 53 | + if (! Filesystem\is_file($sourcePath)) { |
| 54 | + $this->console->error("Completion script not found: {$sourcePath}"); |
| 55 | + |
| 56 | + return ExitCode::ERROR; |
| 57 | + } |
| 58 | + |
| 59 | + if (! $force) { |
| 60 | + $this->console->info("Installing {$shell->value} completions"); |
| 61 | + $this->console->keyValue('Source', $sourcePath); |
| 62 | + $this->console->keyValue('Target', $targetPath); |
| 63 | + $this->console->writeln(); |
| 64 | + |
| 65 | + if (! $this->console->confirm('Proceed with installation?', default: true)) { |
| 66 | + $this->console->warning('Installation cancelled.'); |
| 67 | + |
| 68 | + return ExitCode::CANCELLED; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + Filesystem\ensure_directory_exists($targetDir); |
| 73 | + |
| 74 | + if (Filesystem\is_file($targetPath)) { |
| 75 | + if (! $force && ! $this->console->confirm('Completion file already exists. Overwrite?', default: false)) { |
| 76 | + $this->console->warning('Installation cancelled.'); |
| 77 | + |
| 78 | + return ExitCode::CANCELLED; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + Filesystem\copy_file($sourcePath, $targetPath, overwrite: true); |
| 83 | + $this->console->success("Installed completion script to: {$targetPath}"); |
| 84 | + |
| 85 | + $this->console->writeln(); |
| 86 | + $this->console->info('Next steps:'); |
| 87 | + $this->console->instructions($shell->getPostInstallInstructions()); |
| 88 | + |
| 89 | + return ExitCode::SUCCESS; |
| 90 | + } |
| 91 | + |
| 92 | + private function getSourcePath(Shell $shell): string |
| 93 | + { |
| 94 | + return Path::canonicalize( |
| 95 | + path(__DIR__, '..', $shell->getSourceFilename())->toString(), |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments