Chris Sosa | c1bd111 | 2011-08-26 15:22:10 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 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 Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 8 | # Echo a list of -- flags that the current command accepts. The |
| 9 | # function assumes that the command supports shflags' --help flag. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 10 | _flags() { |
| 11 | echo $(command "${COMP_WORDS[0]}" --help 2>&1 \ |
| 12 | | egrep -o -- --\[^\ \]+: \ |
| 13 | | sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/') |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 14 | } |
| 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 19 | _flag_complete() { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 20 | 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 Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 30 | # 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 32 | _argeq() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 33 | 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 61 | _board_sysroots() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 62 | local builddir=/build |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 63 | if [ -d ${builddir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 64 | echo $(command ls "${builddir}") |
| 65 | fi |
| 66 | } |
| 67 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 68 | _complete_board_sysroot_flag() { |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 69 | 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 Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 78 | # Completion for --board= argument for existing board sysroots |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 79 | _board_sysroot() { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 80 | _flag_complete && return 0 |
Eric Li | cad2b49 | 2010-04-01 13:07:10 -0700 | [diff] [blame] | 81 | _complete_board_sysroot_flag && return 0 |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 84 | # echo the existing target board overlays |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 85 | _board_overlays() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 86 | local overlaydir=../overlays |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 87 | if [ -d ${overlaydir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 88 | echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,) |
| 89 | fi |
| 90 | } |
| 91 | |
| 92 | # Completion for --board= argument for existing board overlays |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 93 | _board_overlay() { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 94 | _flag_complete && return 0 |
| 95 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 96 | COMPREPLY=() |
| 97 | local arg=$(_argeq --board) |
| 98 | if [[ ${arg} == --board=* ]]; then |
| 99 | COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) ) |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 100 | fi |
| 101 | } |
| 102 | |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 103 | # Completion for -c and -s argument for autotest script |
| 104 | _ls_autotest() { |
| 105 | local autotest_dir=../third_party/autotest/files |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 106 | ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \ |
| 107 | | sed s/"..\/third_party\/autotest\/files\/"//g |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 110 | _autotest_complete() { |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 111 | _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 Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 128 | # Complete cros_workon's <command> argument. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 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 |
Mandeep Singh Baines | 93da99b | 2011-02-05 11:42:19 -0800 | [diff] [blame] | 136 | # "cros_workon --all list". |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 137 | _complete_cros_workon_command() { |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 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 | |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 144 | # 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 154 | # 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 Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 158 | local cros_workon=$(_cros_workon_executable) |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 159 | ${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 Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 164 | _complete_cros_workon_package() { |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 165 | [ ${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 Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 181 | # Complete cros_workon arguments. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 182 | _cros_workon() { |
| 183 | COMPREPLY=() |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 184 | _flag_complete && return 0 |
| 185 | _complete_board_sysroot_flag && return 0 |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 186 | _complete_cros_workon_command && return 0 |
| 187 | _complete_cros_workon_package && return 0 |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 188 | return 0 |
| 189 | } |
| 190 | |
Darin Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 191 | _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 Sosa | bca106d | 2010-08-19 16:18:42 -0700 | [diff] [blame] | 203 | "$repo" manifest -o /dev/stdout 2> /dev/null \ |
| 204 | | grep 'project name=' \ |
| 205 | | sed 's/.\+name="\([^"]\+\)".\+/\1/' |
Darin Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 206 | } |
| 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 249 | complete -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 |
| 255 | complete -o bashdefault -o default -F _board_overlay setup_board |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 256 | complete -o bashdefault -o default -o nospace -F _autotest_complete autotest |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 257 | complete -F _cros_workon cros_workon |
Darin Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 258 | complete -F _complete_repo repo |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 259 | |
| 260 | ### Local Variables: |
| 261 | ### mode: shell-script |
| 262 | ### End: |