1 #compdef gradle gradlew gw
3 # Taken from https://github.com/gradle/gradle-completion
4 # Copyright (c) 2017 Eric Wendelin
6 # Permission is hereby granted, free of charge, to any person obtaining a copy of
7 # this software and associated documentation files (the "Software"), to deal in
8 # the Software without restriction, including without limitation the rights to
9 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 # of the Software, and to permit persons to whom the Software is furnished to do
11 # so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included in all
14 # copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 __gradle-set-project-root-dir() {
27 project_root_dir=`pwd`
28 while [[ $dir != '/' ]]; do
29 if [[ -f "$dir/settings.gradle" || -f "$dir/settings.gradle.kts" || -f "$dir/gradlew" ]]; then
33 dir="$(dirname "$dir")"
38 __gradle-init-cache-dir() {
39 cache_dir="$HOME/.gradle/completion"
43 __gradle-set-settings-file() {
44 # In order of precedence: --settings-file=filename, settings.gradle, settings.gradle.kts
46 local default_gradle_settings_file="$project_root_dir/settings.gradle"
47 if [[ ! -f $default_gradle_settings_file ]]; then
48 default_gradle_settings_file="$project_root_dir/settings.gradle.kts"
50 gradle_settings_file=${${(v)opt_args[(i)-c|--settings-file]}:-$default_gradle_settings_file}
53 __gradle-set-build-file() {
54 __gradle-set-settings-file
55 # In order of precedence: --build-file=filename, rootProject.buildFileName, build.gradle, build.gradle.kts
57 local default_gradle_build_file_name="build.gradle"
58 if [[ -r $gradle_settings_file ]]; then
59 default_gradle_build_file_name=${$(grep "^rootProject\.buildFileName" $gradle_settings_file | \
60 sed -n -e "s/rootProject\.buildFileName = [\'\"]\(.*\)[\'\"]/\1/p")}
62 default_gradle_build_file_name="${default_gradle_build_file:-build.gradle}"
65 local default_gradle_build_file="$project_root_dir/$default_gradle_build_file_name"
66 if [[ ! -f $default_gradle_build_file ]]; then
67 default_gradle_build_file="$project_root_dir/build.gradle.kts"
70 # If a build file is specified after '-b' or '--build-file', use this file.
71 gradle_build_file=${${(v)opt_args[(i)-b|--build-file]}:-$default_gradle_build_file}
74 __gradle-set-cache-name() {
75 # Cache name is constructed from the absolute path of the build file.
76 cache_name=${${gradle_build_file:a}//[^[:alnum:]]/_}
79 __gradle-set-files-checksum() {
80 # Cache MD5 sum of all Gradle scripts and modified timestamps
81 if builtin command -v md5 > /dev/null; then
82 gradle_files_checksum=( $(md5 -q -s "$(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null)") )
83 elif builtin command -v md5sum > /dev/null; then
84 gradle_files_checksum=( $(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null | md5sum | awk '{print $1}') )
86 _message 'Cannot generate completions as neither md5 nor md5sum exist on \$PATH'
91 __gradle-generate-script-cache() {
92 # Invalidate cache after 3 weeks by default
93 local cache_ttl_mins=${$(echo $GRADLE_CACHE_TTL_MINUTES):-30240}
94 local script_exclude_pattern=${$(echo $GRADLE_COMPLETION_EXCLUDE_PATTERN):-"/(.git|build|integTest|samples|templates|smokeTest|testFixtures|out)/"}
95 if [[ ! $(find $cache_dir/$cache_name -mmin -$cache_ttl_mins 2>/dev/null) ]]; then
96 zle -R "Generating Gradle build script cache"
97 # Cache all Gradle scripts
98 local -a gradle_build_scripts
99 gradle_build_scripts=( $(find $project_root_dir -type f -name "*.gradle" -o -name "*.gradle.kts" 2>/dev/null | grep -E -v "$script_exclude_pattern") )
100 printf "%s\n" "${gradle_build_scripts[@]}" >| $cache_dir/$cache_name
104 __gradle-generate-tasks-cache() {
105 __gradle-set-files-checksum
107 # Use Gradle wrapper when it exists.
108 local gradle_cmd="gradle"
109 if [[ -x "$project_root_dir/gradlew" ]]; then
110 gradle_cmd="$project_root_dir/gradlew"
113 zle -R "Generating Gradle task cache from $gradle_build_file"
115 # Run gradle to retrieve possible tasks and cache.
116 # Reuse Gradle Daemon if IDLE but don't start a new one.
117 local gradle_tasks_output
118 if [[ ! -z "$($gradle_cmd --status 2>/dev/null | grep IDLE)" ]]; then
119 gradle_tasks_output="$($gradle_cmd --daemon --build-file $gradle_build_file --console plain -q tasks --all 2>/dev/null)"
121 gradle_tasks_output="$($gradle_cmd --no-daemon --build-file $gradle_build_file --console plain -q tasks --all 2>/dev/null)"
123 local gradle_all_tasks="" root_tasks="" subproject_tasks="" output_line
125 for output_line in ${(f)"$(printf "%s\n" "${gradle_tasks_output[@]}")"}; do
126 if [[ $output_line =~ ^([[:lower:]][[:alnum:][:punct:]]*)([[:space:]]-[[:space:]]([[:print:]]*))? ]]; then
127 local task_name="${match[1]}"
128 local task_description="${match[3]}"
129 # Completion for subproject tasks with ':' prefix
130 if [[ $task_name =~ ^([[:alnum:][:punct:]]+):([[:alnum:]]+) ]]; then
131 gradle_all_tasks+="${task_name//:/\\:}:$task_description\n\\:${task_name//:/\\:}:$task_description\n"
132 subproject_tasks+="${match[2]}\n"
134 gradle_all_tasks+="${task_name//:/\\:}:$task_description\n"
135 root_tasks+="$task_name\n"
140 # subproject tasks can be referenced implicitly from root project
141 if [[ $GRADLE_COMPLETION_UNQUALIFIED_TASKS == "true" ]]; then
142 local -a implicit_tasks
143 implicit_tasks=( $(comm -23 <(echo $subproject_tasks | sort) <(echo $root_tasks | sort)) )
144 for task in $(printf "%s\n" "${implicit_tasks[@]}"); do
145 gradle_all_tasks+="$task\n"
149 echo $gradle_all_tasks >| $cache_dir/$gradle_files_checksum
150 echo $gradle_files_checksum >| $cache_dir/$cache_name.md5
153 __gradle-completion-init() {
154 local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir
155 __gradle-init-cache-dir
156 __gradle-set-project-root-dir
157 __gradle-set-build-file
158 if [[ -f $gradle_build_file ]]; then
159 __gradle-set-cache-name
160 __gradle-generate-script-cache
161 __gradle-set-files-checksum
162 __gradle-generate-tasks-cache
168 local cache_dir cache_name gradle_build_file gradle_files_checksum project_root_dir
170 __gradle-init-cache-dir
171 __gradle-set-project-root-dir
172 __gradle-set-build-file
173 if [[ -f $gradle_build_file ]]; then
174 __gradle-set-cache-name
175 __gradle-generate-script-cache
176 __gradle-set-files-checksum
178 # The cache key is md5 sum of all gradle scripts, so it's valid if it exists.
179 if [[ -f $cache_dir/$cache_name.md5 ]]; then
180 local cached_checksum="$(cat $cache_dir/$cache_name.md5)"
181 local -a cached_tasks
182 if [[ -z $cur ]]; then
183 cached_tasks=(${(f)"$(cat $cache_dir/$cached_checksum)"})
185 cached_tasks=(${(f)"$(grep "^${cur//:/\\\\:}" $cache_dir/$cached_checksum)"})
187 _describe 'all tasks' cached_tasks && ret=0
189 __gradle-generate-tasks-cache
192 # Regenerate tasks cache in the background
193 if [[ $gradle_files_checksum != "$(cat $cache_dir/$cache_name.md5)" || ! -f $cache_dir/$gradle_files_checksum || $(wc -c < $cache_dir/$gradle_files_checksum) -le 1 ]]; then
194 $(__gradle-generate-tasks-cache 1>&2 2>/dev/null &)
197 _describe 'built-in tasks' '(
198 "buildEnvironment:Displays all buildscript dependencies declared in root project."
199 "components:Displays the components produced by root project."
200 "dependencies:Displays all dependencies declared in root project."
201 "dependencyInsight:Displays the insight into a specific dependency in root project."
202 "dependentComponents:Displays the dependent components of components in root project."
203 "help:Displays a help message."
204 "init:Initializes a new Gradle build."
205 "model:Displays the configuration model of root project."
206 "projects:Displays the sub-projects of root project."
207 "properties:Displays the properties of root project."
208 "tasks:Displays the tasks runnable from root project."
209 "wrapper:Generates Gradle wrapper files."
214 __gradle_subcommand() {
220 '--configuration=[The configuration to generate the report for.]:dependency configuration:_gradle_dependency_configurations' && ret=0
224 '--dependency=[Shows the details of given dependency.]' \
225 '--configuration=[Looks for the dependency in given configuration.]:dependency configuration:_gradle_dependency_configurations' && ret=0
229 '--task[The task to show help for.]' && ret=0
233 '--dsl=[DSL to be used in generated scripts.]:dsl:(groovy kotlin)' \
234 '--package=[Package for the generated source.]' \
235 '--project-name=[Name of the generated project.]' \
236 '--test-framework=[Test framework to be used.]:test framework:(junit kotlintest scalatest spock testng)' \
237 '--type=[Project type to generate.]:project type:(basic cpp-application cpp-library groovy-application groovy-library java-application java-library kotlin-application kotlin-library pom scala-library)' && ret=0
241 '--all[List all tasks, including subproject tasks.]' \
242 '--group=[Show tasks only from given task group.]' && ret=0
246 '--debug-jvm[Enable debugging for the test process. The process is started suspended and listening on port 5005. Requires the "java" plugin.]' \
247 '--fail-fast[Stops test execution after the first failed test. Requires the "java" plugin.]' \
248 '--tests=[Sets test class or method name to be included, * is supported. Requires the "java" plugin.]' \
249 '(-)*:: :->task-or-option' && ret=0
253 '--distribution-type=[Binary-only or all with docs and sources]:*:distribution type:(bin all)' \
254 '--gradle-version=[Set Gradle version for wrapper]' \
255 '--gradle-distribution-sha256-sum=[SHA-256 checksum]' \
256 '--gradle-distribution-url=[Set Gradle distribution URL]' && ret=0
260 {-a,--no-rebuild}'[Do not rebuild project dependencies.]' \
261 '(--no-build-cache)--build-cache[Enable the Gradle build cache.]' \
262 {-b,--build-file}'[Specifies the build file.]:build script:_files -g \*.gradle' \
263 {-C,--cache}'[Specifies how compiled build scripts should be cached.]:cache policy:(on rebuild)' \
264 {-c,--settings-file}'[Specifies the settings file.]:settings file:_files -g \*.gradle' \
265 '(--no-configure-on-demand)--configure-on-demand[Only relevant projects are configured in this build run.]' \
266 '--console=[Specifies which type of console output to generate.]:console output type:(plain auto rich verbose)' \
267 '--continue[Continues task execution after a task failure.]' \
268 '-Dorg.gradle.cache.reserved.mb=[Reserve Gradle Daemon memory for operations.]' \
269 '-Dorg.gradle.caching=[Set true to enable Gradle build cache.]:enable build cache:(true false)' \
270 '-Dorg.gradle.console=[Set type of console output to generate.]:console output type:(plain auto rich verbose)' \
271 '-Dorg.gradle.daemon.debug=[Set true to debug Gradle Daemon.]:enable daemon debug:(true false)' \
272 '-Dorg.gradle.daemon.idletimeout=[Kill Gradle Daemon after # idle millis.]' \
273 '-Dorg.gradle.debug=[Set true to debug Gradle Client.]' \
274 '-Dorg.gradle.jvmargs=[Set JVM arguments.]' \
275 '-Dorg.gradle.java.home=[Set JDK home dir.]' \
276 '-Dorg.gradle.logging.level=[Set default Gradle log level.]:log level:(quiet warn lifecycle info debug)' \
277 '-Dorg.gradle.parallel=[Set true to enable parallel project builds.]:enable parallel build:(true false)' \
278 '-Dorg.gradle.priority=[Set priority for Gradle worker processes.]:priority:(low normal)' \
279 '-Dorg.gradle.warning.mode=[Set types of warnings to log.]:warning level:(all summary none)' \
280 '-Dorg.gradle.workers.max=[Set the number of workers Gradle is allowed to use.]' \
281 '(-i --info -w --warn -q --quiet)'{-d,--debug}'[Log in debug mode (includes normal stacktrace).]' \
282 '(--no-daemon)--daemon[Uses the Gradle daemon to run the build. Starts the daemon if not running.]' \
283 '--foreground[Starts the Gradle daemon in the foreground.]' \
284 {-g,--gradle-user-home}'[Specifies the gradle user home directory.]:file:_directories' \
285 \*--include-build'[Includes the specified build in the composite.]:file:_directories' \
286 \*{-I,--init-script}'[Specifies an initialization script.]:init script:_files -g \*.gradle' \
287 '(-d --debug -w --warn -q --quiet)'{-i,--info}'[Set log level to info.]' \
288 '--max-workers[Set the maximum number of concurrent workers that Gradle may use.]:number workers' \
289 {-m,--dry-run}'[Runs the builds with all task actions disabled.]' \
290 '--no-color[Do not use color in the console output. (Removed in Gradle 3.0)]' \
291 '(--build-cache)--no-build-cache[Do not use the Gradle build cache.]' \
292 '(--configure-on-demand)--no-configure-on-demand[Disables configuration on demand.]' \
293 '(--daemon)--no-daemon[Do not use the Gradle daemon to run the build.]' \
294 '(--parallel)--no-parallel[Disables parallel execution to build projects.]' \
295 '(--scan)--no-scan[Do not create a build scan.]' \
296 '--offline[The build should operate without accessing network resources.]' \
297 \*{-P+,--project-prop}'[Set project property for the build script (e.g. -Pmyprop=myvalue).]:project property (prop=val):' \
298 {-p,--project-dir}'[Specifies the start directory for Gradle.]:start directory:_directories' \
299 '(--no-parallel)--parallel[Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use.]' \
300 '--profile[Profiles build execution time and generates a report in the <build_dir>/reports/profile directory.]' \
301 '--priority[Set priority for Gradle worker processes.]:priority:(low normal)' \
302 '--project-cache-dir[Specifies the project-specific cache directory.]:cache directory:_directories' \
303 '(-d --debug -w --warn -i --info)'{-q,--quiet}'[Log errors only.]' \
304 '--recompile-scripts[Force build script recompiling.]' \
305 '--refresh[Refresh the state of resources of the type(s) specified.]:refresh policy:(dependencies)' \
306 '--refresh-dependencies[Refresh the state of dependencies.]' \
307 '--rerun-tasks[Ignore previously cached task results.]' \
308 '(--no-scan)--scan[Create a build scan.]' \
309 '(-S --full-stacktrace)'{-s,--stacktrace}'[Print out the stacktrace for all exceptions.]' \
310 '(-s --stacktrace)'{-S,--full-stacktrace}'[Print out the full (very verbose) stacktrace for all exceptions.]' \
311 '--system-prop[system property (prop=val)]' \
312 {-t,--continuous}'[Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change.]' \
313 {-u,--no-search-upward}"[Don't search in parent folders for a settings.gradle file.]" \
314 '(--write-locks)--update-locks[Perform a partial update of the dependency lock.]' \
315 '(-d --debug -q --quiet -i --info)'{-w,--warn}'[Log warnings and errors only.]' \
316 '--warning-mode=[Set types of warnings to log.]:warning mode:(all summary none)' \
317 '(--update-locks)--write-locks[Persists dependency resolution for locked configurations.]' \
318 {-x,--exclude-task}'[Specify a task to be excluded from execution.]' && ret=0
325 (( $+functions[_gradle_dependency_configurations] )) ||
326 _gradle_dependency_configurations() {
331 'testCompileClasspath'
332 'testRuntimeClasspath'
334 _describe -t 'dependency configurations' "dependency configuration" configurations
338 local cur=${words[CURRENT]}
339 local curcontext="$curcontext" state
344 '(-)'{-\?,-h,--help}'[Shows a help message.]' \
345 {-a,--no-rebuild}'[Do not rebuild project dependencies.]' \
346 '(--no-build-cache)--build-cache[Enable the Gradle build cache.]' \
347 {-b,--build-file}'[Specifies the build file.]:build script:_files -g \*.gradle' \
348 {-C,--cache}'[Specifies how compiled build scripts should be cached.]:cache policy:(on rebuild)' \
349 {-c,--settings-file}'[Specifies the settings file.]:settings file:_files -g \*.gradle:->argument-expected' \
350 '(--no-configure-on-demand)--configure-on-demand[Only relevant projects are configured in this build run.]' \
351 '--console=[Specifies which type of console output to generate.]:console output type:(plain auto rich verbose)' \
352 '--continue[Continues task execution after a task failure.]' \
353 '-Dorg.gradle.cache.reserved.mb=[Reserve Gradle Daemon memory for operations.]' \
354 '-Dorg.gradle.caching=[Set true to enable Gradle build cache.]' \
355 '-Dorg.gradle.console=[Set type of console output to generate.]:console output type:(plain auto rich verbose)' \
356 '-Dorg.gradle.daemon.debug=[Set true to debug Gradle Daemon.]' \
357 '-Dorg.gradle.daemon.idletimeout=[Kill Gradle Daemon after # idle millis.]' \
358 '-Dorg.gradle.debug=[Set true to debug Gradle Client.]' \
359 '-Dorg.gradle.jvmargs=[Set JVM arguments.]' \
360 '-Dorg.gradle.java.home=[Set JDK home dir.]' \
361 '-Dorg.gradle.logging.level=[Set default Gradle log level.]:log level:(quiet warn lifecycle info debug)' \
362 '-Dorg.gradle.parallel=[Set true to enable parallel project builds.]:(true false)' \
363 '-Dorg.gradle.priority=[Set priority for Gradle worker processes.]:priority:(low normal)' \
364 '-Dorg.gradle.warning.mode=[Set types of warnings to log.]:warning level:(all summary none)' \
365 '-Dorg.gradle.workers.max=[Set the number of workers Gradle is allowed to use.]' \
366 '(-i --info -w --warn -q --quiet)'{-d,--debug}'[Log in debug mode (includes normal stacktrace).]' \
367 '(--no-daemon)--daemon[Uses the Gradle daemon to run the build. Starts the daemon if not running.]' \
368 '--foreground[Starts the Gradle daemon in the foreground.]' \
369 {-g,--gradle-user-home}'[Specifies the gradle user home directory.]:home directory:_directories:->argument-expected' \
370 '(-)--gui[Launches the Gradle GUI. (Removed in Gradle 4.0)]' \
371 \*--include-build'[Includes the specified build in the composite.]:file:_directories:->argument-expected' \
372 \*{-I,--init-script}'[Specifies an initialization script.]:init script:_files -g \*.gradle:->argument-expected' \
373 '(-d --debug -w --warn -q --quiet)'{-i,--info}'[Set log level to info.]' \
374 '--max-workers[Set the maximum number of concurrent workers that Gradle may use.]:number workers:->argument-expected' \
375 {-m,--dry-run}'[Runs the builds with all task actions disabled.]' \
376 '--no-color[Do not use color in the console output. (Removed in Gradle 3.0)]' \
377 '(--build-cache)--no-build-cache[Do not use the Gradle build cache.]' \
378 '(--configure-on-demand)--no-configure-on-demand[Disables configuration on demand.]' \
379 '(--daemon)--no-daemon[Do not use the Gradle daemon to run the build.]' \
380 '(--parallel)--no-parallel[Disables parallel execution to build projects.]' \
381 '(--scan)--no-scan[Do not create a build scan.]' \
382 '--offline[The build should operate without accessing network resources.]' \
383 \*{-P+,--project-prop}'[Set project property for the build script (e.g. -Pmyprop=myvalue).]:project property (prop=val):->argument-expected' \
384 {-p,--project-dir}'[Specifies the start directory for Gradle.]:start directory:_directories:->argument-expected' \
385 '(--no-parallel)--parallel[Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use.]' \
386 '--priority=[Set priority for Gradle worker processes.]:priority:(low normal)' \
387 '--profile[Profiles build execution time and generates a report in the <build_dir>/reports/profile directory.]' \
388 '--project-cache-dir=[Specifies the project-specific cache directory.]:cache directory:_directories:->argument-expected' \
389 '(-d --debug -w --warn -i --info)'{-q,--quiet}'[Log errors only.]' \
390 '--recompile-scripts[Force build script recompiling.]' \
391 '--refresh[Refresh the state of resources of the type(s) specified.]:refresh policy:(dependencies)' \
392 '--refresh-dependencies[Refresh the state of dependencies.]' \
393 '--rerun-tasks[Ignore previously cached task results.]' \
394 '(--no-scan)--scan[Create a build scan.]' \
395 '(-S --full-stacktrace)'{-s,--stacktrace}'[Print out the stacktrace for all exceptions.]' \
396 '(-s --stacktrace)'{-S,--full-stacktrace}'[Print out the full (very verbose) stacktrace for all exceptions.]' \
397 '(-)--status[Shows status of running and recently stopped Gradle Daemons.]' \
398 '(-)--stop[Stops all Gradle daemons.]' \
399 '--system-prop[system property (prop=val)]' \
400 {-t,--continuous}'[Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change.]' \
401 {-u,--no-search-upward}"[Don't search in parent folders for a settings.gradle file.]" \
402 '(--write-locks)--update-locks[Perform a partial update of the dependency lock.]' \
403 '(-)'{-v,--version}'[Print version info.]' \
404 '(-d --debug -q --quiet -i --info)'{-w,--warn}'[Log warnings and errors only.]' \
405 '--warning-mode=[Set types of warnings to log.]:warning mode:(all summary none)' \
406 '(--update-locks)--write-locks[Persists dependency resolution for locked configurations.]' \
407 {-x,--exclude-task}'[Specify a task to be excluded from execution.]' \
408 '(-)*:: :->task-or-option' && ret=0
410 if [[ $words[CURRENT] != -* && $state != "argument-expected" ]]; then
411 __gradle_tasks && ret=0
413 curcontext=${curcontext%:*:*}:gradle-$words[1]:
414 __gradle_subcommand && ret=0