22
22
23
23
# Bash breaks words on : by default. Subproject tasks have ':'
24
24
# Avoid inaccurate completions for subproject tasks
25
- COMP_WORDBREAKS=$( echo " $ COMP_WORDBREAKS" | sed -e ' s/://g ' )
25
+ COMP_WORDBREAKS=" ${ COMP_WORDBREAKS//:/ } "
26
26
27
27
function __gradle-set-project-root-dir() {
28
28
project_root_dir=" $( _bash-it-find-in-ancestor " settings.gradle" " gradlew" ) "
@@ -31,31 +31,32 @@ function __gradle-set-project-root-dir() {
31
31
32
32
__gradle-init-cache-dir () {
33
33
cache_dir=" $HOME /.gradle/completion"
34
- mkdir -p $cache_dir
34
+ mkdir -p " $cache_dir "
35
35
}
36
36
37
37
__gradle-set-build-file () {
38
38
# Look for default build script in the settings file (settings.gradle by default)
39
39
# Otherwise, default is the file 'build.gradle' in the current directory.
40
40
gradle_build_file=" $project_root_dir /build.gradle"
41
41
if [[ -f " $project_root_dir /settings.gradle" ]]; then
42
- local build_file_name=$( grep " ^rootProject\.buildFileName" " $project_root_dir /settings.gradle" \
42
+ local build_file_name
43
+ build_file_name=$( grep " ^rootProject\.buildFileName" " $project_root_dir /settings.gradle" \
43
44
| sed -n -e " s/rootProject\.buildFileName = [\'\" ]\(.*\)[\'\" ]/\1/p" )
44
45
gradle_build_file=" $project_root_dir /${build_file_name:- build.gradle} "
45
46
fi
46
47
}
47
48
48
49
__gradle-set-cache-name () {
49
50
# Cache name is constructed from the absolute path of the build file.
50
- cache_name=$( echo $ gradle_build_file | sed -e ' s/\//_/g ' )
51
+ cache_name=${ gradle_build_file// ' / ' / _}
51
52
}
52
53
53
54
__gradle-set-files-checksum () {
54
55
# Cache MD5 sum of all Gradle scripts and modified timestamps
55
56
if _command_exists md5; then
56
- gradle_files_checksum=$( md5 -q -s " $( cat " $cache_dir /$cache_name " | xargs ls -o 2> /dev/null) " )
57
+ gradle_files_checksum=$( md5 -q -s " $( xargs ls -o < " $cache_dir /$cache_name " 2> /dev/null) " )
57
58
elif _command_exists md5sum; then
58
- gradle_files_checksum=$( cat " $cache_dir /$cache_name " | xargs ls -o 2> /dev/null | md5sum | awk ' {print $1}' )
59
+ gradle_files_checksum=$( xargs ls -o < " $cache_dir /$cache_name " 2> /dev/null | md5sum | awk ' {print $1}' )
59
60
else
60
61
echo " Cannot generate completions as neither md5 nor md5sum exist on \$ PATH"
61
62
fi
@@ -66,10 +67,15 @@ __gradle-generate-script-cache() {
66
67
local cache_ttl_mins=${GRADLE_CACHE_TTL_MINUTES:- 30240}
67
68
local script_exclude_pattern=${GRADLE_COMPLETION_EXCLUDE_PATTERN:- " /(build|integTest|out)/" }
68
69
69
- if [[ ! $( find $cache_dir /$cache_name -mmin -$cache_ttl_mins 2> /dev/null) ]]; then
70
+ if [[ ! $( find " $cache_dir /$cache_name " -mmin " -$cache_ttl_mins " 2> /dev/null) ]]; then
70
71
# Cache all Gradle scripts
71
- local gradle_build_scripts=$( find $project_root_dir -type f -name " *.gradle" -o -name " *.gradle.kts" 2> /dev/null | grep -E -v " $script_exclude_pattern " )
72
- printf " %s\n" " ${gradle_build_scripts[@]} " > $cache_dir /$cache_name
72
+ local gradle_build_scripts
73
+ gradle_build_scripts=$(
74
+ find " $project_root_dir " -type f \
75
+ \( -name " *.gradle" -o -name " *.gradle.kts" \) 2> /dev/null \
76
+ | grep -E -v " $script_exclude_pattern "
77
+ )
78
+ printf " %s\n" " ${gradle_build_scripts[@]} " > " $cache_dir /$cache_name "
73
79
fi
74
80
}
75
81
@@ -115,7 +121,10 @@ __gradle-long-options() {
115
121
--system-prop - Set a system property
116
122
--version - Prints Gradle version info
117
123
--warn - Log warnings and errors only"
118
- COMPREPLY=($( compgen -W " $args " -- " ${COMP_WORDS[COMP_CWORD]} " ) )
124
+ COMPREPLY=()
125
+ while IFS=' ' read -r line; do
126
+ COMPREPLY+=(" $line " )
127
+ done < <( compgen -W " $args " -- " ${COMP_WORDS[COMP_CWORD]} " )
119
128
}
120
129
121
130
__gradle-properties () {
@@ -130,7 +139,10 @@ __gradle-properties() {
130
139
-Dorg.gradle.parallel= - Set true to enable parallel project builds (incubating)
131
140
-Dorg.gradle.parallel.intra= - Set true to enable intra-project parallel builds (incubating)
132
141
-Dorg.gradle.workers.max= - Set the number of workers Gradle is allowed to use"
133
- COMPREPLY=($( compgen -W " $args " -- " ${COMP_WORDS[COMP_CWORD]} " ) )
142
+ COMPREPLY=()
143
+ while IFS=' ' read -r line; do
144
+ COMPREPLY+=(" $line " )
145
+ done < <( compgen -W " $args " -- " ${COMP_WORDS[COMP_CWORD]} " )
134
146
return 0
135
147
}
136
148
@@ -156,7 +168,8 @@ __gradle-short-options() {
156
168
-I - Specifies an initialization script
157
169
-P - Sets a project property of the root project
158
170
-S - Print out the full (very verbose) stacktrace"
159
- COMPREPLY=($( compgen -W " $args " -- " ${COMP_WORDS[COMP_CWORD]} " ) )
171
+ COMPREPLY=()
172
+ while IFS=' ' read -r line; do COMPREPLY+=(" $line " ); done < <( compgen -W " $args " -- " ${COMP_WORDS[COMP_CWORD]} " )
160
173
}
161
174
162
175
__gradle-notify-tasks-cache-build () {
@@ -179,10 +192,10 @@ __gradle-generate-tasks-cache() {
179
192
# Run gradle to retrieve possible tasks and cache.
180
193
# Reuse Gradle Daemon if IDLE but don't start a new one.
181
194
local gradle_tasks_output
182
- if [[ ! -z " $( $ gradle_cmd --status 2> /dev/null | grep IDLE ) " ]] ; then
183
- gradle_tasks_output=" $( $gradle_cmd -b $gradle_build_file --daemon -q tasks --all) "
195
+ if " $ gradle_cmd" --status 2> /dev/null | grep -q IDLE ; then
196
+ gradle_tasks_output=" $( " $gradle_cmd " -b " $gradle_build_file " --daemon -q tasks --all) "
184
197
else
185
- gradle_tasks_output=" $( $gradle_cmd -b $gradle_build_file --no-daemon -q tasks --all) "
198
+ gradle_tasks_output=" $( " $gradle_cmd " -b " $gradle_build_file " --no-daemon -q tasks --all) "
186
199
fi
187
200
local output_line
188
201
local task_description
@@ -206,15 +219,18 @@ __gradle-generate-tasks-cache() {
206
219
207
220
# subproject tasks can be referenced implicitly from root project
208
221
if [[ $GRADLE_COMPLETION_UNQUALIFIED_TASKS == " true" ]]; then
209
- local -a implicit_tasks=()
210
- implicit_tasks=($( comm -23 <( printf " %s\n" " ${subproject_tasks[@]} " | sort) <( printf " %s\n" " ${root_tasks[@]} " | sort) ) )
211
- for task in $( printf " %s\n" " ${implicit_tasks[@]} " ) ; do
212
- gradle_all_tasks+=($task )
222
+ local -a implicit_tasks
223
+ while IFS=' ' read -r line; do
224
+ implicit_tasks+=(" $line " )
225
+ done < <( comm -23 <( printf " %s\n" " ${subproject_tasks[@]} " | sort) \
226
+ <( printf " %s\n" " ${root_tasks[@]} " | sort) )
227
+ for task in " ${implicit_tasks[@]} " ; do
228
+ gradle_all_tasks+=(" $task " )
213
229
done
214
230
fi
215
231
216
- printf " %s\n" " ${gradle_all_tasks[@]} " > $cache_dir /$gradle_files_checksum
217
- echo $gradle_files_checksum > $cache_dir /$cache_name .md5
232
+ printf " %s\n" " ${gradle_all_tasks[@]} " > " $cache_dir /$gradle_files_checksum "
233
+ echo " $gradle_files_checksum " > " $cache_dir /$cache_name .md5"
218
234
}
219
235
220
236
__gradle-completion-init () {
@@ -262,22 +278,30 @@ _gradle() {
262
278
__gradle-set-files-checksum
263
279
264
280
# The cache key is md5 sum of all gradle scripts, so it's valid if it exists.
265
- if [[ -f $cache_dir /$cache_name .md5 ]]; then
266
- local cached_checksum=" $( cat $cache_dir /$cache_name .md5) "
281
+ if [[ -f " $cache_dir /$cache_name .md5" ]]; then
282
+ local cached_checksum
283
+ cached_checksum=" $( cat " $cache_dir /$cache_name .md5" ) "
267
284
local -a cached_tasks
268
285
if [[ -z $cur ]]; then
269
- cached_tasks=($( cat $cache_dir /$cached_checksum ) )
286
+ while IFS=' ' read -r line; do
287
+ cached_tasks+=(" $line " )
288
+ done < " $cache_dir /$cached_checksum "
270
289
else
271
- cached_tasks=($( grep " ^$cur " $cache_dir /$cached_checksum ) )
290
+ while IFS=' ' read -r line; do
291
+ cached_tasks+=(" $line " )
292
+ done < <( grep " ^$cur " " $cache_dir /$cached_checksum " )
272
293
fi
273
- COMPREPLY=($( compgen -W " ${cached_tasks[*]} " -- " $cur " ) )
294
+ while IFS=' ' read -r line; do
295
+ COMPREPLY+=(" $line " )
296
+ done < <( compgen -W " ${cached_tasks[*]} " -- " $cur " )
274
297
else
275
298
__gradle-notify-tasks-cache-build
276
299
fi
277
300
278
301
# Regenerate tasks cache in the background
279
- if [[ $gradle_files_checksum != " $( cat $cache_dir /$cache_name .md5) " || ! -f $cache_dir /$gradle_files_checksum ]]; then
280
- $( __gradle-generate-tasks-cache 1>&2 2> /dev/null & )
302
+ if [[ $gradle_files_checksum != " $( cat " $cache_dir /$cache_name .md5" ) " ||
303
+ ! -f " $cache_dir /$gradle_files_checksum " ]]; then
304
+ (__gradle-generate-tasks-cache & > /dev/null & )
281
305
fi
282
306
else
283
307
# Default tasks available outside Gradle projects
@@ -293,15 +317,18 @@ projects - Displays the sub-projects of root project.
293
317
properties - Displays the properties of root project.
294
318
tasks - Displays the tasks runnable from root project.
295
319
wrapper - Generates Gradle wrapper files."
296
- COMPREPLY=($( compgen -W " $args " -- " ${COMP_WORDS[COMP_CWORD]} " ) )
320
+ COMPREPLY=()
321
+ while IFS=' ' read -r line; do
322
+ COMPREPLY+=(" $line " )
323
+ done < <( compgen -W " ${args} " -- " ${cur} " )
297
324
fi
298
325
fi
299
326
300
327
IFS=" $OLDIFS "
301
328
302
329
# Remove description ("[:space:]" and after) if only one possibility
303
330
if [[ ${# COMPREPLY[*]} -eq 1 ]]; then
304
- COMPREPLY=(${COMPREPLY[0]%% * } )
331
+ COMPREPLY=(" ${COMPREPLY[0]%% * } " )
305
332
fi
306
333
307
334
return 0
0 commit comments