gradle.plugin.zsh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. '-h[Help]' \
  25. '-D[System property]' \
  26. '-d[Log at the debug level]' \
  27. '--gui[Launches the Gradle GUI app]' \
  28. '--stop[Stop the Gradle daemon]' \
  29. '--daemon[Use the Gradle daemon]' \
  30. '--no-daemon[Do not use the Gradle daemon]' \
  31. '--rerun-task [Specifies that any task optimization is ignored.]' \
  32. '-i[Log at the info level]' \
  33. '-m[Dry run]' \
  34. '-P[Set a project property]' \
  35. '-p[Specifies the start directory]' \
  36. '--profile[Profile the build time]' \
  37. '-q[Log at the quiet level (only show errors)]' \
  38. '-v[Print the Gradle version info]' \
  39. '-x[Specify a task to be excluded]' \
  40. '-b[Specifies the build file.]' \
  41. '-c[Specifies the settings file.]' \
  42. '--continue[Continues task execution after a task failure.]' \
  43. '-g[Specifies the Gradle user home directory.]' \
  44. '-I[Specifies an initialization script.]' \
  45. '--refresh-dependencies[Refresh the state of dependencies.]' \
  46. '-u[Don''t search in parent directories for a settings.gradle file.]' \
  47. '*::command:->command' \
  48. && return 0
  49. }
  50. ##############################################################################
  51. # Examine the build.gradle file to see if its timestamp has changed;
  52. # and if so, regenerate the .gradle_tasks cache file
  53. ############################################################################
  54. _gradle_does_task_list_need_generating () {
  55. [[ ! -f .gradletasknamecache ]] || [[ build.gradle -nt .gradletasknamecache ]]
  56. }
  57. ##############
  58. # Parse the tasks from `gradle(w) tasks --all` and return them to the calling function.
  59. # All lines in the output from gradle(w) that are between /^-+$/ and /^\s*$/
  60. # are considered to be tasks. If and when gradle adds support for listing tasks
  61. # for programmatic parsing, this method can be deprecated.
  62. ##############
  63. _gradle_parse_tasks () {
  64. lines_might_be_tasks=false
  65. task_name_buffer=""
  66. while read -r line; do
  67. if [[ $line =~ ^-+$ ]]; then
  68. lines_might_be_tasks=true
  69. # Empty buffer, because it contains items that are not tasks
  70. task_name_buffer=""
  71. elif [[ $line =~ ^\s*$ ]]; then
  72. if [[ "$lines_might_be_tasks" = true ]]; then
  73. # If a newline is found, echo the buffer to the calling function
  74. while read -r task; do
  75. echo $task | awk '/[a-zA-Z0-9:-]+/ {print $1}'
  76. done <<< "$task_name_buffer"
  77. # Empty buffer, because we are done with the tasks
  78. task_name_buffer=""
  79. fi
  80. lines_might_be_tasks=false
  81. elif [[ "$lines_might_be_tasks" = true ]]; then
  82. task_name_buffer="${task_name_buffer}\n${line}"
  83. fi
  84. done <<< "$1"
  85. }
  86. ##############
  87. # Gradle tasks from subprojects are allowed to be executed without specifying
  88. # the subproject; that task will then be called on all subprojects.
  89. # gradle(w) tasks --all only lists tasks per subproject, but when autocompleting
  90. # we often want to be able to run a specific task on all subprojects, e.g.
  91. # "gradle clean".
  92. # This function uses the list of tasks from "gradle tasks --all", and for each
  93. # line grabs everything after the last ":" and combines that output with the original
  94. # output. The combined list is returned as the result of this function.
  95. ##############
  96. _gradle_parse_and_extract_tasks () {
  97. # All tasks
  98. tasks=$(_gradle_parse_tasks "$1")
  99. # Task name without sub project(s) prefix
  100. simple_tasks=$(echo $tasks | awk 'BEGIN { FS = ":" } { print $NF }')
  101. echo "$tasks\n$simple_tasks"
  102. }
  103. ##############################################################################
  104. # Discover the gradle tasks by running "gradle tasks --all"
  105. ############################################################################
  106. _gradle_tasks () {
  107. if [[ -f build.gradle ]]; then
  108. _gradle_arguments
  109. if _gradle_does_task_list_need_generating; then
  110. _gradle_parse_and_extract_tasks "$(gradle tasks --all)" > .gradletasknamecache
  111. fi
  112. compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache)
  113. fi
  114. }
  115. _gradlew_tasks () {
  116. if [[ -f build.gradle ]]; then
  117. _gradle_arguments
  118. if _gradle_does_task_list_need_generating; then
  119. _gradle_parse_and_extract_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
  120. fi
  121. compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache)
  122. fi
  123. }
  124. ##############################################################################
  125. # Register the completions against the gradle and gradlew commands
  126. ############################################################################
  127. compdef _gradle_tasks gradle
  128. compdef _gradlew_tasks gradlew