Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions _gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,51 @@ __gradle-completion-init() {
return 0
}

# Instead of printing all tasks for all subprojects, print only subprojects names to limit number of entries shown on the screen
# Once a single subproject is choosen - return its all available tasks.
__gradle-trim-tasks-to-subprojects() {
if [[ "$1" =~ ^(.+:.*) ]]; then
# do nothing if a subproject is already choosen
cat
else
awk -F '[[:alnum:]_]:' '{
# Split entries into (subprojects names) AND (tasks accessible from root project)
if ($1 ~ /:/) {
# keep only subproject name (skip task name part)
c = split($1, arr, "\\\\:")

if (c == 3) {
current = "\\:" arr[c-1] "\\:"
} else {
current = arr[c-1] "\\:"
}
# keep uniq names (it assumes that the input to this funtion is already sorted)
if (m == 0 || current != module[m-1]) {
module[m++] = current
}
module_task_line[mtl++] = $0
} else {
# otherwise store the whole line with description
root_task_line[rtl++] = $0
}
} END {
for(i=0; i<rtl; i++) {
print root_task_line[i]
}
if (m > 1) {
for(i=0; i<m; i++) {
print module[i]
}
} else {
# at most 1 subproject is choosen, show possible tasks
for(i=0; i<mtl; i++) {
print module_task_line[i]
}
}
}'
fi
}

__gradle_tasks() {
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir

Expand All @@ -157,10 +202,12 @@ __gradle_tasks() {
if [[ -f $cache_dir/$cache_name.md5 ]]; then
local cached_checksum="$(cat $cache_dir/$cache_name.md5)"
local -a cached_tasks

if [[ -z $cur ]]; then
cached_tasks=(${(f)"$(cat $cache_dir/$cached_checksum)"})
cached_tasks=(${(f)"$(cat $cache_dir/$cached_checksum | __gradle-trim-tasks-to-subprojects "" )"})
else
cached_tasks=(${(f)"$(grep "^${cur//:/\\\\:}" $cache_dir/$cached_checksum)"})
cur_esc="${cur//:/\\\\:}"
cached_tasks=(${(f)"$(grep "^${cur_esc}" $cache_dir/$cached_checksum | __gradle-trim-tasks-to-subprojects $cur)"})
fi
_describe 'all tasks' cached_tasks && ret=0
else
Expand Down
50 changes: 48 additions & 2 deletions gradle-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,50 @@ __gradle-short-options() {
COMPREPLY=( $(compgen -W "$args" -- "$cur") )
}

# Instead of printing all tasks for all subprojects, print only subprojects names to limit number of entries shown on the screen
# Once a single subproject is choosen - return its all available tasks.
__gradle-trim-tasks-to-subprojects() {
if [[ "$1" =~ ^(.+:.*) ]]; then
# do nothing if a subproject is already choosen
cat
else
awk '{
# Split entries into (subprojects names) AND (tasks accessible from root project)
if ($1 ~ /:/) {
# keep only subproject name (skip task name part)
c = split($1, arr, ":")
if (c == 3) {
current = ":" arr[c-1] ":"
} else {
current = arr[c-1] ":"
}
# keep uniq names (it assumes that the input to this funtion is already sorted)
if (m == 0 || current != module[m-1]) {
module[m++] = current
}
module_task_line[mtl++] = $0
} else {
# otherwise store the whole line with description
root_task_line[rtl++] = $0
}
} END {
for(i=0; i<rtl; i++) {
print root_task_line[i]
}
if (m > 1) {
for(i=0; i<m; i++) {
print module[i]
}
} else {
# at most 1 subproject is choosen, show possible tasks
for(i=0; i<mtl; i++) {
print module_task_line[i]
}
}
}'
fi
}

__gradle-tasks() {
local cur
_get_comp_words_by_ref -n : cur
Expand All @@ -196,10 +240,11 @@ __gradle-tasks() {
local cached_checksum="$(cat "$cache_dir/$cache_name.md5")"
local -a cached_tasks
if [[ -z "$cur" ]]; then
cached_tasks=( $(cat "$cache_dir/$cached_checksum") )
cached_tasks=( $(cat "$cache_dir/$cached_checksum" | __gradle-trim-tasks-to-subprojects "$cur") )
else
cached_tasks=( $(grep "^$cur" "$cache_dir/$cached_checksum") )
cached_tasks=( $(grep "^$cur" "$cache_dir/$cached_checksum" | __gradle-trim-tasks-to-subprojects "$cur") )
fi

COMPREPLY=( $(compgen -W "${cached_tasks[*]}" -- "$cur") )
else
__gradle-notify-tasks-cache-build
Expand Down Expand Up @@ -300,6 +345,7 @@ __gradle-generate-tasks-cache() {
# subproject tasks can be referenced implicitly from root project
if [[ "$GRADLE_COMPLETION_UNQUALIFIED_TASKS" == "true" ]]; then
local -a implicit_tasks=()
# TODO make root_tasks or implicit_tasks (?) uniq somewhere below, to not store in cache unnecessary entries.
implicit_tasks=( $(comm -23 <(printf "%s\n" "${subproject_tasks[@]}" | sort) <(printf "%s\n" "${root_tasks[@]}" | sort)) )
for task in $(printf "%s\n" "${implicit_tasks[@]}"); do
gradle_all_tasks+=( "$task" )
Expand Down