blob: 451cabea66967612c96bddf30531a0811ab99ce2 [file] [log] [blame]
Darin Petkov049dbab2010-02-23 18:16:37 -08001# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Add programmable completion to some Chromium OS build scripts
6
7
Darin Petkovd6bf1012010-02-26 13:10:11 -08008# Echo a list of -- flags that the current command accepts. The
9# function assumes that the command supports shflags' --help flag.
Darin Petkov593e7962010-08-13 14:45:02 -070010_flags() {
11 echo $(command "${COMP_WORDS[0]}" --help 2>&1 \
12 | egrep -o -- --\[^\ \]+: \
13 | sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/')
Darin Petkovd6bf1012010-02-26 13:10:11 -080014}
15
16
17# Complete flags, i.e., current words starting with --. Return 1 if
18# the current word doesn't start with --, 0 otherwise.
Darin Petkov593e7962010-08-13 14:45:02 -070019_flag_complete() {
Darin Petkovd6bf1012010-02-26 13:10:11 -080020 COMPREPLY=()
21 local cur="${COMP_WORDS[COMP_CWORD]}"
22 if [[ "${cur}" == --* ]]; then
23 COMPREPLY=( $(compgen -W "$(_flags)" -- ${cur}) )
24 return 0
25 fi
26 return 1
27}
28
29
Darin Petkov049dbab2010-02-23 18:16:37 -080030# Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the
31# word to be completed. If found, echo "--arg=foo".
Darin Petkov593e7962010-08-13 14:45:02 -070032_argeq() {
Darin Petkov049dbab2010-02-23 18:16:37 -080033 local arg=$1
34 local w0="${COMP_WORDS[COMP_CWORD]}"
35 local w1="${COMP_WORDS[COMP_CWORD-1]}"
36
37 # Check for completing "--arg="
38 if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then
39 echo "${w1}${w0}"
40 return 0
41 fi
42
43 # Check for completing "--arg foo"
44 if [ "${w1}" == ${arg} ]; then
45 echo "${w1}=${w0}"
46 return 0
47 fi
48
49 # Check for completing "--arg=foo"
50 if [ ${COMP_CWORD} -gt 2 ]; then
51 local w2="${COMP_WORDS[COMP_CWORD-2]}"
52 if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then
53 echo "${w2}${w1}${w0}"
54 return 0
55 fi
56 fi
57}
58
59
60# echo the existing target board sysroots
Darin Petkov593e7962010-08-13 14:45:02 -070061_board_sysroots() {
Darin Petkov049dbab2010-02-23 18:16:37 -080062 local builddir=/build
Darin Petkovd6bf1012010-02-26 13:10:11 -080063 if [ -d ${builddir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -080064 echo $(command ls "${builddir}")
65 fi
66}
67
Darin Petkov593e7962010-08-13 14:45:02 -070068_complete_board_sysroot_flag() {
Eric Li3d13cd12010-03-31 11:47:22 -070069 COMPREPLY=()
70 local arg=$(_argeq --board)
71 if [[ ${arg} == --board=* ]]; then
72 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) )
73 return 0
74 fi
75 return 1
76}
77
Darin Petkov049dbab2010-02-23 18:16:37 -080078# Completion for --board= argument for existing board sysroots
Darin Petkov593e7962010-08-13 14:45:02 -070079_board_sysroot() {
Darin Petkovd6bf1012010-02-26 13:10:11 -080080 _flag_complete && return 0
Eric Licad2b492010-04-01 13:07:10 -070081 _complete_board_sysroot_flag && return 0
Darin Petkov049dbab2010-02-23 18:16:37 -080082}
83
Darin Petkov049dbab2010-02-23 18:16:37 -080084# echo the existing target board overlays
Darin Petkov593e7962010-08-13 14:45:02 -070085_board_overlays() {
Darin Petkov049dbab2010-02-23 18:16:37 -080086 local overlaydir=../overlays
Darin Petkovd6bf1012010-02-26 13:10:11 -080087 if [ -d ${overlaydir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -080088 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,)
89 fi
90}
91
92# Completion for --board= argument for existing board overlays
Darin Petkov593e7962010-08-13 14:45:02 -070093_board_overlay() {
Darin Petkovd6bf1012010-02-26 13:10:11 -080094 _flag_complete && return 0
95
Darin Petkov049dbab2010-02-23 18:16:37 -080096 COMPREPLY=()
97 local arg=$(_argeq --board)
98 if [[ ${arg} == --board=* ]]; then
99 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) )
Darin Petkov049dbab2010-02-23 18:16:37 -0800100 fi
101}
102
Eric Li3d13cd12010-03-31 11:47:22 -0700103# Completion for -c and -s argument for autotest script
104_ls_autotest() {
105 local autotest_dir=../third_party/autotest/files
Darin Petkov593e7962010-08-13 14:45:02 -0700106 ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \
107 | sed s/"..\/third_party\/autotest\/files\/"//g
Eric Li3d13cd12010-03-31 11:47:22 -0700108}
109
Darin Petkov593e7962010-08-13 14:45:02 -0700110_autotest_complete() {
Eric Li3d13cd12010-03-31 11:47:22 -0700111 _flag_complete && return 0
112
113 local arg=$(_argeq -c)
114 if [[ ${arg} == -c=* ]]; then
115 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})"))
116 return 0
117 fi
118
119 arg=$(_argeq -s)
120 if [[ ${arg} == -s=* ]]; then
121 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})"))
122 return 0
123 fi
124
125 _complete_board_sysroot_flag && return 0
126}
127
Darin Petkov593e7962010-08-13 14:45:02 -0700128# Complete the cros_workon <command> argument.
129#
130# TODO(petkov): We should probably extract the list of commands from
131# cros_workon --help, just like we do for flags (see _flag_complete).
132#
133# TODO(petkov): Currently, this assumes that the command is the first
134# argument. In practice, the command is the first non-flag
135# argument. I.e., this should be fixed to support something like
136# "./cros_workon --all list".
137_cros_workon_command_complete() {
138 [ ${COMP_CWORD} -eq 1 ] || return 1
139 local command="${COMP_WORDS[1]}"
140 COMPREPLY=($(compgen -W "start stop list iterate" -- "$command"))
141 return 0
142}
143
144# Lists the workon (or live, if --all is passed in) ebuilds. Lists
145# both the full names (e.g., chromeos-base/metrics) as well as just
146# the ebuild names (e.g., metrics).
147_cros_workon_list() {
148 local cros_workon="${COMP_WORDS[0]}"
149 ${cros_workon} list $1 | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
150}
151
152# Completes the current cros_workon argument assuming it's a
153# package/ebuild name.
154_cros_workon_package_complete() {
155 [ ${COMP_CWORD} -gt 1 ] || return 1
156 local package="${COMP_WORDS[COMP_CWORD]}"
157 local command="${COMP_WORDS[1]}"
158 # If "start", complete based on all workon packages.
159 if [[ ${command} == "start" ]]; then
160 COMPREPLY=($(compgen -W "$(_cros_workon_list --all)" -- "$package"))
161 return 0
162 fi
163 # If "stop" or "iterate", complete based on all live packages.
164 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
165 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
166 return 0
167 fi
168 return 1
169}
170
171# Complete the cros_workon arguments.
172_cros_workon() {
173 COMPREPLY=()
174 _cros_workon_command_complete && return 0
175 _flag_complete && return 0
176 _complete_board_sysroot_flag && return 0
177 _cros_workon_package_complete && return 0
178 return 0
179}
180
181complete -o bashdefault -o default -F _board_sysroot \
182 build_autotest.sh \
183 build_image \
184 build_packages \
185 image_to_usb.sh \
186 mod_image_for_test.sh
187complete -o bashdefault -o default -F _board_overlay setup_board
Eric Li3d13cd12010-03-31 11:47:22 -0700188complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Darin Petkov593e7962010-08-13 14:45:02 -0700189complete -o bashdefault -o default -F _cros_workon cros_workon
Darin Petkov049dbab2010-02-23 18:16:37 -0800190
191### Local Variables:
192### mode: shell-script
193### End: