Skip to content

Commit

Permalink
Improve gradle plugin task parsing (ohmyzsh#5230)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
bes authored and mcornella committed Sep 15, 2016
1 parent 59c66db commit e468436
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions plugins/gradle/_gradle
1 change: 1 addition & 0 deletions plugins/gradle/_gradlew
33 changes: 31 additions & 2 deletions plugins/gradle/gradle.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@ _gradle_does_task_list_need_generating () {
[[ ! -f .gradletasknamecache ]] || [[ build.gradle -nt .gradletasknamecache ]]
}

##############
# Parse the tasks from `gradle(w) tasks --all` into .gradletasknamecache
# All lines in the output from gradle(w) that are between /^-+$/ and /^\s*$/
# are considered to be tasks. If and when gradle adds support for listing tasks
# for programmatic parsing, this method can be deprecated.
##############
_gradle_parse_tasks () {
lines_might_be_tasks=false
task_name_buffer=""
while read -r line; do
if [[ $line =~ ^-+$ ]]; then
lines_might_be_tasks=true
# Empty buffer, because it contains items that are not tasks
task_name_buffer=""
elif [[ $line =~ ^\s*$ ]]; then
if [[ "$lines_might_be_tasks" = true ]]; then
# If a newline is found, send the buffer to .gradletasknamecache
while read -r task; do
echo $task | awk '/[a-zA-Z0-9:-]+/ {print $1}'
done <<< "$task_name_buffer"
# Empty buffer, because we are done with the tasks
task_name_buffer=""
fi
lines_might_be_tasks=false
elif [[ "$lines_might_be_tasks" = true ]]; then
task_name_buffer="${task_name_buffer}\n${line}"
fi
done <<< "$1"
}

##############################################################################
# Discover the gradle tasks by running "gradle tasks --all"
Expand All @@ -68,7 +97,7 @@ _gradle_tasks () {
if [[ -f build.gradle ]]; then
_gradle_arguments
if _gradle_does_task_list_need_generating; then
gradle tasks --all | awk '/[a-zA-Z0-9:-]* - / {print $1}' > .gradletasknamecache
_gradle_parse_tasks "$(gradle tasks --all)" > .gradletasknamecache
fi
compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache)
fi
Expand All @@ -78,7 +107,7 @@ _gradlew_tasks () {
if [[ -f build.gradle ]]; then
_gradle_arguments
if _gradle_does_task_list_need_generating; then
./gradlew tasks --all | awk '/[a-zA-Z0-9:-]* - / {print $1}' > .gradletasknamecache
_gradle_parse_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
fi
compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache)
fi
Expand Down

0 comments on commit e468436

Please sign in to comment.