DumpCompletionCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Completion\CompletionInput;
  12. use Symfony\Component\Console\Completion\CompletionSuggestions;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Process\Process;
  19. /**
  20. * Dumps the completion script for the current shell.
  21. *
  22. * @author Wouter de Jong <wouter@wouterj.nl>
  23. */
  24. final class DumpCompletionCommand extends Command
  25. {
  26. protected static $defaultName = 'completion';
  27. protected static $defaultDescription = 'Dump the shell completion script';
  28. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  29. {
  30. if ($input->mustSuggestArgumentValuesFor('shell')) {
  31. $suggestions->suggestValues($this->getSupportedShells());
  32. }
  33. }
  34. protected function configure()
  35. {
  36. $fullCommand = $_SERVER['PHP_SELF'];
  37. $commandName = basename($fullCommand);
  38. $fullCommand = @realpath($fullCommand) ?: $fullCommand;
  39. $this
  40. ->setHelp(<<<EOH
  41. The <info>%command.name%</> command dumps the shell completion script required
  42. to use shell autocompletion (currently only bash completion is supported).
  43. <comment>Static installation
  44. -------------------</>
  45. Dump the script to a global completion file and restart your shell:
  46. <info>%command.full_name% bash | sudo tee /etc/bash_completion.d/{$commandName}</>
  47. Or dump the script to a local file and source it:
  48. <info>%command.full_name% bash > completion.sh</>
  49. <comment># source the file whenever you use the project</>
  50. <info>source completion.sh</>
  51. <comment># or add this line at the end of your "~/.bashrc" file:</>
  52. <info>source /path/to/completion.sh</>
  53. <comment>Dynamic installation
  54. --------------------</>
  55. Add this to the end of your shell configuration file (e.g. <info>"~/.bashrc"</>):
  56. <info>eval "$({$fullCommand} completion bash)"</>
  57. EOH
  58. )
  59. ->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')
  60. ->addOption('debug', null, InputOption::VALUE_NONE, 'Tail the completion debug log')
  61. ;
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output): int
  64. {
  65. $commandName = basename($_SERVER['argv'][0]);
  66. if ($input->getOption('debug')) {
  67. $this->tailDebugLog($commandName, $output);
  68. return self::SUCCESS;
  69. }
  70. $shell = $input->getArgument('shell') ?? self::guessShell();
  71. $completionFile = __DIR__.'/../Resources/completion.'.$shell;
  72. if (!file_exists($completionFile)) {
  73. $supportedShells = $this->getSupportedShells();
  74. ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)
  75. ->writeln(sprintf('<error>Detected shell "%s", which is not supported by Symfony shell completion (supported shells: "%s").</>', $shell, implode('", "', $supportedShells)));
  76. return self::INVALID;
  77. }
  78. $output->write(str_replace(['{{ COMMAND_NAME }}', '{{ VERSION }}'], [$commandName, $this->getApplication()->getVersion()], file_get_contents($completionFile)));
  79. return self::SUCCESS;
  80. }
  81. private static function guessShell(): string
  82. {
  83. return basename($_SERVER['SHELL'] ?? '');
  84. }
  85. private function tailDebugLog(string $commandName, OutputInterface $output): void
  86. {
  87. $debugFile = sys_get_temp_dir().'/sf_'.$commandName.'.log';
  88. if (!file_exists($debugFile)) {
  89. touch($debugFile);
  90. }
  91. $process = new Process(['tail', '-f', $debugFile], null, null, null, 0);
  92. $process->run(function (string $type, string $line) use ($output): void {
  93. $output->write($line);
  94. });
  95. }
  96. /**
  97. * @return string[]
  98. */
  99. private function getSupportedShells(): array
  100. {
  101. return array_map(function ($f) {
  102. return pathinfo($f, \PATHINFO_EXTENSION);
  103. }, glob(__DIR__.'/../Resources/completion.*'));
  104. }
  105. }