chromium/tools/bash-completion

# Copyright 2012 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Flag completion rule for bash.
# To load in your shell, "source path/to/this/file".

# Usage examples
# ==============
#
# Browser command line switches:
# $ out/gn/chrome --site-per-pro<tab>
# $ google-chrome --site-per-pro<tab>
#
# Test switches (i.e. --gtest_* and --test-launcher-* switches):
# $ out/gn/unit_tests --gtest_filt<tab>
# $ out/gn/unit_tests --test-launcher-j<tab>
#
# Web test switches:
# $ third_party/blink/tools/run_web_tests.py --additional-driver-f<tab>
# $ .../run_web_tests.py --additional-driver-flag=--site-per-pro<tab>
#
# Blink blink_tool.py sub-commands:
# $ third_party/blink/tools/blink_tool.py reb<tab>

if [ -n "$BASH_SOURCE" ]; then
    # The $BASH_SOURCE variable returns path of current script in bash.
    chrome_source=$(cd $(dirname $BASH_SOURCE)/.. && pwd)
else
    # This is here for other similar shells, e.g. zsh.
    chrome_source=$(cd $(dirname $0)/.. && pwd)
fi

_chrome_flag() {
    local cur targets
    cur="${COMP_WORDS[COMP_CWORD]}"
    targets=$(cd $chrome_source; \
        git ls-files '*switches*' | \
        xargs sed -ne 's/^[^/]*"\([^" /]\{1,\}\)".*/--\1/p')
    COMPREPLY=($(compgen -W "$targets" -- "$cur"))
    return 0
}

_gtest_flag() {
    local cur gtest_flags launcher_flags
    cur="${COMP_WORDS[COMP_CWORD]}"
    gtest_flags=$(sed -ne 's/^.*FromGTestEnv("\([^" /]\+\)".*$/--gtest_\1/p' \
      "$chrome_source/third_party/googletest/src/googletest/src/gtest.cc")
    chrome_test_launcher_flags=$(sed -ne 's/^[^/]*"\([^" /]\{1,\}\)".*/--\1/p' \
      "$chrome_source/base/test/test_switches.cc")
    COMPREPLY=($(
      compgen -W "$gtest_flags $chrome_test_launcher_flags" -- "$cur"))
    return 0
}

_web_test_flag() {
    local cur targets blinkpy_dir prev_switch
    cur="${COMP_WORDS[COMP_CWORD]}"

    # Complete content_shell switches if appropriate.
    if [ "${COMP_CWORD}" -gt 2 -a "${COMP_WORDS[COMP_CWORD-1]}" = "=" ]
    then
      prev_switch="${COMP_WORDS[COMP_CWORD-2]}"
      if [ "$prev_switch" = "--additional-drt-flag" -o \
           "$prev_switch" = "--additional-driver-flag" ]
      then
        targets=$(cd $chrome_source; \
            git ls-files 'content/*switches*.cc' | \
            xargs sed -ne 's/^[^/]*"\([^" /]\{1,\}\)".*/--\1/p')
        COMPREPLY=($(compgen -W "$targets" -- "$cur"))
        return 0
      fi
    fi

    # Complete run_web_tests.py switches.
    blinkpy_dir="$chrome_source/third_party/blink/tools/blinkpy"
    targets=$(sed -ne 's/^[[:space:]]*"\(--[a-z-]\+\)",[[:space:]]*$/\1/p' \
        "$blinkpy_dir/web_tests/run_webkit_tests.py")
    COMPREPLY=($(compgen -W "$targets" -- "$cur"))
    return 0
}

_blink_tool_flag() {
    local cur targets blink_tools_dir
    cur="${COMP_WORDS[COMP_CWORD]}"
    blink_tools_dir=$chrome_source/third_party/blink/tools
    targets=$($blink_tools_dir/blink_tool.py help | grep '^   [a-z]' | \
        awk '{ print $1 }')
    COMPREPLY=($(compgen -W "$targets" -- "$cur"))
    return 0
}

complete -F _chrome_flag google-chrome
complete -F _chrome_flag chrome
complete -F _chrome_flag content_shell
if [ $(uname) = "Darwin" ]
then
  complete -F _chrome_flag Chromium
fi

for gtest_test_executable in $(
  cd $chrome_source;
  git ls-files '*/BUILD.gn' | xargs sed -ne 's/^.*test("\([^"]\+\)").*$/\1/p'
); do
  complete -F _gtest_flag $gtest_test_executable
done

complete -F _web_test_flag run_web_tests.py
complete -F _blink_tool_flag blink_tool.py