3b13dc07d823ebcb67ea429de305b5413e99ac1e

Author
Erik Zivkovic <erik.zivkovic@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

gradle: extract simple task names from subproject tasks (#5704)

Currently, only tasks with complete subproject specifier are added to
.gradletasknamecache. Gradle commands can be called for all (sub-)projects
they are defined for, using their name as defined in the subproject, here
called "simple" task names. One example is "gradle clean".

This patch adds support for parsing out those "simple" task names from the list
of fully specified task names. The .gradletasknamecache file will contain
both the fully specified names, and the "simple" names for your autocompletion
pleasure.

Diff

 1diff --git a/plugins/gradle/gradle.plugin.zsh b/plugins/gradle/gradle.plugin.zsh
 2index b2015a351d062fc889f0a40c586ca987aef668c6..65b9d4685677db96bfe44af1c861d5e939d3da74 100644
 3--- a/plugins/gradle/gradle.plugin.zsh
 4+++ b/plugins/gradle/gradle.plugin.zsh
 5@@ -61,7 +61,7 @@ _gradle_does_task_list_need_generating () {
 6 }
 7 
 8 ##############
 9-# Parse the tasks from `gradle(w) tasks --all` into .gradletasknamecache
10+# Parse the tasks from `gradle(w) tasks --all` and return them to the calling function.
11 # All lines in the output from gradle(w) that are between /^-+$/ and /^\s*$/
12 # are considered to be tasks. If and when gradle adds support for listing tasks
13 # for programmatic parsing, this method can be deprecated.
14@@ -76,7 +76,7 @@ _gradle_parse_tasks () {
15       task_name_buffer=""
16     elif [[ $line =~ ^\s*$ ]]; then
17       if [[ "$lines_might_be_tasks" = true ]]; then
18-        # If a newline is found, send the buffer to .gradletasknamecache
19+        # If a newline is found, echo the buffer to the calling function
20         while read -r task; do
21           echo $task | awk '/[a-zA-Z0-9:-]+/ {print $1}'
22         done <<< "$task_name_buffer"
23@@ -90,6 +90,25 @@ _gradle_parse_tasks () {
24   done <<< "$1"
25 }
26 
27+
28+##############
29+# Gradle tasks from subprojects are allowed to be executed without specifying
30+# the subproject; that task will then be called on all subprojects.
31+# gradle(w) tasks --all only lists tasks per subproject, but when autocompleting
32+# we often want to be able to run a specific task on all subprojects, e.g.
33+# "gradle clean".
34+# This function uses the list of tasks from "gradle tasks --all", and for each
35+# line grabs everything after the last ":" and combines that output with the original
36+# output. The combined list is returned as the result of this function.
37+##############
38+_gradle_parse_and_extract_tasks () {
39+  # All tasks
40+  tasks=$(_gradle_parse_tasks "$1")
41+  # Task name without sub project(s) prefix
42+  simple_tasks=$(echo $tasks | awk 'BEGIN { FS = ":" } { print $NF }')
43+  echo "$tasks\n$simple_tasks"
44+}
45+
46 ##############################################################################
47 # Discover the gradle tasks by running "gradle tasks --all"
48 ############################################################################
49@@ -97,7 +116,7 @@ _gradle_tasks () {
50   if [[ -f build.gradle ]]; then
51     _gradle_arguments
52     if _gradle_does_task_list_need_generating; then
53-      _gradle_parse_tasks "$(gradle tasks --all)" > .gradletasknamecache
54+      _gradle_parse_and_extract_tasks "$(gradle tasks --all)" > .gradletasknamecache
55     fi
56     compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache)
57   fi
58@@ -107,7 +126,7 @@ _gradlew_tasks () {
59   if [[ -f build.gradle ]]; then
60     _gradle_arguments
61     if _gradle_does_task_list_need_generating; then
62-      _gradle_parse_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
63+      _gradle_parse_and_extract_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
64     fi
65     compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache)
66   fi