Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-app completion hook generation (not default behavior) #48

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{$1} works under BASH, but doesn't resolve to anything under ZSH. Just using $1 works correctly on both BASH and ZSH.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking that out.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1328f12 by the way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the first thing I've checked before wanting to to ask you why you've merged this PR and checked the merge result to find the error instead tell me to fix that and merging only afterwards.

} 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