📜 ⬆️ ⬇️

Bash completion for Composer

Good day to all.

Dedicated to those who use Composer, but faced with the fact that when choosing a package to install (require) you need to enter the version of this package. It is not very convenient to search for it somewhere constantly, even using * it is not always possible to install everything correctly.

Thus, I decided to publish an auto-substitution script.
At the moment, it requires the substitution of package names, and if the package is already substituted, then the version of this package is only for the require command.

The essence of the idea was simple - to substitute the package name, then substitute its version, and if the previous is not the package name, then this is the version, then try again to find the package name.
')
The script itself is usually on the way (you can make a symlink there, well, or someone like that)
/usr/share/bash-completion/completions
Script content
 # Composer completion _composer() { local cur prev words cword _init_completion || return local special i for (( i=0; i < ${#words[@]}-1; i++ )); do if [[ ${words[i]} == @(require) ]]; then special=${words[i]} fi done if [[ -n $special ]]; then if [[ $prev != $special ]]; then # ,       , #     COMPREPLY=( $( composer show $prev 2>/dev/null | \ sed -r -e 's/\x1b\[[0-9]+m//g' | grep ^versions | \ sed -r -e 's/^versions : //' -e 's/, /\n/g' | grep "^$cur" | sort -u ) ) fi if [[ -z $COMPREPLY ]]; then #     ,        COMPREPLY=( $( composer show -a 2>/dev/null | \ grep -v '^No composer' | grep "^$cur" | sort -u ) ) fi return 0 fi #     if [[ "$cur" == -* ]]; then COMPREPLY=( $( compgen -W '-h -q -v -V -n -d \ --help --quiet --verbose --version --ansi --no-ansi \ --no-interaction --profile --working-dir' -- "$cur" ) ) else COMPREPLY=( $( compgen -W 'about archive config create-project \ depends diagnose dump-autoload dumpautoload \ help init install list require search \ self-update selfupdate show status update validate' -- "$cur" ) ) fi return 0 } && complete -F _composer composer #     composer 


Of the minuses, I want to note a long response due to the fact that the composer itself slowly reads its repository.
The latter was partially corrected by caching the list of packages and their versions, but until the next update of the composer itself.
It turns out that there is a possibility not to autocomplete into the line a package that is already on the list, but was not at the time of caching. In this case, you must either delete the cache, or update the composer itself. Something like this ... well, I think this is a compromise.

Of course, it is necessary that Composer be installed in the system as a composer .
Personally, I put it in / usr / local / bin and periodically update sudo composer self-update via sudo composer self-update .

If anyone has a desire to add something to improve, then I ask on Github

That's all.
Thanks to all!

Source: https://habr.com/ru/post/176715/


All Articles