_bazel 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #compdef bazel bazelisk
  2. # Copyright 2015 The Bazel Authors. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Installation
  16. # ------------
  17. #
  18. # 1. Add this script to a directory on your $fpath:
  19. # fpath[1,0]=~/.zsh/completion/
  20. # mkdir -p ~/.zsh/completion/
  21. # cp scripts/zsh_completion/_bazel ~/.zsh/completion
  22. #
  23. # 2. Optionally, add the following to your .zshrc.
  24. # zstyle ':completion:*' use-cache on
  25. # zstyle ':completion:*' cache-path ~/.zsh/cache
  26. #
  27. # This way, the completion script does not have to parse Bazel's options
  28. # repeatedly. The directory in cache-path must be created manually.
  29. #
  30. # 3. Restart the shell
  31. #
  32. # Options
  33. # -------
  34. # completion:init:bazel:* cache-lifetime
  35. # Lifetime for the completion cache (if turned on, default: 1 week)
  36. local curcontext="$curcontext" state line
  37. : ${BAZEL_COMPLETION_PACKAGE_PATH:=%workspace%}
  38. : ${BAZEL:=bazel}
  39. _bazel_b() { ${BAZEL} --noblock_for_lock "$@" 2>/dev/null; }
  40. # Default cache lifetime is 1 week
  41. zstyle -s ":completion:${curcontext}:" cache-lifetime lifetime
  42. if [[ -z "${lifetime}" ]]; then
  43. lifetime=$((60*60*24*7))
  44. fi
  45. _bazel_cache_policy() {
  46. local -a oldp
  47. oldp=( "$1"(Nms+${lifetime}) )
  48. (( $#oldp ))
  49. }
  50. _set_cache_policy() {
  51. zstyle -s ":completion:*:$curcontext*" cache-policy update_policy
  52. if [[ -z "$update_policy" ]]; then
  53. zstyle ":completion:$curcontext*" cache-policy _bazel_cache_policy
  54. fi
  55. }
  56. # Skips over all global arguments. After invocation, OFFSET contains the
  57. # position of the bazel command in $words.
  58. _adapt_subcommand_offset() {
  59. OFFSET=2
  60. for w in ${words[2,-1]}; do
  61. if [[ $w == (#b)-* ]]; then
  62. (( OFFSET++ ))
  63. else
  64. return
  65. fi
  66. done
  67. }
  68. # Retrieve the cache but also check that the value is not empty.
  69. _bazel_safe_retrieve_cache() {
  70. _retrieve_cache $1 && [[ ${(P)#2} -gt 0 ]]
  71. }
  72. # Puts the name of the variable that contains the options for the bazel
  73. # subcommand handed in as the first argument into the global variable
  74. # _bazel_cmd_options.
  75. _bazel_get_options() {
  76. local lcmd=$1
  77. _bazel_cmd_options=_bazel_${lcmd}_options
  78. _bazel_cmd_args=_bazel_${lcmd}_args
  79. if [[ ${(P)#_bazel_cmd_options} != 0 ]]; then
  80. return
  81. fi
  82. if _cache_invalid BAZEL_${lcmd}_options || _cache_invalid BAZEL_${lcmd}_args \
  83. || ! _bazel_safe_retrieve_cache BAZEL_${lcmd}_options ${_bazel_cmd_options} \
  84. || ! _retrieve_cache BAZEL_${lcmd}_args ${_bazel_cmd_args}; then
  85. if ! eval "$(_bazel_b help completion)"; then
  86. return
  87. fi
  88. local opts_var
  89. if [[ $lcmd == "startup_options" ]]; then
  90. opts_var="BAZEL_STARTUP_OPTIONS"
  91. else
  92. opts_var="BAZEL_COMMAND_${lcmd:u}_FLAGS"
  93. fi
  94. local -a raw_options
  95. if ! eval "raw_options=(\${(@f)$opts_var})"; then
  96. return
  97. fi
  98. local -a option_list
  99. for opt in $raw_options; do
  100. case $opt in
  101. --*"={"*)
  102. local lst="${${opt##*"={"}%"}"}"
  103. local opt="${opt%%=*}="
  104. option_list+=("${opt}:string:_values '' ${lst//,/ }") ;;
  105. --*=path)
  106. option_list+=("${opt%path}:path:_files") ;;
  107. --*=label)
  108. option_list+=("${opt%label}:target:_bazel_complete_target") ;;
  109. --*=*)
  110. option_list+=("${opt}:string:") ;;
  111. *)
  112. option_list+=("$opt") ;;
  113. esac
  114. done
  115. local -a cmd_args
  116. local cmd_type
  117. if eval "cmd_type=\${BAZEL_COMMAND_${lcmd:u}_ARGUMENT}" && [[ -n $cmd_type ]]; then
  118. case $cmd_type in
  119. label|label-*)
  120. cmd_args+=("*::${cmd_type}:_bazel_complete_target_${cmd_type//-/_}") ;;
  121. info-key)
  122. cmd_args+=('1::key:_bazel_info_key') ;;
  123. path)
  124. cmd_args+=('1::profile:_path_files') ;;
  125. "command|{"*"}")
  126. local lst=${${cmd_type#"command|{"}%"}"}
  127. cmd_args+=("1::topic:_bazel_help_topic -- ${lst//,/ }") ;;
  128. esac
  129. fi
  130. typeset -g "${_bazel_cmd_options}"="${(pj:|:)option_list[*]}"
  131. _store_cache BAZEL_${lcmd}_options ${_bazel_cmd_options}
  132. typeset -g "${_bazel_cmd_args}"="${(pj:|:)cmd_args[*]}"
  133. _store_cache BAZEL_${lcmd}_args ${_bazel_cmd_args}
  134. fi
  135. }
  136. _get_build_targets() {
  137. local pkg=$1
  138. local rule_re
  139. typeset -a completions
  140. case $target_type in
  141. test)
  142. rule_re=".*_test"
  143. ;;
  144. build)
  145. rule_re=".*"
  146. ;;
  147. bin)
  148. rule_re=".*_test|.*_binary"
  149. ;;
  150. esac
  151. completions=(${$(_bazel_b query "kind(\"${rule_re}\", ${pkg}:all)" 2>/dev/null)##*:})
  152. if ( (( ${#completions} > 0 )) && [[ $target_type != bin ]] ); then
  153. completions+=(all)
  154. fi
  155. echo ${completions[*]}
  156. }
  157. # Returns all packages that match $PREFIX. PREFIX may start with //, in which
  158. # case the workspace roots are searched. Otherwise, they are completed based on
  159. # PWD.
  160. _get_build_packages() {
  161. local workspace pfx
  162. typeset -a package_roots paths final_paths
  163. workspace=$PWD
  164. package_roots=(${(ps.:.)BAZEL_COMPLETION_PACKAGE_PATH})
  165. package_roots=(${^package_roots//\%workspace\%/$workspace})
  166. if [[ "${(e)PREFIX}" == //* ]]; then
  167. pfx=${(e)PREFIX[2,-1]}
  168. else
  169. pfx=${(e)PREFIX}
  170. fi
  171. paths=(${^package_roots}/${pfx}*(/))
  172. for p in ${paths[*]}; do
  173. if [[ -f ${p}/BUILD || -f ${p}/BUILD.bazel ]]; then
  174. final_paths+=(${p##*/}:)
  175. fi
  176. final_paths+=(${p##*/}/)
  177. done
  178. echo ${final_paths[*]}
  179. }
  180. _package_remove_slash() {
  181. if [[ $KEYS == ':' && $LBUFFER == */ ]]; then
  182. LBUFFER=${LBUFFER[1,-2]}
  183. fi
  184. }
  185. # Completion function for BUILD targets, called by the completion system.
  186. _bazel_complete_target() {
  187. local expl
  188. typeset -a packages targets
  189. if [[ "${(e)PREFIX}" != *:* ]]; then
  190. # There is no : in the prefix, completion can be either
  191. # a package or a target, if the cwd is a package itself.
  192. if [[ -f $PWD/BUILD || -f $PWD/BUILD.bazel ]]; then
  193. targets=($(_get_build_targets ""))
  194. _description build_target expl "BUILD target"
  195. compadd "${expl[@]}" -a targets
  196. fi
  197. packages=($(_get_build_packages))
  198. _description build_package expl "BUILD package"
  199. # Chop of the leading path segments from the prefix for display.
  200. compset -P '*/'
  201. compadd -R _package_remove_slash -S '' "${expl[@]}" -a packages
  202. else
  203. targets=($(_get_build_targets "${${(e)PREFIX}%:*}"))
  204. _description build_target expl "BUILD target"
  205. # Ignore the current prefix for the upcoming completion, since we only list
  206. # the names of the targets, not the full path.
  207. compset -P '*:'
  208. compadd "${expl[@]}" -a targets
  209. fi
  210. }
  211. _bazel_complete_target_label() {
  212. typeset -g target_type=build
  213. _bazel_complete_target
  214. }
  215. _bazel_complete_target_label_test() {
  216. typeset -g target_type=test
  217. _bazel_complete_target
  218. }
  219. _bazel_complete_target_label_bin() {
  220. typeset -g target_type=bin
  221. _bazel_complete_target
  222. }
  223. ### Actual completion commands
  224. _bazel() {
  225. _adapt_subcommand_offset
  226. if (( CURRENT - OFFSET > 0 )); then
  227. # Remember the subcommand name, stored globally so we can access it
  228. # from any subsequent function
  229. cmd=${words[OFFSET]//-/_}
  230. # Set the context for the subcommand.
  231. curcontext="${curcontext%:*:*}:bazel-$cmd:"
  232. _set_cache_policy
  233. # Narrow the range of words we are looking at to exclude cmd
  234. # name and any leading options
  235. (( CURRENT = CURRENT - OFFSET + 1 ))
  236. shift $((OFFSET - 1)) words
  237. # Run the completion for the subcommand
  238. _bazel_get_options $cmd
  239. _arguments : \
  240. ${(Pps:|:)_bazel_cmd_options} \
  241. ${(Pps:|:)_bazel_cmd_args}
  242. else
  243. _set_cache_policy
  244. # Start special handling for global options,
  245. # which can be retrieved by calling
  246. # $ bazel help startup_options
  247. _bazel_get_options startup_options
  248. _arguments : \
  249. ${(Pps:|:)_bazel_cmd_options} \
  250. "*:commands:_bazel_commands"
  251. fi
  252. return
  253. }
  254. _get_commands() {
  255. # bazel_cmd_list is a global (g) array (a)
  256. typeset -ga _bazel_cmd_list
  257. # Use `bazel help` instead of `bazel help completion` to get command
  258. # descriptions.
  259. if _bazel_cmd_list=("${(@f)$(_bazel_b help | awk '
  260. /Available commands/ { command=1; }
  261. / [-a-z]+[ \t]+.+/ { if (command) { printf "%s:", $1; for (i=2; i<=NF; i++) printf "%s ", $i; print "" } }
  262. /^$/ { command=0; }')}"); then
  263. _store_cache BAZEL_commands _bazel_cmd_list
  264. fi
  265. }
  266. # Completion function for bazel subcommands, called by the completion system.
  267. _bazel_commands() {
  268. if [[ ${#_bazel_cmd_list} == 0 ]]; then
  269. if _cache_invalid BAZEL_commands \
  270. || ! _bazel_safe_retrieve_cache BAZEL_commands _bazel_cmd_list; then
  271. _get_commands
  272. fi
  273. fi
  274. _describe -t bazel-commands 'Bazel command' _bazel_cmd_list
  275. }
  276. # Completion function for bazel help options, called by the completion system.
  277. _bazel_help_topic() {
  278. if [[ ${#_bazel_cmd_list} == 0 ]]; then
  279. if _cache_invalid BAZEL_commands \
  280. || ! _bazel_safe_retrieve_cache BAZEL_commands _bazel_cmd_list; then
  281. _get_commands
  282. fi
  283. fi
  284. while [[ $# -gt 0 ]]; do
  285. if [[ $1 == -- ]]; then
  286. shift
  287. break
  288. fi
  289. shift
  290. done
  291. _bazel_help_list=($@)
  292. _bazel_help_list+=($_bazel_cmd_list)
  293. _describe -t bazel-help 'Help topic' _bazel_help_list
  294. }
  295. # Completion function for bazel info keys, called by the completion system.
  296. _bazel_info_key() {
  297. if [[ ${#_bazel_info_keys_list} == 0 ]]; then
  298. if _cache_invalid BAZEL_info_keys \
  299. || ! _bazel_safe_retrieve_cache BAZEL_info_keys _bazel_info_keys_list; then
  300. typeset -ga _bazel_info_keys_list
  301. # Use `bazel help` instead of `bazel help completion` to get info-key
  302. # descriptions.
  303. if _bazel_info_keys_list=("${(@f)$(_bazel_b help info-keys | awk '
  304. { printf "%s:", $1; for (i=2; i<=NF; i++) printf "%s ", $i; print "" }')}"); then
  305. _store_cache BAZEL_info_keys _bazel_info_keys_list
  306. fi
  307. fi
  308. fi
  309. _describe -t bazel-info 'Key' _bazel_info_keys_list
  310. }