e46843685c1f337e1266a51c9cae1889c4ae9eba

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

Message

Improve gradle plugin task parsing (#5230)

* Improve gradle plugin task parsing

Added _gradle and _gradlew as symbolic links to gradle.plugin.zsh,
otherwise the plugin was not properly loaded.

Output from `gradlew tasks --all` is now parsed in two levels,
first we find segments between `------...` and a newline.
Second, all those lines are parsed and cleaned using awk
and added to .gradletasknamecache.

Tested on gradle 2.13, and gradlew 2.14.

* Remove .gradletasknamecache before regenerating it

Remove the .gradletasknamecache file to avoid having an unnecessary
newline at the top of the file when regenerating it.

* Improve gradle task parsing by writing .gradletasknamecache atomically

Previously the .gradletasknamecache file was written line by line inside a parsing loop,
which could cause errors such as half-written cache files if the process was aborted.

This also removes the need of deleting the .gradletasknamecache file before parsing.

Diff

 1diff --git a/plugins/gradle/_gradle b/plugins/gradle/_gradle
 2new file mode 120000
 3index 0000000000000000000000000000000000000000..80723f2fc07b3da179693654769fe549a3deca41
 4--- /dev/null
 5+++ b/plugins/gradle/_gradle
 6@@ -0,0 +1 @@
 7+gradle.plugin.zsh
 8diff --git a/plugins/gradle/_gradlew b/plugins/gradle/_gradlew
 9new file mode 120000
10index 0000000000000000000000000000000000000000..80723f2fc07b3da179693654769fe549a3deca41
11--- /dev/null
12+++ b/plugins/gradle/_gradlew
13@@ -0,0 +1 @@
14+gradle.plugin.zsh
15diff --git a/plugins/gradle/gradle.plugin.zsh b/plugins/gradle/gradle.plugin.zsh
16index a908eaeaa9ba3db60dafad77257e012b403739a8..b2015a351d062fc889f0a40c586ca987aef668c6 100644
17--- a/plugins/gradle/gradle.plugin.zsh
18+++ b/plugins/gradle/gradle.plugin.zsh
19@@ -60,6 +60,35 @@ _gradle_does_task_list_need_generating () {
20   [[ ! -f .gradletasknamecache ]] || [[ build.gradle -nt .gradletasknamecache ]]
21 }
22 
23+##############
24+# Parse the tasks from `gradle(w) tasks --all` into .gradletasknamecache
25+# All lines in the output from gradle(w) that are between /^-+$/ and /^\s*$/
26+# are considered to be tasks. If and when gradle adds support for listing tasks
27+# for programmatic parsing, this method can be deprecated.
28+##############
29+_gradle_parse_tasks () {
30+  lines_might_be_tasks=false
31+  task_name_buffer=""
32+  while read -r line; do
33+    if [[ $line =~ ^-+$ ]]; then
34+      lines_might_be_tasks=true
35+      # Empty buffer, because it contains items that are not tasks
36+      task_name_buffer=""
37+    elif [[ $line =~ ^\s*$ ]]; then
38+      if [[ "$lines_might_be_tasks" = true ]]; then
39+        # If a newline is found, send the buffer to .gradletasknamecache
40+        while read -r task; do
41+          echo $task | awk '/[a-zA-Z0-9:-]+/ {print $1}'
42+        done <<< "$task_name_buffer"
43+        # Empty buffer, because we are done with the tasks
44+        task_name_buffer=""
45+      fi
46+      lines_might_be_tasks=false
47+    elif [[ "$lines_might_be_tasks" = true ]]; then
48+      task_name_buffer="${task_name_buffer}\n${line}"
49+    fi
50+  done <<< "$1"
51+}
52 
53 ##############################################################################
54 # Discover the gradle tasks by running "gradle tasks --all"
55@@ -68,7 +97,7 @@ _gradle_tasks () {
56   if [[ -f build.gradle ]]; then
57     _gradle_arguments
58     if _gradle_does_task_list_need_generating; then
59-      gradle tasks --all | awk '/[a-zA-Z0-9:-]* - / {print $1}' > .gradletasknamecache
60+      _gradle_parse_tasks "$(gradle tasks --all)" > .gradletasknamecache
61     fi
62     compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache)
63   fi
64@@ -78,7 +107,7 @@ _gradlew_tasks () {
65   if [[ -f build.gradle ]]; then
66     _gradle_arguments
67     if _gradle_does_task_list_need_generating; then
68-      ./gradlew tasks --all | awk '/[a-zA-Z0-9:-]* - / {print $1}' > .gradletasknamecache
69+      _gradle_parse_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
70     fi
71     compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache)
72   fi