Skip to content

Commit 6701f3d

Browse files
committed
Add remove and update completions based on required packages
Resolves #4
1 parent d00499c commit 6701f3d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This is an experimental hack to add [Symfony BASH auto complete](https://github.
1616
# so for this hack to work, we are always running the completion command in ~/.composer
1717
function _composercomplete {
1818
export COMP_LINE COMP_POINT COMP_WORDBREAKS;
19+
local -x COMPOSER_CWD=`pwd`
1920
local RESULT STATUS
2021

2122
# Honour the COMPOSER_HOME variable if set
@@ -59,6 +60,7 @@ This is an experimental hack to add [Symfony BASH auto complete](https://github.
5960
fi
6061

6162
local RESULT STATUS
63+
local -x COMPOSER_CWD=`pwd`
6264
RESULT=("${(@f)$( cd $composer_dir && composer _completion )}")
6365
STATUS=$?;
6466

src/ComposerCompletionCommand.php

+59
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
class ComposerCompletionCommand extends CompletionCommand
99
{
10+
/**
11+
* Whether or not composer is being run using the global command
12+
* @var boolean
13+
*/
14+
protected $isGlobal = false;
15+
1016
protected function runCompletion()
1117
{
1218
$context = $this->handler->getContext();
@@ -24,6 +30,22 @@ protected function runCompletion()
2430
$context->setCommandLine(
2531
preg_replace_callback('/( global| g)( |$)/', $replace, $context->getCommandLine(), 1)
2632
);
33+
34+
$this->isGlobal = true;
35+
}
36+
37+
// Complete for composer.json in current directory
38+
if ($this->isGlobal) {
39+
$composerFile = getcwd() . '/composer.json';
40+
} else {
41+
$workingDir = getenv('COMPOSER_CWD');
42+
$composerFile = $workingDir . '/composer.json';
43+
}
44+
45+
if (file_exists($composerFile)) {
46+
$this->addProjectLocalCompletions(
47+
json_decode(file_get_contents($composerFile), true)
48+
);
2749
}
2850

2951
// Complete for `help` command's `command` argument
@@ -47,4 +69,41 @@ function() use ($application) {
4769

4870
return $this->handler->runCompletion();
4971
}
72+
73+
/**
74+
* Setup completions that require a composer.json file to work
75+
* @param array $config - parsed composer.json
76+
*/
77+
protected function addProjectLocalCompletions($config)
78+
{
79+
$packages = $this->getRequiredPackages($config);
80+
81+
$completeRequiredPackages = function() use ($packages) {
82+
return $packages;
83+
};
84+
85+
// Complete for `remove` and `update` commands `packages` argument
86+
$this->handler->addHandler(new Completion('remove', 'packages', Completion::TYPE_ARGUMENT, $completeRequiredPackages));
87+
$this->handler->addHandler(new Completion('update', 'packages', Completion::TYPE_ARGUMENT, $completeRequiredPackages));
88+
}
89+
90+
/**
91+
* Get a list of package names that are required in a composer.json config
92+
* @param array $config
93+
* @return array
94+
*/
95+
protected function getRequiredPackages($config)
96+
{
97+
$packages = array();
98+
99+
if (isset($config['require'])) {
100+
$packages = array_merge($packages, array_keys($config['require']));
101+
}
102+
103+
if (isset($config['require-dev'])) {
104+
$packages = array_merge($packages, array_keys($config['require-dev']));
105+
}
106+
107+
return $packages;
108+
}
50109
}

0 commit comments

Comments
 (0)