| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Console\Command;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
15 |
use Symfony\Component\Console\Input\InputArgument;
|
|
|
16 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
17 |
use Symfony\Component\Console\Input\InputOption;
|
|
|
18 |
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
|
|
19 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
20 |
use Symfony\Component\Process\Process;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Dumps the completion script for the current shell.
|
|
|
24 |
*
|
|
|
25 |
* @author Wouter de Jong <wouter@wouterj.nl>
|
|
|
26 |
*/
|
|
|
27 |
#[AsCommand(name: 'completion', description: 'Dump the shell completion script')]
|
|
|
28 |
final class DumpCompletionCommand extends Command
|
|
|
29 |
{
|
|
|
30 |
/**
|
|
|
31 |
* @deprecated since Symfony 6.1
|
|
|
32 |
*/
|
|
|
33 |
protected static $defaultName = 'completion';
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @deprecated since Symfony 6.1
|
|
|
37 |
*/
|
|
|
38 |
protected static $defaultDescription = 'Dump the shell completion script';
|
|
|
39 |
|
|
|
40 |
private array $supportedShells;
|
|
|
41 |
|
|
|
42 |
protected function configure()
|
|
|
43 |
{
|
|
|
44 |
$fullCommand = $_SERVER['PHP_SELF'];
|
|
|
45 |
$commandName = basename($fullCommand);
|
|
|
46 |
$fullCommand = @realpath($fullCommand) ?: $fullCommand;
|
|
|
47 |
|
|
|
48 |
$shell = $this->guessShell();
|
|
|
49 |
[$rcFile, $completionFile] = match ($shell) {
|
|
|
50 |
'fish' => ['~/.config/fish/config.fish', "/etc/fish/completions/$commandName.fish"],
|
| 1663 |
lars |
51 |
'zsh' => ['~/.zshrc', '$fpath[1]/_'.$commandName],
|
| 148 |
lars |
52 |
default => ['~/.bashrc', "/etc/bash_completion.d/$commandName"],
|
|
|
53 |
};
|
|
|
54 |
|
| 991 |
lars |
55 |
$supportedShells = implode(', ', $this->getSupportedShells());
|
|
|
56 |
|
| 148 |
lars |
57 |
$this
|
|
|
58 |
->setHelp(<<<EOH
|
|
|
59 |
The <info>%command.name%</> command dumps the shell completion script required
|
| 991 |
lars |
60 |
to use shell autocompletion (currently, {$supportedShells} completion are supported).
|
| 148 |
lars |
61 |
|
|
|
62 |
<comment>Static installation
|
|
|
63 |
-------------------</>
|
|
|
64 |
|
|
|
65 |
Dump the script to a global completion file and restart your shell:
|
|
|
66 |
|
|
|
67 |
<info>%command.full_name% {$shell} | sudo tee {$completionFile}</>
|
|
|
68 |
|
|
|
69 |
Or dump the script to a local file and source it:
|
|
|
70 |
|
|
|
71 |
<info>%command.full_name% {$shell} > completion.sh</>
|
|
|
72 |
|
|
|
73 |
<comment># source the file whenever you use the project</>
|
|
|
74 |
<info>source completion.sh</>
|
|
|
75 |
|
|
|
76 |
<comment># or add this line at the end of your "{$rcFile}" file:</>
|
|
|
77 |
<info>source /path/to/completion.sh</>
|
|
|
78 |
|
|
|
79 |
<comment>Dynamic installation
|
|
|
80 |
--------------------</>
|
|
|
81 |
|
|
|
82 |
Add this to the end of your shell configuration file (e.g. <info>"{$rcFile}"</>):
|
|
|
83 |
|
|
|
84 |
<info>eval "$({$fullCommand} completion {$shell})"</>
|
|
|
85 |
EOH
|
|
|
86 |
)
|
|
|
87 |
->addArgument('shell', InputArgument::OPTIONAL, 'The shell type (e.g. "bash"), the value of the "$SHELL" env var will be used if this is not given', null, $this->getSupportedShells(...))
|
|
|
88 |
->addOption('debug', null, InputOption::VALUE_NONE, 'Tail the completion debug log')
|
|
|
89 |
;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
93 |
{
|
|
|
94 |
$commandName = basename($_SERVER['argv'][0]);
|
|
|
95 |
|
|
|
96 |
if ($input->getOption('debug')) {
|
|
|
97 |
$this->tailDebugLog($commandName, $output);
|
|
|
98 |
|
|
|
99 |
return self::SUCCESS;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
$shell = $input->getArgument('shell') ?? self::guessShell();
|
|
|
103 |
$completionFile = __DIR__.'/../Resources/completion.'.$shell;
|
|
|
104 |
if (!file_exists($completionFile)) {
|
|
|
105 |
$supportedShells = $this->getSupportedShells();
|
|
|
106 |
|
|
|
107 |
if ($output instanceof ConsoleOutputInterface) {
|
|
|
108 |
$output = $output->getErrorOutput();
|
|
|
109 |
}
|
|
|
110 |
if ($shell) {
|
|
|
111 |
$output->writeln(sprintf('<error>Detected shell "%s", which is not supported by Symfony shell completion (supported shells: "%s").</>', $shell, implode('", "', $supportedShells)));
|
|
|
112 |
} else {
|
|
|
113 |
$output->writeln(sprintf('<error>Shell not detected, Symfony shell completion only supports "%s").</>', implode('", "', $supportedShells)));
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return self::INVALID;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$output->write(str_replace(['{{ COMMAND_NAME }}', '{{ VERSION }}'], [$commandName, CompleteCommand::COMPLETION_API_VERSION], file_get_contents($completionFile)));
|
|
|
120 |
|
|
|
121 |
return self::SUCCESS;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
private static function guessShell(): string
|
|
|
125 |
{
|
|
|
126 |
return basename($_SERVER['SHELL'] ?? '');
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
private function tailDebugLog(string $commandName, OutputInterface $output): void
|
|
|
130 |
{
|
|
|
131 |
$debugFile = sys_get_temp_dir().'/sf_'.$commandName.'.log';
|
|
|
132 |
if (!file_exists($debugFile)) {
|
|
|
133 |
touch($debugFile);
|
|
|
134 |
}
|
|
|
135 |
$process = new Process(['tail', '-f', $debugFile], null, null, null, 0);
|
|
|
136 |
$process->run(function (string $type, string $line) use ($output): void {
|
|
|
137 |
$output->write($line);
|
|
|
138 |
});
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* @return string[]
|
|
|
143 |
*/
|
|
|
144 |
private function getSupportedShells(): array
|
|
|
145 |
{
|
| 1663 |
lars |
146 |
if (isset($this->supportedShells)) {
|
|
|
147 |
return $this->supportedShells;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
$shells = [];
|
|
|
151 |
|
|
|
152 |
foreach (new \DirectoryIterator(__DIR__.'/../Resources/') as $file) {
|
|
|
153 |
if (str_starts_with($file->getBasename(), 'completion.') && $file->isFile()) {
|
|
|
154 |
$shells[] = $file->getExtension();
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
sort($shells);
|
|
|
158 |
|
|
|
159 |
return $this->supportedShells = $shells;
|
| 148 |
lars |
160 |
}
|
|
|
161 |
}
|