From b19eb3822eecc0a0120ce26de7a3520c9dc5e93a Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Mon, 11 Jul 2022 11:00:03 +0200 Subject: [PATCH] [Console] Be explicit about the completion API version Using the application version is wrong, especially for standalone apps like PHPStan. The completion API is not bound to change on a PHPStan version upgrade, but instead only when something in symfony/console changes. --- .../Console/Command/CompleteCommand.php | 21 +++++++++++-------- .../Console/Command/DumpCompletionCommand.php | 2 +- .../Console/Resources/completion.bash | 2 +- .../Console/Resources/completion.fish | 2 +- .../Console/Tests/Fixtures/application_1.json | 13 ++++++++++-- .../Console/Tests/Fixtures/application_1.xml | 8 +++++-- .../Console/Tests/Fixtures/application_2.json | 13 ++++++++++-- .../Console/Tests/Fixtures/application_2.xml | 8 +++++-- 8 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Component/Console/Command/CompleteCommand.php b/src/Symfony/Component/Console/Command/CompleteCommand.php index 404be465025a4..8c8d62f860c6c 100644 --- a/src/Symfony/Component/Console/Command/CompleteCommand.php +++ b/src/Symfony/Component/Console/Command/CompleteCommand.php @@ -31,6 +31,8 @@ #[AsCommand(name: '|_complete', description: 'Internal command to provide shell completion suggestions')] final class CompleteCommand extends Command { + public const COMPLETION_API_VERSION = '1'; + /** * @deprecated since Symfony 6.1 */ @@ -65,7 +67,8 @@ protected function configure(): void ->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type ("'.implode('", "', array_keys($this->completionOutputs)).'")') ->addOption('input', 'i', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'An array of input tokens (e.g. COMP_WORDS or argv)') ->addOption('current', 'c', InputOption::VALUE_REQUIRED, 'The index of the "input" array that the cursor is in (e.g. COMP_CWORD)') - ->addOption('symfony', 'S', InputOption::VALUE_REQUIRED, 'The version of the completion script') + ->addOption('api-version', 'a', InputOption::VALUE_REQUIRED, 'The API version of the completion script') + ->addOption('symfony', 'S', InputOption::VALUE_REQUIRED, 'deprecated') ; } @@ -77,16 +80,16 @@ protected function initialize(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output): int { try { - // uncomment when a bugfix or BC break has been introduced in the shell completion scripts - //$version = $input->getOption('symfony'); - //if ($version && version_compare($version, 'x.y', '>=')) { - // $message = sprintf('Completion script version is not supported ("%s" given, ">=x.y" required).', $version); - // $this->log($message); + // "symfony" must be kept for compat with the shell scripts generated by Symfony Console 5.4 - 6.1 + $version = $input->getOption('symfony') ? '1' : $input->getOption('api-version'); + if ($version && version_compare($version, self::COMPLETION_API_VERSION, '>=')) { + $message = sprintf('Completion script version is not supported ("%s" given, ">=%s" required).', $version, self::COMPLETION_API_VERSION); + $this->log($message); - // $output->writeln($message.' Install the Symfony completion script again by using the "completion" command.'); + $output->writeln($message.' Install the Symfony completion script again by using the "completion" command.'); - // return 126; - //} + return 126; + } $shell = $input->getOption('shell'); if (!$shell) { diff --git a/src/Symfony/Component/Console/Command/DumpCompletionCommand.php b/src/Symfony/Component/Console/Command/DumpCompletionCommand.php index 5f42db3194060..090f2601cd21c 100644 --- a/src/Symfony/Component/Console/Command/DumpCompletionCommand.php +++ b/src/Symfony/Component/Console/Command/DumpCompletionCommand.php @@ -107,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return self::INVALID; } - $output->write(str_replace(['{{ COMMAND_NAME }}', '{{ VERSION }}'], [$commandName, $this->getApplication()->getVersion()], file_get_contents($completionFile))); + $output->write(str_replace(['{{ COMMAND_NAME }}', '{{ VERSION }}'], [$commandName, CompleteCommand::COMPLETION_API_VERSION], file_get_contents($completionFile))); return self::SUCCESS; } diff --git a/src/Symfony/Component/Console/Resources/completion.bash b/src/Symfony/Component/Console/Resources/completion.bash index c5e89c3c282ab..baa988b7b4393 100644 --- a/src/Symfony/Component/Console/Resources/completion.bash +++ b/src/Symfony/Component/Console/Resources/completion.bash @@ -22,7 +22,7 @@ _sf_{{ COMMAND_NAME }}() { local cur prev words cword _get_comp_words_by_ref -n := cur prev words cword - local completecmd=("$sf_cmd" "_complete" "-sbash" "-c$cword" "-S{{ VERSION }}") + local completecmd=("$sf_cmd" "_complete" "-sbash" "-c$cword" "-a{{ VERSION }}") for w in ${words[@]}; do w=$(printf -- '%b' "$w") # remove quotes from typed values diff --git a/src/Symfony/Component/Console/Resources/completion.fish b/src/Symfony/Component/Console/Resources/completion.fish index 680d180e03843..936b01d6c87f0 100644 --- a/src/Symfony/Component/Console/Resources/completion.fish +++ b/src/Symfony/Component/Console/Resources/completion.fish @@ -9,7 +9,7 @@ function _sf_{{ COMMAND_NAME }} set sf_cmd (commandline -o) set c (count (commandline -oc)) - set completecmd "$sf_cmd[1]" "_complete" "-sfish" "-S{{ VERSION }}" + set completecmd "$sf_cmd[1]" "_complete" "-sfish" "-a{{ VERSION }}" for i in $sf_cmd if [ $i != "" ] diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_1.json b/src/Symfony/Component/Console/Tests/Fixtures/application_1.json index db3c250fff003..0d655c39a7e82 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_1.json +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_1.json @@ -4,7 +4,7 @@ "name": "_complete", "hidden": true, "usage": [ - "_complete [-s|--shell SHELL] [-i|--input INPUT] [-c|--current CURRENT] [-S|--symfony SYMFONY]" + "_complete [-s|--shell SHELL] [-i|--input INPUT] [-c|--current CURRENT] [-a|--api-version API-VERSION] [-S|--symfony SYMFONY]" ], "description": "Internal command to provide shell completion suggestions", "help": "Internal command to provide shell completion suggestions", @@ -17,7 +17,7 @@ "accept_value": true, "is_value_required": true, "is_multiple": false, - "description": "The version of the completion script", + "description": "deprecated", "default": null }, "help": { @@ -109,6 +109,15 @@ "is_multiple": true, "description": "An array of input tokens (e.g. COMP_WORDS or argv)", "default": [] + }, + "api-version": { + "name": "--api-version", + "shortcut": "-a", + "accept_value": true, + "is_value_required": true, + "is_multiple": false, + "description": "The API version of the completion script", + "default": null } } } diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml index 9010a68a17a36..617786cdae194 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml @@ -3,7 +3,7 @@