Skip to content

Commit f7fd60b

Browse files
committed
Merge pull request #48 from aik099:44-support-completion-wrapper-for-multi-app-completion
2 parents 35659d2 + 1328f12 commit f7fd60b

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

src/CompletionCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
5353
$program = $argv[0];
5454

5555
$factory = new HookFactory();
56+
$alias = $input->getOption('program');
57+
$multiple = (bool)$input->getOption('multiple');
58+
59+
// When completing for multiple apps having absolute path in the alias doesn't make sense.
60+
if (!$alias && $multiple) {
61+
$alias = basename($program);
62+
}
63+
5664
$hook = $factory->generateHook(
5765
$input->getOption('shell-type') ?: $this->getShellType(),
5866
$program,
59-
$input->getOption('program')
67+
$alias,
68+
$multiple
6069
);
6170

6271
$output->write($hook, true);
@@ -118,6 +127,12 @@ protected function createDefinition()
118127
InputOption::VALUE_REQUIRED,
119128
"Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
120129
),
130+
new InputOption(
131+
'multiple',
132+
'm',
133+
InputOption::VALUE_NONE,
134+
"Generated hook can be used for multiple applications."
135+
),
121136
new InputOption(
122137
'shell-type',
123138
null,

src/HookFactory.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ public static function getShellTypes()
114114
* @param string $type - a key from self::$hooks
115115
* @param string $programPath
116116
* @param string $programName
117+
* @param bool $multiple
118+
*
117119
* @return string
118120
*/
119-
public function generateHook($type, $programPath, $programName = null)
121+
public function generateHook($type, $programPath, $programName = null, $multiple = false)
120122
{
121123
if (!isset(self::$hooks[$type])) {
122124
throw new \RuntimeException(sprintf(
@@ -129,6 +131,12 @@ public function generateHook($type, $programPath, $programName = null)
129131
// Use the program path if an alias/name is not given
130132
$programName = $programName ?: $programPath;
131133

134+
if ($multiple) {
135+
$completionCommand = '$1 _completion';
136+
} else {
137+
$completionCommand = $programPath . ' _completion';
138+
}
139+
132140
return str_replace(
133141
array(
134142
'%%function_name%%',
@@ -140,7 +148,7 @@ public function generateHook($type, $programPath, $programName = null)
140148
$this->generateFunctionName($programPath, $programName),
141149
$programName,
142150
$programPath,
143-
"$programPath _completion"
151+
$completionCommand
144152
),
145153
$this->stripComments(self::$hooks[$type])
146154
);

tests/Stecman/Component/Symfony/Console/BashCompletion/HookFactoryTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,42 @@ protected function setUp()
1616
$this->factory = new HookFactory();
1717
}
1818

19-
public function testBashSyntax()
19+
/**
20+
* @dataProvider generateHookDataProvider
21+
*/
22+
public function testBashSyntax($programPath, $programName, $multiple)
2023
{
2124
if ($this->hasProgram('bash')) {
22-
$script = $this->factory->generateHook('bash', '/path/to/myprogram', 'myprogram');
25+
$script = $this->factory->generateHook('bash', $programPath, $programName, $multiple);
2326
$this->assertSyntaxIsValid($script, 'bash -n', 'BASH hook');
24-
2527
} else {
2628
$this->markTestSkipped("Couldn't detect BASH program to run hook syntax check");
2729
}
2830
}
2931

30-
public function testZshSyntax()
32+
/**
33+
* @dataProvider generateHookDataProvider
34+
*/
35+
public function testZshSyntax($programPath, $programName, $multiple)
3136
{
3237
if ($this->hasProgram('zsh')) {
33-
$script = $this->factory->generateHook('zsh', '/path/to/myprogram', 'myprogram');
38+
$script = $this->factory->generateHook('zsh', $programPath, $programName, $multiple);
3439
$this->assertSyntaxIsValid($script, 'zsh -n', 'ZSH hook');
3540
} else {
3641
$this->markTestSkipped("Couldn't detect ZSH program to run hook syntax check");
3742
}
3843
}
3944

45+
public function generateHookDataProvider()
46+
{
47+
return array(
48+
array('/path/to/myprogram', null, false),
49+
array('/path/to/myprogram', null, true),
50+
array('/path/to/myprogram', 'myprogram', false),
51+
array('/path/to/myprogram', 'myprogram', true),
52+
);
53+
}
54+
4055
protected function hasProgram($programName)
4156
{
4257
exec(sprintf(

0 commit comments

Comments
 (0)