Bash completion for Grails
I have been using Grails a bit recently (may be the subject of a Blog entry later). There is often the need dive out to the command line to run stuff like:
prompt% grails create-domain-class book
This has coincided with wanting to learn more about the auto complete feature of bash. I found this tip from Doyle of DoyleCentral.com but, to me, it is deficient in that it has a hard coded list of Grails targets. So, I decided to spin my own that gets the list of targets from Grails. Here is what I put into my .bashrc
file:
# Setup completion for grails. Lazy load the words and do it once only.
_grails()
{
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ -z "${GRAILS_TARGETS}" ]; then
GRAILS_TARGETS=$(for i in `grails help | grep '^grails ' | sed 's/^grails *//'`; do echo -n "$i "; done)
fi
COMPREPLY=( $(compgen -W "${GRAILS_TARGETS}" -- ${cur}) )
}
complete -F _grails grails
This approach uses the variable GRAILS\_TARGETS
to contain the list of Grails targets. It is lazily evaluated as it is expensive to calculate. Now of course you may still prefer the other approach.
PS: I am back to using Emacs Muse for writing Blog entries.