Skip to content

Commit

Permalink
Merge pull request #48 from aik099:44-support-completion-wrapper-for-…
Browse files Browse the repository at this point in the history
…multi-app-completion
  • Loading branch information
stecman committed May 17, 2015
2 parents 35659d2 + 1328f12 commit f7fd60b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/CompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -118,6 +127,12 @@ protected function createDefinition()
InputOption::VALUE_REQUIRED,
"Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
),
new InputOption(
'multiple',
'm',
InputOption::VALUE_NONE,
"Generated hook can be used for multiple applications."
),
new InputOption(
'shell-type',
null,
Expand Down
12 changes: 10 additions & 2 deletions src/HookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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%%',
Expand All @@ -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])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f7fd60b

Please sign in to comment.