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.
This commit is contained in:
Erik Zivkovic 2016-09-14 14:57:52 +02:00
commit ff862f84a7

View file

@ -66,7 +66,7 @@ _gradle_does_task_list_need_generating () {
# are considered to be tasks. If and when gradle adds support for listing tasks # are considered to be tasks. If and when gradle adds support for listing tasks
# for programmatic parsing, this method can be deprecated. # for programmatic parsing, this method can be deprecated.
############## ##############
_gradle_parse_tasks_to_gradletasknamecache () { _gradle_parse_tasks () {
lines_might_be_tasks=false lines_might_be_tasks=false
task_name_buffer="" task_name_buffer=""
while read -r line; do while read -r line; do
@ -78,7 +78,7 @@ _gradle_parse_tasks_to_gradletasknamecache () {
if [[ "$lines_might_be_tasks" = true ]]; then if [[ "$lines_might_be_tasks" = true ]]; then
# If a newline is found, send the buffer to .gradletasknamecache # If a newline is found, send the buffer to .gradletasknamecache
while read -r task; do while read -r task; do
echo $task | awk '/[a-zA-Z0-9:-]+/ {print $1}' >> .gradletasknamecache echo $task | awk '/[a-zA-Z0-9:-]+/ {print $1}'
done <<< "$task_name_buffer" done <<< "$task_name_buffer"
# Empty buffer, because we are done with the tasks # Empty buffer, because we are done with the tasks
task_name_buffer="" task_name_buffer=""
@ -97,13 +97,7 @@ _gradle_tasks () {
if [[ -f build.gradle ]]; then if [[ -f build.gradle ]]; then
_gradle_arguments _gradle_arguments
if _gradle_does_task_list_need_generating; then if _gradle_does_task_list_need_generating; then
# First empty the old cache file _gradle_parse_tasks "$(gradle tasks --all)" > .gradletasknamecache
if [ -f .gradletasknamecache ]; then
rm .gradletasknamecache
fi
# Then append the rest of the lines to that file
output=$(gradle tasks --all)
_gradle_parse_tasks_to_gradletasknamecache $output
fi fi
compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache) compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache)
fi fi
@ -113,13 +107,7 @@ _gradlew_tasks () {
if [[ -f build.gradle ]]; then if [[ -f build.gradle ]]; then
_gradle_arguments _gradle_arguments
if _gradle_does_task_list_need_generating; then if _gradle_does_task_list_need_generating; then
# First empty the old cache file _gradle_parse_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
if [ -f .gradletasknamecache ]; then
rm .gradletasknamecache
fi
# Then append the rest of the lines to that file
output=$(./gradlew tasks --all)
_gradle_parse_tasks_to_gradletasknamecache $output
fi fi
compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache) compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache)
fi fi