Autocomplete Grails Script Names in bash/zsh
2008/03/25Jesse over at Refactr posted a nice tip about using tab completion for the ssh command. It grabs host names and IPs out of the ssh known_hosts file.
That got me to thinking that it would be pretty useful to have tab completion of Grails commands available. At my company, we’ve written about 20 custom Gant scripts and it can sometimes be a problem to remember their names. Running “grails help” often takes too long, so I probably ls scripts once a day or so to remind myself if it’s clean-db or clear-db.
I did a quick google search and found that Scott Davis posted some instructions about a year ago (originally by Doyle@DoyleCentral).
It was a good start, but there were two issues I had with the solution. The first is that it only worked in bash (I prefer zsh) and the second is that it used a static list of script names stored in the GRAILS_HOME directory. Any new scripts, or app specific scripts would need to be manually added to the static list.
Grails Gant scripts can exist in 4 possible locations:
- $GRAILS_HOME/scripts
- $USER_HOME/.grails/scripts
- $PROJECT_HOME/scripts
- $PROJECT_HOME/plugins/*/scripts
After some playing around, I was able to come up with a bash script that allows for real-time completion of all four potential script repositories. $PROJECT_HOME is considered to be the current directory, so if you’re not in a grails app, you’ll only see completion scripts for the first two.
Just copy this into your .profile/.bash_profile and restart your terminal session.
_grailsscripts() { SCRIPT_DIRS="$GRAILS_HOME/scripts ./scripts ~/.grails/scripts" if [ -d plugins ] then for PLUGIN_DIR in $(ls -d plugins/*/scripts 2> /dev/null); do SCRIPT_DIRS="$SCRIPT_DIRS $PLUGIN_DIR" done fi for D in $SCRIPT_DIRS; do if [ -d $D ] then ls -1 $D/*.groovy 2> /dev/null | sed -E 's/(.*)\/(.*)\.groovy/\2/' | sed -E 's/([A-Z])/-\1/g' | sed -E 's/^-//' | tr "[:upper:]" "[:lower:]" fi done | sort | uniq | grep -vE "^_" } _grails() { COMPREPLY=( $(compgen -W "$(_grailsscripts)" -- ${COMP_WORDS[COMP_CWORD]}) ) } complete -F _grails grails
After you do that you can type grails c and then if you hit tab twice, you’ll see this list:
fourthwall:~ ted$ grails c clean create-app create-integration-test create-service compile create-controller create-plugin create-tag-lib console create-domain-class create-script create-unit-test
If you type another r and hit tab it will finish the word create-. Then type d and hit tab and it will finish off grails create-domain-class for you.
For those using zsh, you need to have run compinstall. After that, all you need to do is put this in your .zshrc, but it needs to be below the compinit declaration added by compinstall:
_grailsscriptdirs() { local SCRIPT_DIRS="$GRAILS_HOME/scripts ./scripts ~/.grails/scripts" if [ -d plugins ] then for PLUGIN_DIR in $(ls -d plugins/*/scripts); do SCRIPT_DIRS="$SCRIPT_DIRS $PLUGIN_DIR" done fi echo $SCRIPT_DIRS } _grailsscripts() { for D in $(_grailsscriptdirs); do if [ -d $D ] then ls -1 $D/*.groovy | sed -E 's/(.*)\/(.*)\.groovy/\2/' | sed -E 's/([A-Z])/-\1/g' | sed -E 's/^-//' | tr "[:upper:]" "[:lower:]" fi done | sort | uniq | grep -vE "^_" } _grails() { if (( CURRENT == 2 )); then scripts=( $(_grailsscripts) ) _multi_parts / scripts else _files fi } compdef _grails grails
(3/28/08 – zsh updated slightly to only insert scripts for the first parameter of a grails command)
I’m sure it’s possible to clean up the bash/zsh scripting (I’m a groovy programmer not a bash shell scripter :), but these both seemed to work pretty well from my testing.
There are 12 comments in this article: