blob: bc9988aaceaa1ee45bac46ea9294f6114f2cd91e [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 Petkov5ad2cb52010-08-18 14:01:56 -0700128# Complete cros_workon's <command> argument.
Darin Petkov593e7962010-08-13 14:45:02 -0700129#
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".
Darin Petkov6944c5d2010-08-18 09:57:34 -0700137_complete_cros_workon_command() {
Darin Petkov593e7962010-08-13 14:45:02 -0700138 [ ${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
Darin Petkov6944c5d2010-08-18 09:57:34 -0700144# Prints the full path to the cros_workon executable, handling tilde
145# expansion for the current user.
146_cros_workon_executable() {
147 local cros_workon="${COMP_WORDS[0]}"
148 if [[ "$cros_workon" == '~/'* ]]; then
149 cros_workon="$HOME/${cros_workon#'~/'}"
150 fi
151 echo "$cros_workon"
152}
153
Darin Petkov593e7962010-08-13 14:45:02 -0700154# Lists the workon (or live, if --all is passed in) ebuilds. Lists
155# both the full names (e.g., chromeos-base/metrics) as well as just
156# the ebuild names (e.g., metrics).
157_cros_workon_list() {
Darin Petkov6944c5d2010-08-18 09:57:34 -0700158 local cros_workon=$(_cros_workon_executable)
Darin Petkov593e7962010-08-13 14:45:02 -0700159 ${cros_workon} list $1 | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
160}
161
162# Completes the current cros_workon argument assuming it's a
163# package/ebuild name.
Darin Petkov6944c5d2010-08-18 09:57:34 -0700164_complete_cros_workon_package() {
Darin Petkov593e7962010-08-13 14:45:02 -0700165 [ ${COMP_CWORD} -gt 1 ] || return 1
166 local package="${COMP_WORDS[COMP_CWORD]}"
167 local command="${COMP_WORDS[1]}"
168 # If "start", complete based on all workon packages.
169 if [[ ${command} == "start" ]]; then
170 COMPREPLY=($(compgen -W "$(_cros_workon_list --all)" -- "$package"))
171 return 0
172 fi
173 # If "stop" or "iterate", complete based on all live packages.
174 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
175 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
176 return 0
177 fi
178 return 1
179}
180
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700181# Complete cros_workon arguments.
Darin Petkov593e7962010-08-13 14:45:02 -0700182_cros_workon() {
183 COMPREPLY=()
Darin Petkov593e7962010-08-13 14:45:02 -0700184 _flag_complete && return 0
185 _complete_board_sysroot_flag && return 0
Darin Petkov6944c5d2010-08-18 09:57:34 -0700186 _complete_cros_workon_command && return 0
187 _complete_cros_workon_package && return 0
Darin Petkov593e7962010-08-13 14:45:02 -0700188 return 0
189}
190
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700191_list_repo_commands() {
192 local repo=${COMP_WORDS[0]}
193 "$repo" help --all | grep -E '^ ' | sed 's/ \([^ ]\+\) .\+/\1/'
194}
195
196_list_repo_branches() {
197 local repo=${COMP_WORDS[0]}
198 "$repo" branches 2>&1 | grep \| | sed 's/[ *][Pp ] *\([^ ]\+\) .*/\1/'
199}
200
201_list_repo_projects() {
202 local repo=${COMP_WORDS[0]}
Chris Sosabca106d2010-08-19 16:18:42 -0700203 "$repo" manifest -o /dev/stdout 2> /dev/null \
204 | grep 'project name=' \
205 | sed 's/.\+name="\([^"]\+\)".\+/\1/'
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700206}
207
208# Complete repo's <command> argument.
209_complete_repo_command() {
210 [ ${COMP_CWORD} -eq 1 ] || return 1
211 local command=${COMP_WORDS[1]}
212 COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "$command"))
213 return 0
214}
215
216_complete_repo_arg() {
217 [ ${COMP_CWORD} -gt 1 ] || return 1
218 local command=${COMP_WORDS[1]}
219 local current=${COMP_WORDS[COMP_CWORD]}
220 if [[ ${command} == "abandon" ]]; then
221 if [[ ${COMP_CWORD} -eq 2 ]]; then
222 COMPREPLY=($(compgen -W "$(_list_repo_branches)" -- "$current"))
223 else
224 COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "$current"))
225 fi
226 return 0
227 fi
228 if [[ ${command} == "help" ]]; then
229 [ ${COMP_CWORD} -eq 2 ] && \
230 COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "$current"))
231 return 0
232 fi
233 if [[ ${command} == "start" ]]; then
234 [ ${COMP_CWORD} -gt 2 ] && \
235 COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "$current"))
236 return 0
237 fi
238 return 1
239}
240
241# Complete repo arguments.
242_complete_repo() {
243 COMPREPLY=()
244 _complete_repo_command && return 0
245 _complete_repo_arg && return 0
246 return 0
247}
248
Darin Petkov593e7962010-08-13 14:45:02 -0700249complete -o bashdefault -o default -F _board_sysroot \
250 build_autotest.sh \
251 build_image \
252 build_packages \
253 image_to_usb.sh \
254 mod_image_for_test.sh
255complete -o bashdefault -o default -F _board_overlay setup_board
Eric Li3d13cd12010-03-31 11:47:22 -0700256complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Darin Petkov6944c5d2010-08-18 09:57:34 -0700257complete -F _cros_workon cros_workon
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700258complete -F _complete_repo repo
Darin Petkov049dbab2010-02-23 18:16:37 -0800259
260### Local Variables:
261### mode: shell-script
262### End: