gradle.plugin.zsh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ##############################################################################
  2. # A descriptive listing of core Gradle commands
  3. ############################################################################
  4. function _gradle_core_commands() {
  5. local ret=1 state
  6. _arguments ':subcommand:->subcommand' && ret=0
  7. case $state in
  8. subcommand)
  9. subcommands=(
  10. "properties:Display all project properties"
  11. "tasks:Calculate and display all tasks"
  12. "dependencies:Calculate and display all dependencies"
  13. "projects:Discover and display all sub-projects"
  14. "build:Build the project"
  15. "help:Display help"
  16. )
  17. _describe -t subcommands 'gradle subcommands' subcommands && ret=0
  18. esac
  19. return ret
  20. }
  21. function _gradle_arguments() {
  22. _arguments -C \
  23. '-a[Do not rebuild project dependencies]' \
  24. '-b[Specifies the build file]' \
  25. '-c[Specifies the settings file]' \
  26. '-d[Log at the debug level]' \
  27. '-g[Specifies the Gradle user home directory]' \
  28. '-h[Shows a help message]' \
  29. '-i[Set log level to INFO]' \
  30. '-m[Runs the build with all task actions disabled]' \
  31. '-p[Specifies the start directory for Gradle]' \
  32. '-q[Log errors only]' \
  33. '-s[Print out the stacktrace also for user exceptions]' \
  34. '-t[Continuous mode. Automatically re-run build after changes]' \
  35. '-u[Don''t search in parent directories for a settings.gradle file]' \
  36. '-v[Prints Gradle version info]' \
  37. '-x[Specify a task to be excluded]' \
  38. '-D[Set a system property]' \
  39. '-I[Specifies an initialization script]' \
  40. '-P[Sets a project property of the root project]' \
  41. '-S[Print out the full (very verbose) stacktrace]' \
  42. '--build-file[Specifies the build file]' \
  43. '--configure-on-demand[Only relevant projects are configured]' \
  44. '--console[Type of console output to generate (plain, auto, or rich)]' \
  45. '--continue[Continues task execution after a task failure]' \
  46. '--continuous[Continuous mode. Automatically re-run build after changes]' \
  47. '--daemon[Use the Gradle Daemon]' \
  48. '--debug[Log at the debug level]' \
  49. '--dry-run[Runs the build with all task actions disabled]' \
  50. '--exclude-task[Specify a task to be excluded]' \
  51. '--full-stacktrace[Print out the full (very verbose) stacktrace]' \
  52. '--gradle-user-home[Specifies the Gradle user home directory]' \
  53. '--gui[Launches the Gradle GUI app (Deprecated)]' \
  54. '--help[Shows a help message]' \
  55. '--include-build[Run the build as a composite, including the specified build]' \
  56. '--info[Set log level to INFO]' \
  57. '--init-script[Specifies an initialization script]' \
  58. '--max-workers[Set the maximum number of workers that Gradle may use]' \
  59. '--no-daemon[Do not use the Gradle Daemon]' \
  60. '--no-rebuild[Do not rebuild project dependencies]' \
  61. '--no-search-upwards[Don''t search in parent directories for a settings.gradle file]' \
  62. '--offline[Build without accessing network resources]' \
  63. '--parallel[Build projects in parallel]' \
  64. '--profile[Profile build time and create report]' \
  65. '--project-cache-dir[Specifies the project-specific cache directory]' \
  66. '--project-dir[Specifies the start directory for Gradle]' \
  67. '--project-prop[Sets a project property of the root project]' \
  68. '--quiet[Log errors only]' \
  69. '--recompile-scripts[Forces scripts to be recompiled, bypassing caching]' \
  70. '--refresh-dependencies[Refresh the state of dependencies]' \
  71. '--rerun-task[Specifies that any task optimization is ignored]' \
  72. '--settings-file[Specifies the settings file]' \
  73. '--stacktrace[Print out the stacktrace also for user exceptions]' \
  74. '--status[Print Gradle Daemon status]' \
  75. '--stop[Stop all Gradle Daemons]' \
  76. '--system-prop[Set a system property]' \
  77. '--version[Prints Gradle version info]' \
  78. '*::command:->command' \
  79. && return 0
  80. }
  81. ##############################################################################
  82. # Examine the build.gradle file to see if its timestamp has changed;
  83. # and if so, regenerate the .gradle_tasks cache file
  84. ############################################################################
  85. _gradle_does_task_list_need_generating () {
  86. [[ ! -f .gradletasknamecache ]] || [[ build.gradle -nt .gradletasknamecache || build.gradle.kts -nt .gradletasknamecache ]]
  87. }
  88. ##############
  89. # Parse the tasks from `gradle(w) tasks --all` and return them to the calling function.
  90. # All lines in the output from gradle(w) that are between /^-+$/ and /^\s*$/
  91. # are considered to be tasks. If and when gradle adds support for listing tasks
  92. # for programmatic parsing, this method can be deprecated.
  93. ##############
  94. _gradle_parse_tasks () {
  95. lines_might_be_tasks=false
  96. task_name_buffer=""
  97. while read -r line; do
  98. if [[ $line =~ ^-+$ ]]; then
  99. lines_might_be_tasks=true
  100. # Empty buffer, because it contains items that are not tasks
  101. task_name_buffer=""
  102. elif [[ $line =~ ^\s*$ ]]; then
  103. if [[ "$lines_might_be_tasks" = true ]]; then
  104. # If a newline is found, echo the buffer to the calling function
  105. while read -r task; do
  106. echo $task | awk '/[a-zA-Z0-9:-]+/ {print $1}'
  107. done <<< "$task_name_buffer"
  108. # Empty buffer, because we are done with the tasks
  109. task_name_buffer=""
  110. fi
  111. lines_might_be_tasks=false
  112. elif [[ "$lines_might_be_tasks" = true ]]; then
  113. task_name_buffer="${task_name_buffer}\n${line}"
  114. fi
  115. done <<< "$1"
  116. }
  117. ##############
  118. # Gradle tasks from subprojects are allowed to be executed without specifying
  119. # the subproject; that task will then be called on all subprojects.
  120. # gradle(w) tasks --all only lists tasks per subproject, but when autocompleting
  121. # we often want to be able to run a specific task on all subprojects, e.g.
  122. # "gradle clean".
  123. # This function uses the list of tasks from "gradle tasks --all", and for each
  124. # line grabs everything after the last ":" and combines that output with the original
  125. # output. The combined list is returned as the result of this function.
  126. ##############
  127. _gradle_parse_and_extract_tasks () {
  128. # All tasks
  129. tasks=$(_gradle_parse_tasks "$1")
  130. # Task name without sub project(s) prefix
  131. simple_tasks=$(echo $tasks | awk 'BEGIN { FS = ":" } { print $NF }')
  132. echo "$tasks\n$simple_tasks"
  133. }
  134. ##############################################################################
  135. # Discover the gradle tasks by running "gradle tasks --all"
  136. ############################################################################
  137. _gradle_tasks () {
  138. if [[ -f build.gradle || -f build.gradle.kts ]]; then
  139. _gradle_arguments
  140. if _gradle_does_task_list_need_generating; then
  141. _gradle_parse_and_extract_tasks "$(gradle tasks --all)" > .gradletasknamecache
  142. fi
  143. compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache)
  144. fi
  145. }
  146. _gradlew_tasks () {
  147. if [[ -f build.gradle || -f build.gradle.kts ]]; then
  148. _gradle_arguments
  149. if _gradle_does_task_list_need_generating; then
  150. _gradle_parse_and_extract_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
  151. fi
  152. compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache)
  153. fi
  154. }
  155. ##############################################################################
  156. # Register the completions against the gradle and gradlew commands
  157. ############################################################################
  158. compdef _gradle_tasks gradle
  159. compdef _gradlew_tasks gradlew