blob: b4c2431dc20feb34050c5f7b61f6d2858babdb27 [file] [log] [blame]
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +00001# -*- sh -*- (Bash only)
2#
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17#
18# Bash completion of Bazel commands.
19#
20# The template is expanded at build time using tables of commands/options
21# derived from the bazel executable built in the same client; the expansion is
22# written to bazel-complete.bash.
23#
24# Provides command-completion for:
25# - bazel prefix options (e.g. --host_jvm_args)
26# - blaze command-set (e.g. build, test)
27# - blaze command-specific options (e.g. --copts)
28# - values for enum-valued options
29# - package-names, exploring all package-path roots.
30# - targets within packages.
31
32# The package path used by the completion routines. Unfortunately
33# this isn't necessarily the same as the actual package path used by
34# Bazel, but that's ok. (It's impossible for us to reliably know what
35# the relevant package-path, so this is just a good guess. Users can
36# override it if they want.)
37#
38# Don't use it directly. Generate the final script with
39# bazel build //scripts:bash_completion instead.
40#
41: ${BAZEL_COMPLETION_PACKAGE_PATH:=%workspace%}
42
43
44# If true, Bazel query is used for autocompletion. This is more
45# accurate than the heuristic grep, especially for strangely-formatted
46# BUILD files. But it can be slower, especially if the Bazel server
47# is busy, and more brittle, if the BUILD file contains serious
48# errors. This is an experimental feature.
49: ${BAZEL_COMPLETION_USE_QUERY:=false}
50
51
52# If true, Bazel run allows autocompletion for test targets. This is convenient
53# for users who run a lot of tests/benchmarks locally with blaze run.
54: ${BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN:=false}
55
56# Some commands might interfer with the important one, so don't complete them
57: ${BAZEL_IGNORED_COMMAND_REGEX:="__none__"}
58
59# Bazel command
60: ${BAZEL:=bazel}
61
62# Pattern to match for looking for a target
63# BAZEL_BUILD_MATCH_PATTERN__* give the pattern for label-*
64# when looking in the the build file.
65# BAZEL_QUERY_MATCH_PATTERN__* give the pattern for label-*
66# when using 'bazel query'.
67# _RUNTEST are special case when BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN
68# is on.
69: ${BAZEL_BUILD_MATCH_PATTERN__test:='(.*_test|test_suite)'}
70: ${BAZEL_QUERY_MATCH_PATTERN__test:='(test|test_suite)'}
71: ${BAZEL_BUILD_MATCH_PATTERN__bin:='.*_binary'}
72: ${BAZEL_QUERY_MATCH_PATTERN__bin:='(binary)'}
73: ${BAZEL_BUILD_MATCH_PATTERN_RUNTEST__bin:='(.*_(binary|test)|test_suite)'}
74: ${BAZEL_QUERY_MATCH_PATTERN_RUNTEST__bin:='(binary|test)'}
75: ${BAZEL_BUILD_MATCH_PATTERN__:='.*'}
76: ${BAZEL_QUERY_MATCH_PATTERN__:=''}
77
78# Usage: _bazel__get_rule_match_pattern <command>
79# Determine what kind of rules to match, based on command.
80_bazel__get_rule_match_pattern() {
81 local var_name pattern
82 if _bazel__is_true "$BAZEL_COMPLETION_USE_QUERY"; then
83 var_name="BAZEL_QUERY_MATCH_PATTERN"
84 else
85 var_name="BAZEL_BUILD_MATCH_PATTERN"
86 fi
87 if [[ "$1" =~ ^label-?([a-z]*)$ ]]; then
88 pattern=${BASH_REMATCH[1]:-}
89 if _bazel__is_true "$BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN"; then
90 eval "echo \"\${${var_name}_RUNTEST__${pattern}:-\$${var_name}__${pattern}}\""
91 else
92 eval "echo \"\$${var_name}__${pattern}\""
93 fi
94 fi
95}
96
97# Compute workspace directory. Search for the innermost
98# enclosing directory with a WORKSPACE file.
99_bazel__get_workspace_path() {
100 local workspace=$PWD
101 while true; do
102 if [ -f "${workspace}/WORKSPACE" ]; then
103 break
104 elif [ -z "$workspace" -o "$workspace" = "/" ]; then
105 workspace=$PWD
106 break;
107 fi
108 workspace=${workspace%/*}
109 done
110 echo $workspace
111}
112
113
114# Find the current piece of the line to complete, but only do word breaks at
115# certain characters. In particular, ignore these: "':=
116# This method also takes into account the current cursor position.
117#
118# Works with both bash 3 and 4! Bash 3 and 4 perform different word breaks when
119# computing the COMP_WORDS array. We need this here because Bazel options are of
120# the form --a=b, and labels of the form //some/label:target.
121_bazel__get_cword() {
122 local cur=${COMP_LINE:0:$COMP_POINT}
123 # This expression finds the last word break character, as defined in the
124 # COMP_WORDBREAKS variable, but without '=' or ':', which is not preceeded by
125 # a slash. Quote characters are also excluded.
126 local wordbreaks="$COMP_WORDBREAKS"
127 wordbreaks="${wordbreaks//\'/}"
128 wordbreaks="${wordbreaks//\"/}"
129 wordbreaks="${wordbreaks//:/}"
130 wordbreaks="${wordbreaks//=/}"
131 local word_start=$(expr "$cur" : '.*[^\]['"${wordbreaks}"']')
132 echo "${cur:$word_start}"
133}
134
135
136# Usage: _bazel__package_path <workspace> <displacement>
137#
138# Prints a list of package-path root directories, displaced using the
139# current displacement from the workspace. All elements have a
140# trailing slash.
141_bazel__package_path() {
142 local workspace=$1 displacement=$2 root
143 IFS=:
144 for root in ${BAZEL_COMPLETION_PACKAGE_PATH//\%workspace\%/$workspace}; do
145 unset IFS
146 echo "$root/$displacement"
147 done
148}
149
150# Usage: _bazel__options_for <command>
151#
152# Prints the set of options for a given Bazel command, e.g. "build".
153_bazel__options_for() {
154 local options
155 if [[ "${BAZEL_COMMAND_LIST}" =~ ^(.* )?$1( .*)?$ ]]; then
156 local option_name=$(echo $1 | perl -ne 'print uc' | tr "-" "_")
157 eval "echo \${BAZEL_COMMAND_${option_name}_FLAGS}" | tr " " "\n"
158 fi
159}
160# Usage: _bazel__expansion_for <command>
161#
162# Prints the completion pattern for a given Bazel command, e.g. "build".
163_bazel__expansion_for() {
164 local options
165 if [[ "${BAZEL_COMMAND_LIST}" =~ ^(.* )?$1( .*)?$ ]]; then
166 local option_name=$(echo $1 | perl -ne 'print uc' | tr "-" "_")
167 eval "echo \${BAZEL_COMMAND_${option_name}_ARGUMENT}"
168 fi
169}
170
171# Usage: _bazel__matching_targets <kind> <prefix>
172#
173# Prints target names of kind <kind> and starting with <prefix> in the BUILD
174# file given as standard input. <kind> is a basic regex (BRE) used to match the
175# bazel rule kind and <prefix> is the prefix of the target name.
176_bazel__matching_targets() {
177 local kind_pattern="$1"
178 local target_prefix="$2"
179 # The following commands do respectively:
180 # Remove BUILD file comments
181 # Replace \n by spaces to have the BUILD file in a single line
182 # Extract all rule types and target names
183 # Grep the kind pattern and the target prefix
184 # Returns the target name
185 sed 's/#.*$//' \
186 | tr "\n" " " \
187 | sed 's/\([a-zA-Z0-9_]*\) *(\([^)]* \)\{0,1\}name *= *['\''"]\([a-zA-Z0-9_/.+=,@~-]*\)['\''"][^)]*)/\
188type:\1 name:\3\
189/g' \
190 | grep -E "^type:$kind_pattern name:$target_prefix" \
191 | cut -d ':' -f 3
192}
193
194
195# Usage: _bazel__is_true <string>
196#
197# Returns true or false based on the input string. The following are
198# valid true values (the rest are false): "1", "true".
199_bazel__is_true() {
200 local str="$1"
201 [[ "$str" == "1" || "$str" == "true" ]]
202}
203
204# Usage: _bazel__expand_rules_in_package <workspace> <displacement>
205# <current> <label-type>
206#
207# Expands rules in specified packages, exploring all roots of
208# $BAZEL_COMPLETION_PACKAGE_PATH, not just $(pwd). Only rules
209# appropriate to the command are printed. Sets $COMPREPLY array to
210# result.
211#
212# If $BAZEL_COMPLETION_USE_QUERY is true, 'bazel query' is used
213# instead, with the actual Bazel package path;
214# $BAZEL_COMPLETION_PACKAGE_PATH is ignored in this case, since the
215# actual Bazel value is likely to be more accurate.
216_bazel__expand_rules_in_package() {
217 local workspace=$1 displacement=$2 current=$3 label_type=$4
218 local package_name=$(echo "$current" | cut -f1 -d:)
219 local rule_prefix=$(echo "$current" | cut -f2 -d:)
220 local root buildfile rule_pattern r result
221
222 result=
223 pattern=$(_bazel__get_rule_match_pattern "$label_type")
224 if _bazel__is_true "$BAZEL_COMPLETION_USE_QUERY"; then
225 package_name=$(echo "$package_name" | tr -d "'\"") # remove quotes
226 result=$(${BAZEL} --output_base=/tmp/${BAZEL}-completion-$USER query \
227 --keep_going --noshow_progress \
228 "kind('$pattern rule', '$package_name:*')" 2>/dev/null |
229 cut -f2 -d: | grep "^$rule_prefix")
230 else
231 for root in $(_bazel__package_path "$workspace" "$displacement"); do
232 buildfile="$root/$package_name/BUILD"
233 if [ -f "$buildfile" ]; then
234 result=$(_bazel__matching_targets \
235 "$pattern" "$rule_prefix" <"$buildfile")
236 break
237 fi
238 done
239 fi
240
241 index=$(echo $result | wc -w)
242 if [ -n "$result" ]; then
243 echo "$result" | tr " " "\n" | sed 's|$| |'
244 fi
245 # Include ":all" wildcard if there was no unique match. (The zero
246 # case is tricky: we need to include "all" in that case since
247 # otherwise we won't expand "a" to "all" in the absence of rules
248 # starting with "a".)
249 if [ $index -ne 1 ] && expr all : "\\($rule_prefix\\)" >/dev/null; then
250 echo "all "
251 fi
252}
253
254# Usage: _bazel__expand_package_name <workspace> <displacement> <current-word>
255# <label-type>
256#
257# Expands directories, but explores all roots of
258# BAZEL_COMPLETION_PACKAGE_PATH, not just $(pwd). When a directory is
259# a bazel package, the completion offers "pkg:" so you can expand
260# inside the package.
261# Sets $COMPREPLY array to result.
262_bazel__expand_package_name() {
263 local workspace=$1 displacement=$2 current=$3 type=${4:-} root dir index
264 for root in $(_bazel__package_path "$workspace" "$displacement"); do
265 found=0
266 for dir in $(compgen -d $root$current); do
267 [ -L "$dir" ] && continue # skip symlinks (e.g. bazel-bin)
268 [[ "$dir" =~ ^(.*/)?\.[^/]*$ ]] && continue # skip dotted dir (e.g. .git)
269 found=1
270 echo "${dir#$root}/"
271 if [ -f $dir/BUILD ]; then
272 if [ "${type}" = "label-package" ]; then
273 echo "${dir#$root} "
274 else
275 echo "${dir#$root}:"
276 fi
277 fi
278 done
279 [ $found -gt 0 ] && break # Stop searching package path upon first match.
280 done
281}
282
283# Usage: _bazel__expand_target_pattern <workspace> <displacement>
284# <word> <label-syntax>
285#
286# Expands "word" to match target patterns, using the current workspace
287# and displacement from it. "command" is used to filter rules.
288# Sets $COMPREPLY array to result.
289_bazel__expand_target_pattern() {
290 local workspace=$1 displacement=$2 current=$3 label_syntax=$4
291 case "$current" in
292 //*:*) # Expand rule names within package, no displacement.
293 if [ "${label_syntax}" = "label-package" ]; then
294 compgen -S " " -W "BUILD" "$(echo current | cut -f ':' -d2)"
295 else
296 _bazel__expand_rules_in_package "$workspace" "" "$current" "$label_syntax"
297 fi
298 ;;
299 *:*) # Expand rule names within package, displaced.
300 if [ "${label_syntax}" = "label-package" ]; then
301 compgen -S " " -W "BUILD" "$(echo current | cut -f ':' -d2)"
302 else
303 _bazel__expand_rules_in_package \
304 "$workspace" "$displacement" "$current" "$label_syntax"
305 fi
306 ;;
307 //*) # Expand filenames using package-path, no displacement
308 _bazel__expand_package_name "$workspace" "" "$current" "$label_syntax"
309 ;;
310 *) # Expand filenames using package-path, displaced.
311 if [ -n "$current" ]; then
312 _bazel__expand_package_name "$workspace" "$displacement" "$current" "$label_syntax"
313 fi
314 ;;
315 esac
316}
317
318_bazel__get_command() {
319 for word in "${COMP_WORDS[@]:1:COMP_CWORD-1}"; do
320 if echo "$BAZEL_COMMAND_LIST" | grep -wsq -e "$word"; then
321 echo $word
322 break
323 fi
324 done
325}
326
327# Returns the displacement to the workspace given in $1
328_bazel__get_displacement() {
329 if [[ "$PWD" =~ ^$1/.*$ ]]; then
330 echo ${PWD##$1/}/
331 fi
332}
333
334
335# Usage: _bazel__complete_pattern <workspace> <displacement> <current>
336# <type>
337#
338# Expand a word according to a type. The currently supported types are:
339# - {a,b,c}: an enum that can take value a, b or c
340# - label: a label of any kind
341# - label-bin: a label to a runnable rule (basically to a _binary rule)
342# - label-test: a label to a test rule
343# - info-key: an info key as listed by `bazel help info-keys`
344# - command: the name of a command
345# - path: a file path
346# - combinaison of previous type using | as separator
347_bazel__complete_pattern() {
348 local workspace=$1 displacement=$2 current=$3 types=$4
349 for type in $(echo $types | tr "|" "\n"); do
350 case "$type" in
351 label*)
352 _bazel__expand_target_pattern "$workspace" "$displacement" \
353 "$current" "$type"
354 ;;
355 info-key)
356 compgen -S " " -W "${BAZEL_INFO_KEYS}" -- "$current"
357 ;;
358 "command")
359 local commands=$(echo "${BAZEL_COMMAND_LIST}" \
360 | tr " " "\n" | grep -v "^${BAZEL_IGNORED_COMMAND_REGEX}$")
361 compgen -S " " -W "${commands}" -- "$current"
362 ;;
363 path)
364 compgen -f -- "$current"
365 ;;
366 *)
367 compgen -S " " -W "$type" -- "$current"
368 ;;
369 esac
370 done
371}
372
373# Usage: _bazel__expand_options <workspace> <displacement> <current-word>
374# <options>
375#
376# Expands options, making sure that if current-word contains an equals sign,
377# it is handled appropriately.
378_bazel__expand_options() {
379 local workspace="$1" displacement="$2" cur="$3" options="$4"
380 if [[ $cur =~ = ]]; then
381 # also expands special labels
382 current=$(echo "$cur" | cut -f2 -d=)
383 _bazel__complete_pattern "$workspace" "$displacement" "$current" \
384 "$(compgen -W "$options" -- "$cur" | cut -f2 -d=)" \
385 | sort -u
386 else
387 compgen -W "$(echo "$options" | sed 's|=.*$|=|')" -- "$cur" \
388 | sed 's|\([^=]\)$|\1 |'
389 fi
390}
391
392
393_bazel__complete_stdout() {
394 local cur=$(_bazel__get_cword) word command displacement workspace
395
396 # Determine command: "" (startup-options) or one of $BAZEL_COMMAND_LIST.
397 command="$(_bazel__get_command)"
398
399 workspace="$(_bazel__get_workspace_path)"
400 displacement="$(_bazel__get_displacement ${workspace})"
401
402 case "$command" in
403 "") # Expand startup-options or commands
404 local commands=$(echo "${BAZEL_COMMAND_LIST}" \
405 | tr " " "\n" | grep -v "^${BAZEL_IGNORED_COMMAND_REGEX}$")
406 _bazel__expand_options "$workspace" "$displacement" "$cur" \
407 "${commands}\
408 ${BAZEL_STARTUP_OPTIONS}"
409 ;;
410
411 *)
412 case "$cur" in
413 -*) # Expand options:
414 _bazel__expand_options "$workspace" "$displacement" "$cur" \
415 "$(_bazel__options_for $command)"
416 ;;
417 *) # Expand target pattern
418 expansion_pattern="$(_bazel__expansion_for $command)"
419 NON_QUOTE_REGEX="^[\"']"
420 if [[ $command = query && $cur =~ $NON_QUOTE_REGEX ]]; then
421 : # Ideally we would expand query expressions---it's not
422 # that hard, conceptually---but readline is just too
423 # damn complex when it comes to quotation. Instead,
424 # for query, we just expand target patterns, unless
425 # the first char is a quote.
426 elif [ -n "$expansion_pattern" ]; then
427 _bazel__complete_pattern \
428 "$workspace" "$displacement" "$cur" "$expansion_pattern"
429 fi
430 ;;
431 esac
432 ;;
433 esac
434}
435
436_bazel__to_compreply() {
437 local replies="$1"
438 COMPREPLY=()
439 # Trick to preserve whitespaces
440 while IFS="" read -r reply; do
441 COMPREPLY+=("${reply}")
442 done < <(echo "${replies}")
443}
444
445_bazel__complete() {
446 _bazel__to_compreply "$(_bazel__complete_stdout)"
447}
448
449# Some users have aliases such as bt="bazel test" or bb="bazel build", this
450# completion function allows them to have auto-completion for these aliases.
451_bazel__complete_target_stdout() {
452 local cur=$(_bazel__get_cword) word command displacement workspace
453
454 # Determine command: "" (startup-options) or one of $BAZEL_COMMAND_LIST.
455 command="$1"
456
457 workspace="$(_bazel__get_workspace_path)"
458 displacement="$(_bazel__get_displacement ${workspace})"
459
460 _bazel__to_compreply "$(_bazel__expand_target_pattern "$workspace" "$displacement" \
461 "$cur" "$command")"
462}
463
464# default completion for bazel
465complete -F _bazel__complete -o nospace "${BAZEL}"
466
467########################################################################
468## DO NOT EDIT BELOW THIS LINE.
469## This section is automatically generated by update-completion.sh.