DumpCompletionCommand.php 5.3 KB

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