{"id":6197,"date":"2012-08-16T12:03:23","date_gmt":"2012-08-16T06:33:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6197"},"modified":"2012-08-16T12:03:23","modified_gmt":"2012-08-16T06:33:23","slug":"auto-completion-for-grails-scripts-with-dynamic-version-determination","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/auto-completion-for-grails-scripts-with-dynamic-version-determination\/","title":{"rendered":"Auto Completion for Grails Scripts with Dynamic Version Determination"},"content":{"rendered":"<p>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&#8217;t always the answer. Bash auto completion is one feature that I use a lot.<\/p>\n<p>To avoid the hassles of working with multiple grails projects, I use <a href=\"https:\/\/github.com\/deluan\/grails.sh\" target=\"_blank\">this script <\/a>and for auto completion, I use this wonderful <a href=\"http:\/\/naleid.com\/blog\/2008\/03\/25\/autocomplete-grails-script-names-in-bashzsh\/\" target=\"_blank\">auto completion script by Ted Naleid<\/a>. 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 <em><strong>.grails\/$GRAILS_VERSION\/projects\/$PROJ_HOME\/plugins\/*\/scripts.<\/strong><\/em> Another set of options which I thought appropriate were the system properties that we provide.<\/p>\n<p>So, I came up with the custom auto completion script given below<\/p>\n<p>[shell]<\/p>\n<p>_grailsscripts() {<\/p>\n<p>    if [ -z &quot;$PROJ_DIR&quot; ]<br \/>\n    then<br \/>\n       if [ -e &quot;application.properties&quot; ]<br \/>\n       then<br \/>\n          export GRAILS_VERSION=`grep app.grails.version application.properties | sed -E &#8216;s\/.*=(.*)\/\\1\/&#8217;`<br \/>\n          export GRAILS_HOME=&quot;\/opt\/grails-$GRAILS_VERSION&quot;<br \/>\n          PWD=`pwd`<br \/>\n          export PROJ_DIR=`basename $PWD`<br \/>\n       fi<br \/>\n    fi<br \/>\n    SCRIPT_DIRS=&quot;$GRAILS_HOME\/scripts .\/scripts ~\/.grails\/scripts&quot;<br \/>\n    if [ &quot;$PROJ_DIR&quot; ]<br \/>\n       then for PLUGIN_DIR in $(ls -d ~\/.grails\/$GRAILS_VERSION\/projects\/$PROJ_DIR\/plugins\/*\/scripts 2&amp;gt; \/dev\/null); do<br \/>\n       SCRIPT_DIRS=&quot;$SCRIPT_DIRS $PLUGIN_DIR&quot;<br \/>\n       done<br \/>\n    fi<br \/>\n    if [ -d plugins ]<br \/>\n       then for PLUGIN_DIR in $(ls -d plugins\/*\/scripts 2&amp;gt; \/dev\/null); do<br \/>\n       SCRIPT_DIRS=&quot;$SCRIPT_DIRS $PLUGIN_DIR&quot;<br \/>\n       done<br \/>\n    fi<\/p>\n<p>    for D in $SCRIPT_DIRS; do<br \/>\n       if [ -d $D ]<br \/>\n         then ls -1 $D\/*.groovy 2&amp;gt; \/dev\/null | sed -E &#8216;s\/(.*)\\\/(.*)\\.groovy\/\\2\/&#8217; | sed -E &#8216;s\/([A-Z])\/-\\1\/g&#8217; | sed -E &#8216;s\/^-\/\/&#8217; | tr   &quot;[:upper:]&quot; &quot;[:lower:]&quot;<br \/>\n       fi<br \/>\n    done | sort | uniq | grep -vE &quot;^_&quot;<br \/>\n}<\/p>\n<p>grailsOpts=&quot;-Dserver.port= -Dgrails.env= test dev prod&quot;<\/p>\n<p>_grails() {<br \/>\n COMPREPLY=( $(compgen -W &quot;$(_grailsscripts) $grailsOpts&quot; &#8212; ${COMP_WORDS[COMP_CWORD]}) )<br \/>\n}<\/p>\n<p>complete -F _grails grails<br \/>\ncomplete -F _grails grails-debug<\/p>\n<p>[\/shell]<\/p>\n<p>Now, all you need is to add this script under \/etc\/bash_completion.d\/<br \/>\n<br \/>\nHope this helps you save a few keystrokes.<br \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[865,519,4840,866,260,453,8],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6197"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6197"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}