gradle.plugin.zsh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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` into .gradletasknamecache
  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, send the buffer to .gradletasknamecache
  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. # Discover the gradle tasks by running "gradle tasks --all"
  88. ############################################################################
  89. _gradle_tasks () {
  90. if [[ -f build.gradle ]]; then
  91. _gradle_arguments
  92. if _gradle_does_task_list_need_generating; then
  93. _gradle_parse_tasks "$(gradle tasks --all)" > .gradletasknamecache
  94. fi
  95. compadd -X "==== Gradle Tasks ====" $(cat .gradletasknamecache)
  96. fi
  97. }
  98. _gradlew_tasks () {
  99. if [[ -f build.gradle ]]; then
  100. _gradle_arguments
  101. if _gradle_does_task_list_need_generating; then
  102. _gradle_parse_tasks "$(./gradlew tasks --all)" > .gradletasknamecache
  103. fi
  104. compadd -X "==== Gradlew Tasks ====" $(cat .gradletasknamecache)
  105. fi
  106. }
  107. ##############################################################################
  108. # Register the completions against the gradle and gradlew commands
  109. ############################################################################
  110. compdef _gradle_tasks gradle
  111. compdef _gradlew_tasks gradlew