Skip to content

Instantly share code, notes, and snippets.

@resamsel
Forked from nolanlawson/completion-for-gradle.md
Last active August 29, 2015 14:10
Show Gist options
  • Save resamsel/469545c0f2a652a31629 to your computer and use it in GitHub Desktop.
Save resamsel/469545c0f2a652a31629 to your computer and use it in GitHub Desktop.

Gradle tab completion script for Bash

A tab completion script that works for Bash. Relies on the BSD md5 command on Mac and md5sum on Linux, so as long as you have one of those two commands, this should work.

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.github.com/nolanlawson/8694399/raw/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 --no-color | grep '^Gradle ' | sed 's/Gradle //g')
local gradle_files_checksum='';
if [[ -f build.gradle ]]; then # top-level gradle file
if [[ -x `which md5 2> /dev/null` ]]; then # mac
local all_gradle_files=$(find . -name build.gradle 2>/dev/null)
gradle_files_checksum=$(md5 -q -s "$(md5 -q $all_gradle_files)")
else # linux
gradle_files_checksum=($(find . -name build.gradle | xargs md5sum | md5sum))
fi
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!
alltasks="$($gradle_cmd --no-color --quiet tasks --all | grep -E '^[a-z]' | awk '{print $1}' | sort)"
tasks="$(echo "$alltasks" | sed 's/^[^:]*://g' | sort | uniq)"
subtasks="$(echo "$alltasks" | grep ':')"
commands="$(echo "$tasks"; echo "$subtasks")"
if [[ ! -z $commands ]]; then
echo "$commands" > $cache_dir/$gradle_files_checksum
fi
fi
if [[ "$cur" != *:* ]]; then
# There's no : in the current word, skip sub tasks
commands="$(echo "$commands" | sed 's/:[^:]*$/:/g' | uniq)"
else
# There's a : in the current word, skip normal tasks
commands="$(echo "$commands" | grep ':')"
fi
cur=`echo $cur | sed 's/\\\\//g'`
COMPREPLY=( $(compgen -W "$(echo "$commands" | tr '\n' ' ')" -- $cur | sed 's/\\\\//g') )
}
complete -F _gradle -o nospace gradle
complete -F _gradle -o nospace gradlew
complete -F _gradle -o nospace ./gradlew
#COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment