forked from gradle/gradle-completion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_gradle
329 lines (302 loc) · 17.7 KB
/
_gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#compdef gradle gradlew gw
__gradle-set-project-root-dir() {
local dir=`pwd`
project_root_dir=`pwd`
while [[ $dir != '/' ]]; do
if [[ -f "$dir/settings.gradle" || -f "$dir/gradlew" ]]; then
project_root_dir=$dir
return 0
fi
dir="$(dirname "$dir")"
done
return 1
}
__gradle-init-cache-dir() {
cache_dir="$HOME/.gradle/completion"
mkdir -p $cache_dir
}
__gradle-set-build-file() {
# Look for default build script in the settings file (settings.gradle by default)
# Otherwise, default is the file 'build.gradle' in the current directory.
local gradle_settings_file=${${(v)opt_args[(i)-c|--settings-file]}:-"$project_root_dir/settings.gradle"}
local default_gradle_build_file="$project_root_dir/build.gradle"
if [[ -f $gradle_settings_file ]]; then
default_gradle_build_file=${$(grep "^rootProject\.buildFileName" $gradle_settings_file | \
sed -n -e "s/rootProject\.buildFileName = [\'\"]\(.*\)[\'\"]/\1/p")}
default_gradle_build_file="$project_root_dir/${default_gradle_build_file:-build.gradle}"
fi
# If a build file is specified after '-b' or '--build-file', use this file.
gradle_build_file=${${(v)opt_args[(i)-b|--build-file]}:-$default_gradle_build_file}
}
__gradle-set-cache-name() {
# Cache name is constructed from the absolute path of the build file.
cache_name=${${gradle_build_file:a}//[^[:alnum:]]/_}
}
__gradle-set-files-checksum() {
# Cache MD5 sum of all Gradle scripts and modified timestamps
if builtin command -v md5 > /dev/null; then
gradle_files_checksum=( $(md5 -q -s "$(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null)") )
elif builtin command -v md5sum > /dev/null; then
gradle_files_checksum=( $(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null | md5sum | awk '{print $1}') )
else
_message 'Cannot generate completions as neither md5 nor md5sum exist on \$PATH'
return 1
fi
}
__gradle-generate-script-cache() {
# Invalidate cache after 3 weeks by default
local cache_ttl_mins=${$(echo $GRADLE_CACHE_TTL_MINUTES):-30240}
local script_exclude_pattern=${$(echo $GRADLE_COMPLETION_EXCLUDE_PATTERN):-"/(build|integTest|out)/"}
if [[ ! $(find $cache_dir/$cache_name -mmin -$cache_ttl_mins 2>/dev/null) ]]; then
zle -R "Generating Gradle build script cache"
# Cache all Gradle scripts
local -a gradle_build_scripts
gradle_build_scripts=( $(find $project_root_dir -type f -name "*.gradle" -o -name "*.gradle.kts" 2>/dev/null | egrep -v "$script_exclude_pattern") )
printf "%s\n" "${gradle_build_scripts[@]}" > $cache_dir/$cache_name
fi
}
__gradle-generate-tasks-cache() {
__gradle-set-files-checksum
# Use Gradle wrapper when it exists.
local gradle_cmd="gradle"
if [[ -x "$project_root_dir/gradlew" ]]; then
gradle_cmd="$project_root_dir/gradlew"
fi
zle -R "Generating Gradle task cache from $gradle_build_file"
# Run gradle to retrieve possible tasks and cache.
# Reuse Gradle Daemon if IDLE but don't start a new one.
local gradle_tasks_output
if [[ ! -z "$($gradle_cmd --status 2>/dev/null | grep IDLE)" ]]; then
gradle_tasks_output="$($gradle_cmd --daemon --build-file $gradle_build_file -q tasks --all)"
else
gradle_tasks_output="$($gradle_cmd --no-daemon --build-file $gradle_build_file -q tasks --all)"
fi
local gradle_all_tasks="" root_tasks="" subproject_tasks="" output_line
local -a match
for output_line in ${(f)"$(printf "%s\n" "${gradle_tasks_output[@]}")"}; do
if [[ $output_line =~ ^([[:lower:]][[:alnum:][:punct:]]*)([[:space:]]-[[:space:]]([[:print:]]*))? ]]; then
local task_name="${match[1]}"
local task_description="${match[3]}"
# Completion for subproject tasks with ':' prefix
if [[ $task_name =~ ^([[:alnum:][:punct:]]+):([[:alnum:]]+) ]]; then
gradle_all_tasks+="${task_name//:/\\:}:$task_description\n\\:${task_name//:/\\:}:$task_description\n"
subproject_tasks+="${match[2]}\n"
else
gradle_all_tasks+="${task_name//:/\\:}:$task_description\n"
root_tasks+="$task_name\n"
fi
fi
done
# subproject tasks can be referenced implicitly from root project
if [[ $GRADLE_COMPLETION_UNQUALIFIED_TASKS == "true" ]]; then
local -a implicit_tasks
implicit_tasks=( $(comm -23 <(echo $subproject_tasks | sort) <(echo $root_tasks | sort)) )
for task in $(printf "%s\n" "${implicit_tasks[@]}"); do
gradle_all_tasks+="$task\n"
done
fi
echo $gradle_all_tasks > $cache_dir/$gradle_files_checksum
echo $gradle_files_checksum > $cache_dir/$cache_name.md5
}
__gradle-completion-init() {
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir
__gradle-init-cache-dir
__gradle-set-project-root-dir
__gradle-set-build-file
if [[ -f $gradle_build_file ]]; then
__gradle-set-cache-name
__gradle-generate-script-cache
__gradle-set-files-checksum
__gradle-generate-tasks-cache
fi
return 0
}
__gradle_tasks() {
local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir
__gradle-init-cache-dir
__gradle-set-project-root-dir
__gradle-set-build-file
if [[ -f $gradle_build_file ]]; then
__gradle-set-cache-name
__gradle-generate-script-cache
__gradle-set-files-checksum
# The cache key is md5 sum of all gradle scripts, so it's valid if it exists.
if [[ -f $cache_dir/$cache_name.md5 ]]; then
local cached_checksum="$(cat $cache_dir/$cache_name.md5)"
local -a cached_tasks
if [[ -z $cur ]]; then
cached_tasks=(${(f)"$(cat $cache_dir/$cached_checksum)"})
else
cached_tasks=(${(f)"$(grep "^${cur//:/\\\\:}" $cache_dir/$cached_checksum)"})
fi
_describe 'all tasks' cached_tasks && ret=0
else
__gradle-generate-tasks-cache
fi
# Regenerate tasks cache in the background
if [[ $gradle_files_checksum != "$(cat $cache_dir/$cache_name.md5)" || ! -f $cache_dir/$gradle_files_checksum ]]; then
$(__gradle-generate-tasks-cache 1>&2 2>/dev/null &)
fi
else
_describe 'built-in tasks' '(
"buildEnvironment:Displays all buildscript dependencies declared in root project."
"components:Displays the components produced by root project."
"dependencies:Displays all dependencies declared in root project."
"dependencyInsight:Displays the insight into a specific dependency in root project."
"dependentComponents:Displays the dependent components of components in root project."
"help:Displays a help message."
"init:Initializes a new Gradle build."
"model:Displays the configuration model of root project."
"projects:Displays the sub-projects of root project."
"properties:Displays the properties of root project."
"tasks:Displays the tasks runnable from root project."
"wrapper:Generates Gradle wrapper files."
)' && ret=0
fi
}
__gradle_subcommand() {
integer ret=1
case "$words[1]" in
(init)
_arguments \
'--type=[Project type to generate.]:*:project type:(basic groovy-library java-library pom scala-library)' && ret=0
;;
(tasks)
_arguments \
'--all[List all tasks, including subproject tasks]' && ret=0
;;
(wrapper)
_arguments \
'--distribution-type=[Binary-only or all with docs and sources]:*:distribution type:(bin all)' \
'--gradle-version=[Set Gradle version for wrapper]' \
'--gradle-distribution-url[Set Gradle distribution URL]' && ret=0
;;
(*)
_arguments -C \
{-a,--no-rebuild}'[Do not rebuild project dependencies.]' \
'(--no-build-cache)--build-cache[Enable the Gradle build cache.]' \
{-b,--build-file}'[Specifies the build file.]:build script:_files -g \*.gradle' \
{-C,--cache}'[Specifies how compiled build scripts should be cached.]:cache policy:(on rebuild)' \
{-c,--settings-file}'[Specifies the settings file.]:settings file:_files -g \*.gradle' \
'--configure-on-demand[Only relevant projects are configured in this build run.]' \
'--console[Specifies which type of console output to generate.]:console output type:(plain auto rich)' \
'--continue[Continues task execution after a task failure.]' \
'-Dorg.gradle.cache.reserved.mb=[Reserve Gradle Daemon memory for operations.]' \
'-Dorg.gradle.daemon.debug=[Set true to debug Gradle Daemon.]' \
'-Dorg.gradle.daemon.idletimeout=[Kill Gradle Daemon after # idle millis.]' \
'-Dorg.gradle.debug=[Set true to debug Gradle Client.]' \
'-Dorg.gradle.jvmargs=[Set JVM arguments.]' \
'-Dorg.gradle.java.home=[Set JDK home dir.]' \
'-Dorg.gradle.parallel=[Set true to enable parallel project builds.]' \
'-Dorg.gradle.parallel.intra=[Set true to enable intra-project parallel builds.]' \
'-Dorg.gradle.workers.max=[Set the number of workers Gradle is allowed to use.]' \
'(-i --info -w --warn -q --quiet)'{-d,--debug}'[Log in debug mode (includes normal stacktrace).]' \
'(--no-daemon)--daemon[Uses the Gradle daemon to run the build. Starts the daemon if not running.]' \
'--foreground[Starts the Gradle daemon in the foreground.]' \
{-g,--gradle-user-home}'[Specifies the gradle user home directory.]:file:_directories' \
'--include-build[Includes the specified build in the composite.]:file:_directories' \
{-I,--init-script}'[Specifies an initialization script.]:init script:_files -g \*.gradle' \
'(-d --debug -w --warn -q --quiet)'{-i,--info}'[Set log level to info.]' \
'--max-workers[Set the maximum number of concurrent workers that Gradle may use.]:number workers' \
{-m,--dry-run}'[Runs the builds with all task actions disabled.]' \
'--no-color[Do not use color in the console output. (Removed in Gradle 3.0)]' \
'(--build-cache)--no-build-cache[Do not use the Gradle build cache.]' \
'(--daemon)--no-daemon[Do not use the Gradle daemon to run the build.]' \
'(--scan)--no-scan[Do not create a build scan.]' \
'--offline[The build should operate without accessing network resources.]' \
\*{-P+,--project-prop}'[Set project property for the build script (e.g. -Pmyprop=myvalue).]:project property (prop=val):' \
{-p,--project-dir}'[Specifies the start directory for Gradle.]:start directory:_directories' \
'--parallel[Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use.]' \
'--profile[Profiles build execution time and generates a report in the <build_dir>/reports/profile directory.]' \
'--project-cache-dir[Specifies the project-specific cache directory.]:cache directory:_directories' \
'(-d --debug -w --warn -i --info)'{-q,--quiet}'[Log errors only.]' \
'--recompile-scripts[Force build script recompiling.]' \
'--refresh[Refresh the state of resources of the type(s) specified.]:refresh policy:(dependencies)' \
'--refresh-dependencies[Refresh the state of dependencies.]' \
'--rerun-tasks[Ignore previously cached task results.]' \
'(--no-scan)--scan[Create a build scan.]' \
'(-S --full-stacktrace)'{-s,--stacktrace}'[Print out the stacktrace for all exceptions.]' \
'(-s --stacktrace)'{-S,--full-stacktrace}'[Print out the full (very verbose) stacktrace for all exceptions.]' \
'--system-prop[system property (prop=val)]' \
{-t,--continuous}'[Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change.]' \
{-u,--no-search-upward}"[Don't search in parent folders for a settings.gradle file.]" \
'(-d --debug -q --quiet -i --info)'{-w,--warn}'[Log warnings and errors only.]' \
{-x,--exclude-task}'[Specify a task to be excluded from execution.]' && ret=0
;;
esac
return ret
}
_gradle() {
local cur=${words[CURRENT]}
local curcontext="$curcontext" state
integer ret=1
typeset -A opt_args
_arguments -C \
'(-)'{-\?,-h,--help}'[Shows a help message.]' \
{-a,--no-rebuild}'[Do not rebuild project dependencies.]' \
'(--no-build-cache)--build-cache[Enable the Gradle build cache.]' \
{-b,--build-file}'[Specifies the build file.]:build script:_files -g \*.gradle' \
{-C,--cache}'[Specifies how compiled build scripts should be cached.]:cache policy:(on rebuild)' \
{-c,--settings-file}'[Specifies the settings file.]:settings file:_files -g \*.gradle' \
'--configure-on-demand[Only relevant projects are configured in this build run.]' \
'--console[Specifies which type of console output to generate.]:console output type:(plain auto rich)' \
'--continue[Continues task execution after a task failure.]' \
'-Dorg.gradle.cache.reserved.mb=[Reserve Gradle Daemon memory for operations.]' \
'-Dorg.gradle.daemon.debug=[Set true to debug Gradle Daemon.]' \
'-Dorg.gradle.daemon.idletimeout=[Kill Gradle Daemon after # idle millis.]' \
'-Dorg.gradle.debug=[Set true to debug Gradle Client.]' \
'-Dorg.gradle.jvmargs=[Set JVM arguments.]' \
'-Dorg.gradle.java.home=[Set JDK home dir.]' \
'-Dorg.gradle.parallel=[Set true to enable parallel project builds.]' \
'-Dorg.gradle.parallel.intra=[Set true to enable intra-project parallel builds.]' \
'-Dorg.gradle.workers.max=[Set the number of workers Gradle is allowed to use.]' \
'(-i --info -w --warn -q --quiet)'{-d,--debug}'[Log in debug mode (includes normal stacktrace).]' \
'(--no-daemon)--daemon[Uses the Gradle daemon to run the build. Starts the daemon if not running.]' \
'--foreground[Starts the Gradle daemon in the foreground.]' \
{-g,--gradle-user-home}'[Specifies the gradle user home directory.]:home directory:_directories' \
'(-)--gui[Launches the Gradle GUI.]' \
'--include-build[Includes the specified build in the composite.]:file:_directories' \
{-I,--init-script}'[Specifies an initialization script.]:init script:_files -g \*.gradle' \
'(-d --debug -w --warn -q --quiet)'{-i,--info}'[Set log level to info.]' \
'--max-workers[Set the maximum number of concurrent workers that Gradle may use.]:number workers:-># processors' \
{-m,--dry-run}'[Runs the builds with all task actions disabled.]' \
'--no-color[Do not use color in the console output. (Removed in Gradle 3.0)]' \
'(--build-cache)--no-build-cache[Do not use the Gradle build cache.]' \
'(--daemon)--no-daemon[Do not use the Gradle daemon to run the build.]' \
'(--scan)--no-scan[Do not create a build scan.]' \
'--offline[The build should operate without accessing network resources.]' \
\*{-P+,--project-prop}'[Set project property for the build script (e.g. -Pmyprop=myvalue).]:project property (prop=val):' \
{-p,--project-dir}'[Specifies the start directory for Gradle.]:start directory:_directories' \
'--parallel[Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use.]' \
'--profile[Profiles build execution time and generates a report in the <build_dir>/reports/profile directory.]' \
'--project-cache-dir[Specifies the project-specific cache directory.]:cache directory:_directories' \
'(-d --debug -w --warn -i --info)'{-q,--quiet}'[Log errors only.]' \
'--recompile-scripts[Force build script recompiling.]' \
'--refresh[Refresh the state of resources of the type(s) specified.]:refresh policy:(dependencies)' \
'--refresh-dependencies[Refresh the state of dependencies.]' \
'--rerun-tasks[Ignore previously cached task results.]' \
'(--no-scan)--scan[Create a build scan.]' \
'(-S --full-stacktrace)'{-s,--stacktrace}'[Print out the stacktrace for all exceptions.]' \
'(-s --stacktrace)'{-S,--full-stacktrace}'[Print out the full (very verbose) stacktrace for all exceptions.]' \
'(-)--status[Shows status of running and recently stopped Gradle Daemons.]' \
'(-)--stop[Stops all Gradle daemons.]' \
'--system-prop[system property (prop=val)]' \
{-t,--continuous}'[Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change.]' \
{-u,--no-search-upward}"[Don't search in parent folders for a settings.gradle file.]" \
'(-)'{-v,--version}'[Print version info.]' \
'(-d --debug -q --quiet -i --info)'{-w,--warn}'[Log warnings and errors only.]' \
{-x,--exclude-task}'[Specify a task to be excluded from execution.]' \
'(-): :->task' \
'(-)*:: :->option-or-argument' && ret=0
case $state in
(task)
__gradle_tasks && ret=0
;;
(option-or-argument)
curcontext=${curcontext%:*:*}:gradle-$words[1]:
__gradle_subcommand && ret=0
;;
esac
return ret
}
_gradle "$@"