Skip to content

Instantly share code, notes, and snippets.

@dokky
Forked from Ea87/completion-for-gradle.md
Last active November 28, 2016 20:59
Show Gist options
  • Save dokky/10867b9120d4e06555ad5df78965fb41 to your computer and use it in GitHub Desktop.
Save dokky/10867b9120d4e06555ad5df78965fb41 to your computer and use it in GitHub Desktop.
Gradle tab completion for Bash. Works on both Mac and Linux.

Gradle tab completion script for Bash

A tab completion script that works for Bash. Should work on Mac and on Linux.

Usage

$ gradle [TAB]
androidDependencies      check                    init                     properties
assemble                 clean                    installDebug             signingReport
assembleDebug            connectedCheck           installDebugTest         tasks
assembleDebugTest        connectedInstrumentTest  installRelease           uninstallAll
assembleRelease          dependencies             lint                     uninstallDebug
build                    dependencyInsight        lintDebug                uninstallDebugTest
buildDependents          deviceCheck              lintRelease              uninstallRelease
buildNeeded              help                     projects                 wrapper
$ gradle c[TAB]
check                    clean                    connectedCheck           connectedInstrumentTest

Gives tab completions relevent to the current Gradle project (if any).

Install

curl -L -s https://gist.githubusercontent.com/dokky/10867b9120d4e06555ad5df78965fb41/raw/fde16c3bfcc718712540e0314fc0445be08d85f3/gradle-tab-completion.bash \
  -o ~/gradle-tab-completion.bash

Then add to your ~/.bash_profile:

source ~/gradle-tab-completion.bash

It will be kinda slow the first time you use it. But after that, it'll be super fast, because everything's cached based on the md5sum of your build.gradle files.

Credits

Thanks to @ligi for Linux support!

_gradle()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local gradle_cmd='gradle'
if [[ -x ./gradlew ]]; then
gradle_cmd='./gradlew'
fi
if [[ -x ../gradlew ]]; then
gradle_cmd='../gradlew'
fi
local commands=''
local cache_dir="$HOME/.gradle_tabcompletion"
mkdir -p $cache_dir
# TODO: include the gradle version in the checksum? It's kinda slow
#local gradle_version=$($gradle_cmd --version --quiet --console=plain | grep '^Gradle ' | sed 's/Gradle //g')
local gradle_files_checksum='';
if [[ -f build.gradle ]]; then # top-level gradle file
gradle_files_checksum=$(\
find . -name build.gradle 2> /dev/null \
| sort -u \
| xargs cat \
| git hash-object --stdin\
)
else # no top-level gradle file
gradle_files_checksum='no_gradle_files'
fi
if [[ -f $cache_dir/$gradle_files_checksum ]]; then # cached! yay!
commands=$(cat $cache_dir/$gradle_files_checksum)
else # not cached! boo-urns!
root_tasks="$($gradle_cmd --console=plain --quiet tasks | grep --color=never -E '^[a-z]' | awk '{print $1}' | sort | uniq)"
sub_tasks="$($gradle_cmd --console=plain --quiet tasks --all | grep --color=never -E '^[a-z]' | awk '{print $1}' | sort | uniq)"
all_tasks="$(echo "$root_tasks"; echo "$sub_tasks")"
commands="$(echo "$all_tasks" | sed ``/**/s//:/'')"
if [[ ! -z $commands ]]; then
echo "$commands" > $cache_dir/$gradle_files_checksum
fi
fi
local singledash='-? -h -a -b -c -D -d -g -I -i -m -P -p -q -S -s -t -u -v -x'
local doubledash='--help --no-rebuild --all --build-file --settings-file --console --continue --configure-on-demand --system-prop --debug --gradle-user-home --gui --init-script --info --dry-run --offline --project-prop --project-dir --parallel --max-workers --profile --project-cache-dir --quiet --recompile-scripts --refresh-dependencies --rerun-tasks --full-stacktrace --stacktrace --continuous --no-search-upwards --version --exclude-task --parallel-threads --daemon --foreground --no-daemon --stop'
case "${cur}" in
--*)
COMPREPLY=( $(compgen -S " " -W "${doubledash}" -- ${cur}) )
return
;;
-*)
COMPREPLY=( $(compgen -S " " -W "${singledash}" -- ${cur}) )
return
;;
*)
esac
if [[ $cur != ":"* ]]; then
cur=":$cur"
fi
partial=${cur##*:}
parent=$(basename $cur $partial)
COMPREPLY=( $(compgen -W "$(echo "$commands")" -- $cur | sed "s/^$parent//" | sed 's/:.*/:/' | uniq | sort) )
}
complete -F _gradle -o nospace gradle
complete -F _gradle -o nospace gradlew
complete -F _gradle -o nospace ./gradlew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment