gradle.plugin.zsh 7.6 KB

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