Auto Completion for Grails Scripts with Dynamic Version Determination

16 / Aug / 2012 by Vivek Krishna 0 comments

We are constantly striving to be more productive by minimizing key-strokes for a lot of commonly used shell commands. Alias is one way of achieving it, but it isn’t always the answer. Bash auto completion is one feature that I use a lot.

To avoid the hassles of working with multiple grails projects, I use this script and for auto completion, I use this wonderful auto completion script by Ted Naleid. However, there were a few more scripts/arguments which we thought would be a great addition to the auto-completion set. Scripts provided by plugins was one such set and this had to come from the appropriate directory under .grails/$GRAILS_VERSION/projects/$PROJ_HOME/plugins/*/scripts. Another set of options which I thought appropriate were the system properties that we provide.

So, I came up with the custom auto completion script given below


_grailsscripts() {

    if [ -z "$PROJ_DIR" ]
    then
       if [ -e "application.properties" ]
       then
          export GRAILS_VERSION=`grep app.grails.version application.properties | sed -E 's/.*=(.*)/\1/'`
          export GRAILS_HOME="/opt/grails-$GRAILS_VERSION"
          PWD=`pwd`
          export PROJ_DIR=`basename $PWD`
       fi
    fi
    SCRIPT_DIRS="$GRAILS_HOME/scripts ./scripts ~/.grails/scripts"
    if [ "$PROJ_DIR" ]
       then for PLUGIN_DIR in $(ls -d ~/.grails/$GRAILS_VERSION/projects/$PROJ_DIR/plugins/*/scripts 2> /dev/null); do
       SCRIPT_DIRS="$SCRIPT_DIRS $PLUGIN_DIR"
       done
    fi
    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 "^_"
}

grailsOpts="-Dserver.port= -Dgrails.env= test dev prod"

_grails() {
 COMPREPLY=( $(compgen -W "$(_grailsscripts) $grailsOpts" -- ${COMP_WORDS[COMP_CWORD]}) )
}

complete -F _grails grails
complete -F _grails grails-debug

Now, all you need is to add this script under /etc/bash_completion.d/


Hope this helps you save a few keystrokes.

FOUND THIS USEFUL? SHARE IT

Leave a comment -