1. export GRAILS_VERSION="$(ls -lhr $HOME/.grails | egrep -i '1\.' | head -1 | gawk '{print $9 }')"
  2.  
  3. sed_option="-E"
  4.  
  5. [ "$(uname)" == "Linux" ] && sed_option="-r"
  6.  
  7. _get_domain_classes(){
  8.     find ./grails-app/domain -iname *.groovy 2> /dev/null | tr \\n ' ' | sed 's/\.groovy//g' | sed 's/\.\/grails-app\/domain\///g' | tr '/' \.
  9. }
  10.  
  11. _get_tests(){
  12.     find ./test -iname *.groovy 2> /dev/null | sed 's/\.\/test\/integration\///g' | sed 's/\Tests.groovy//g' | tr '/' \.
  13. }
  14.  
  15. _get_plugins(){
  16.     cat $HOME/.grails/$GRAILS_VERSION/plugins/plugins-list.xml 2> /dev/null | grep \<plugin | gawk -F"name=" '{print $2}' | sed 's/\"//g' | sed 's/\/\{0,1\}\>//g'
  17. }
  18.  
  19. _get_scripts(){
  20.     for D in $SCRIPT_DIRS; do
  21.         if [ -d $D ]
  22.             then ls -1 $D/*.groovy 2> /dev/null | sed $sed_option 's/(.*)\/(.*)\.groovy/\2/' | sed $sed_option 's/([A-Z])/-\1/g' | sed $sed_option 's/^-//' | tr "[:upper:]" "[:lower:]"
  23.         fi
  24.     done | sort | uniq | grep -vE "^_"
  25. }
  26.  
  27. _grails_comp(){
  28.     local cur prev opts base
  29.     COMPREPLY=()
  30.     cur="${COMP_WORDS[COMP_CWORD]}"
  31.     prev="${COMP_WORDS[COMP_CWORD-1]}"
  32.     if [ -r ./grails-app ]; then
  33.         SCRIPT_DIRS="$GRAILS_HOME/scripts ./scripts $HOME/.grails/scripts"
  34.         if [ -d plugins ]
  35.             then for PLUGIN_DIR in $(ls -d plugins/*/scripts 2> /dev/null); do
  36.             SCRIPT_DIRS="$SCRIPT_DIRS $PLUGIN_DIR"
  37.             done
  38.         fi
  39.  
  40.         opts=$(_get_scripts)
  41.    
  42.         case "${prev}" in
  43.             generate-all)
  44.                 local classes=$(_get_domain_classes)
  45.                 COMPREPLY=( $(compgen -W "${classes}" -- ${cur}) )
  46.                 return 0
  47.                 ;;
  48.             generate-views)
  49.                 local classes=$(_get_domain_classes)
  50.                 COMPREPLY=( $(compgen -W "${classes}" -- ${cur}) )
  51.                 return 0
  52.                 ;;
  53.             generate-controller)
  54.                 local classes=$(_get_domain_classes)
  55.                 COMPREPLY=( $(compgen -W "${classes}" -- ${cur}) )
  56.                 return 0
  57.                 ;;
  58.             test-app)
  59.                 local test_classes=$(_get_tests)
  60.                 COMPREPLY=( $(compgen -W "${test_classes}" -- ${cur}) )
  61.                 return 0
  62.                 ;;
  63.             install-plugin)
  64.                 local plugins=$(_get_plugins)
  65.                 COMPREPLY=( $(compgen -W "${plugins}" -- ${cur}) )
  66.                 return 0
  67.                 ;;
  68.             package-plugin)
  69.                 COMPREPLY=( $(compgen -f) )
  70.                 return 0
  71.                 ;;
  72.             plugin-info)
  73.                 local plugins=$(opts)
  74.                 COMPREPLY=( $(compgen -W "${plugins}" -- ${cur}) )
  75.                 return 0
  76.                 ;;
  77.             help)
  78.                 local opts=$(_get_scripts)
  79.                 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  80.                 return 0
  81.                 ;;
  82.             *)
  83.             ;;
  84.         esac
  85.    
  86.         if [[ "${opts}" =~ "${prev}" ]]; then
  87.             COMPREPLY=( $(compgen -f) )
  88.             return 0
  89.         fi
  90.    
  91.         COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
  92.         return 0
  93.     else
  94.         opts="create-app create-plugin help list-plugins package-plugin plugin-info set-proxy"
  95.         case "${prev}" in
  96.             create-app)
  97.                 COMPREPLY=( $(compgen -f) )
  98.                 return 0
  99.                 ;;
  100.             create-plugin)
  101.                 COMPREPLY=( $(compgen -f) )
  102.                 return 0
  103.                 ;;
  104.             package-plugin)
  105.                 COMPREPLY=( $(compgen -f) )
  106.                 return 0
  107.                 ;;
  108.             plugin-info)
  109.                 local plugins=$(_get_plugins)
  110.                 COMPREPLY=( $(compgen -W "${plugins}" -- ${cur}) )
  111.                 return 0
  112.                 ;;
  113.             *)
  114.                 ;;
  115.         esac
  116.         COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
  117.         return 0
  118.     fi
  119. }
  120.  
  121. complete -F _grails_comp grails