diff --git a/src/CompletionCommand.php b/src/CompletionCommand.php index 88108b5..b51666a 100644 --- a/src/CompletionCommand.php +++ b/src/CompletionCommand.php @@ -53,10 +53,19 @@ protected function execute(InputInterface $input, OutputInterface $output) $program = $argv[0]; $factory = new HookFactory(); + $alias = $input->getOption('program'); + $multiple = (bool)$input->getOption('multiple'); + + // When completing for multiple apps having absolute path in the alias doesn't make sense. + if (!$alias && $multiple) { + $alias = basename($program); + } + $hook = $factory->generateHook( $input->getOption('shell-type') ?: $this->getShellType(), $program, - $input->getOption('program') + $alias, + $multiple ); $output->write($hook, true); @@ -118,6 +127,12 @@ protected function createDefinition() InputOption::VALUE_REQUIRED, "Program name that should trigger completion\n(defaults to the absolute application path)." ), + new InputOption( + 'multiple', + 'm', + InputOption::VALUE_NONE, + "Generated hook can be used for multiple applications." + ), new InputOption( 'shell-type', null, diff --git a/src/HookFactory.php b/src/HookFactory.php index 79bcbab..138094e 100644 --- a/src/HookFactory.php +++ b/src/HookFactory.php @@ -114,9 +114,11 @@ public static function getShellTypes() * @param string $type - a key from self::$hooks * @param string $programPath * @param string $programName + * @param bool $multiple + * * @return string */ - public function generateHook($type, $programPath, $programName = null) + public function generateHook($type, $programPath, $programName = null, $multiple = false) { if (!isset(self::$hooks[$type])) { throw new \RuntimeException(sprintf( @@ -129,6 +131,12 @@ public function generateHook($type, $programPath, $programName = null) // Use the program path if an alias/name is not given $programName = $programName ?: $programPath; + if ($multiple) { + $completionCommand = '${1} _completion'; + } else { + $completionCommand = $programPath . ' _completion'; + } + return str_replace( array( '%%function_name%%', @@ -140,7 +148,7 @@ public function generateHook($type, $programPath, $programName = null) $this->generateFunctionName($programPath, $programName), $programName, $programPath, - "$programPath _completion" + $completionCommand ), $this->stripComments(self::$hooks[$type]) ); diff --git a/tests/Stecman/Component/Symfony/Console/BashCompletion/HookFactoryTest.php b/tests/Stecman/Component/Symfony/Console/BashCompletion/HookFactoryTest.php index 7da17ac..0251722 100644 --- a/tests/Stecman/Component/Symfony/Console/BashCompletion/HookFactoryTest.php +++ b/tests/Stecman/Component/Symfony/Console/BashCompletion/HookFactoryTest.php @@ -16,27 +16,42 @@ protected function setUp() $this->factory = new HookFactory(); } - public function testBashSyntax() + /** + * @dataProvider generateHookDataProvider + */ + public function testBashSyntax($programPath, $programName, $multiple) { if ($this->hasProgram('bash')) { - $script = $this->factory->generateHook('bash', '/path/to/myprogram', 'myprogram'); + $script = $this->factory->generateHook('bash', $programPath, $programName, $multiple); $this->assertSyntaxIsValid($script, 'bash -n', 'BASH hook'); - } else { $this->markTestSkipped("Couldn't detect BASH program to run hook syntax check"); } } - public function testZshSyntax() + /** + * @dataProvider generateHookDataProvider + */ + public function testZshSyntax($programPath, $programName, $multiple) { if ($this->hasProgram('zsh')) { - $script = $this->factory->generateHook('zsh', '/path/to/myprogram', 'myprogram'); + $script = $this->factory->generateHook('zsh', $programPath, $programName, $multiple); $this->assertSyntaxIsValid($script, 'zsh -n', 'ZSH hook'); } else { $this->markTestSkipped("Couldn't detect ZSH program to run hook syntax check"); } } + public function generateHookDataProvider() + { + return array( + array('/path/to/myprogram', null, false), + array('/path/to/myprogram', null, true), + array('/path/to/myprogram', 'myprogram', false), + array('/path/to/myprogram', 'myprogram', true), + ); + } + protected function hasProgram($programName) { exec(sprintf(