Skip to content

Commit 85167a0

Browse files
committed
Query Composer to determine global config dir
Composer has a decent chunk of logic for deciding which directory to use as the global config dir. Rather than mirror that logic in shell scripts, I've updated the hooks to use `composer config --global home` to determine the config dir. Running `composer config --global home` on my machine takes around 100ms on average, so I've also updated the install instructions to generate a fixed path for `source`'ing (sluggish shell profiles are a pain). The config dir is also queried on the first completion attempt. I've done this instead of adding an environment variable to the profile, as this implementation is backwards compatible with existing installs. Fixes #10
1 parent 4fda0e9 commit 85167a0

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

README.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ This is an experimental hack to add [Symfony BASH auto complete](https://github.
66

77
## Installation
88

9-
1. Run `composer global require stecman/composer-bash-completion-plugin dev-master`
10-
2. Add a completion hook to your shell's user config file:
11-
- If you're using BASH, put the following in your `~/.bash_profile` file:
12-
13-
```bash
14-
# Add shell auto-completion for composer
15-
source "${COMPOSER_HOME-$HOME/.composer}/vendor/stecman/composer-bash-completion-plugin/hooks/bash-completion"
16-
```
17-
- If you're using ZSH, put the following in your `~/.zshrc` file:
18-
19-
```bash
20-
# Add shell auto-completion for composer
21-
source "${COMPOSER_HOME-$HOME/.composer}/vendor/stecman/composer-bash-completion-plugin/hooks/zsh-completion"
22-
```
23-
3. Reload the modified shell config (or open a new shell), and enjoy tab completion on Composer
9+
1. Install the plugin:
10+
11+
```
12+
composer global require stecman/composer-bash-completion-plugin dev-master
13+
```
14+
15+
2. Generate code to register a completion hook for your shell and Composer configuration:
16+
17+
```
18+
source $(composer config home --global)/vendor/stecman/composer-bash-completion-plugin/generate-hook
19+
```
20+
21+
3. Add the registration code to your shell profile:
22+
23+
- If you're using BASH, copy the output to your `~/.bash_profile`
24+
- If you're using ZSH, copy the output to your `~/.zshrc`
25+
26+
4. Reload your modified shell config (or open a new shell), and enjoy tab completion on Composer
2427

2528
## Explanation
2629

generate-hook

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Generate registration code to go in a shell profile
2+
3+
_composercomplete_hook_dir="$(composer config home --global)/vendor/stecman/composer-bash-completion-plugin/hooks"
4+
5+
echo "# Add tab auto-completion for composer"
6+
7+
if [ ! -z $BASH_VERSION ]; then
8+
echo "source $_composercomplete_hook_dir/bash-completion"
9+
fi
10+
11+
if [ ! -z $ZSH_VERSION ]; then
12+
echo "source $_composercomplete_hook_dir/zsh-completion"
13+
fi
14+
15+
unset _composercomplete_hook_dir

hooks/bash-completion

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ function _composercomplete {
88

99
export CMDLINE_CONTENTS CMDLINE_CURSOR_INDEX CMDLINE_WORDBREAKS
1010

11-
# Honour the COMPOSER_HOME variable if set
12-
local composer_dir=$COMPOSER_HOME
13-
if [ -z "$composer_dir" ]; then
14-
composer_dir=$HOME/.composer
11+
# Query and cache where the global composer.json lives
12+
if [ -z "$_composer_config_dir" ]; then
13+
_composer_config_dir="$(composer config home --global)"
1514
fi
1615

1716
local RESULT STATUS;
18-
19-
RESULT=$(cd $composer_dir && composer depends _completion);
17+
RESULT=$(cd "$_composer_config_dir" && composer depends _completion);
2018
STATUS=$?;
2119

2220
local cur;
@@ -32,4 +30,4 @@ function _composercomplete {
3230
__ltrim_colon_completions "$cur";
3331
};
3432

35-
complete -F _composercomplete composer;
33+
complete -F _composercomplete composer;

hooks/zsh-completion

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ function _composer {
66
local -x CMDLINE_CURSOR_INDEX
77
(( CMDLINE_CURSOR_INDEX = ${#${(j. .)words[1,CURRENT]}} ))
88

9-
# Honour the COMPOSER_HOME variable if set
10-
local composer_dir=$COMPOSER_HOME
11-
if [ -z "$composer_dir" ]; then
12-
composer_dir=$HOME/.composer
9+
# Query and cache where the global composer.json lives
10+
if [ -z "$_composer_config_dir" ]; then
11+
_composer_config_dir="$(composer config home --global)"
1312
fi
1413

1514
local RESULT STATUS
16-
RESULT=("${(@f)$( cd $composer_dir && composer depends _completion )}")
15+
RESULT=("${(@f)$( cd $_composer_config_dir && composer depends _completion )}")
1716
STATUS=$?;
1817

1918
if [ $STATUS -ne 0 ]; then
@@ -24,4 +23,4 @@ function _composer {
2423
compadd -- $RESULT
2524
};
2625

27-
compdef _composer composer;
26+
compdef _composer composer;

0 commit comments

Comments
 (0)