|
- #!/usr/bin/env bash
- #
- # AdMake Bash Completion
- # =======================
- #
- # Bash completion support for AdMake
- #
- # Installation
- # ------------
- #
- # 1. Place it in a `bash-completion.d` folder:
- #
- # * /etc/bash-completion.d
- # * /usr/local/etc/bash-completion.d
- # * ~/bash-completion.d
- #
- # 2. echo "set completion-display-width 0" >> ~/.inputrc
- # 3. Open new bash, and type `admake [TAB][TAB]`
- #
- # Documentation
- # -------------
- # The script is called by bash whenever [TAB] or [TAB][TAB] is pressed after
- # 'admake (..)'. By reading entered command line parameters, it determines possible
- # bash completions and writes them to the COMPREPLY variable. Bash then
- # completes the user input if only one entry is listed in the variable or
- # shows the options if more than one is listed in COMPREPLY.
- #
- # The script first determines the current parameter ($cur), the previous
- # parameter ($prev), the first word ($firstword) and the last word ($lastword).
- # Using the $firstword variable (= the command) and a giant switch/case,
- # completions are written to $complete_words and $complete_options.
- #
- # If the current user input ($cur) starts with '-', only $command_options are
- # displayed/completed, otherwise only $command_words.
- #
- # References
- # ----------
- # [1] http://stackoverflow.com/a/12495480/1440785
- # [2] http://tiswww.case.edu/php/chet/bash/FAQ
- #
-
- shopt -s progcomp
- _admake()
- {
- local cur prev firstword lastword complete_words complete_options
-
- # Don't break words at : and =, see [1] and [2]
- COMP_WORDBREAKS=${COMP_WORDBREAKS//[:=]}
-
- cur=${COMP_WORDS[COMP_CWORD]}
- prev=${COMP_WORDS[COMP_CWORD-1]}
- firstword=$(_admake_get_firstword)
- lastword=$(_admake_get_lastword)
-
- SUBCOMMANDS="build -- Incrementally build the project
- rebuild -- Rebuild the project
- clean -- Clean the project
- clean_dist -- Clean artifacts of ths project
- test -- Run the unit test of this project
- run -- Run the executable of this project
- publish -- Publish this module to the library server
- version -- Generate and update the version information
- version_info -- Generate the version information for this project
- install -- Attempt install this project if the target is existent
- package -- Attempt generate package for this project if the target is existent
- "
-
- GLOBAL_OPTIONS="-h -- Show help message
- "
-
- SUBCOMMAND_OPTIONS="-h -- Show this help message and exit
- -d -- Specify the directory contains the CMakeLists.txt
- -p -- Default to ADMAKE_PLATFORM or x64 if it doesn't exist
- -t -- Build the test project with the static library
- -D -- Macro pass to CMake, e.g: -DENABLE_LOG -DTEST_BUILD=1
- -r -- Build release edition
- -m -- Macro pass to source files, e.g: -m ROS_BACKEND -m TEST_BUILD=1
- -o -- Specify the OS for cross-compiling
- -w -- Specify VS version on Windows
- -l -- Specify the location you will publish your module(Only vaild for \`publish\`)
- -u -- Update the dependencies before build
- --lcm -- Specify the backend of message BUS to LCM
- --zcm -- Specify the backend of message BUS to ZCM
- --ros -- Specify the backend of message BUS to ROS
- "
-
- SUBCOMMANDS_SHORT="-h -d -p -t -D -r -u"
-
- LINUX_PLATFORM_OPTIONS="x86 -- For Windows or Linux x86 target
- x64 -- For Windows or Linux x64 target
- arm -- For arm eabi target, Linux Host Only
- armhf -- For armhf target, Linux Host Only
- arm-none -- For arm embedded device target, Windows or Linux Host
- qnx -- For QNX6.6 edition
- qnx7 -- For QNX7 edition
- "
-
- WIN_PLATFORM_OPTIONS="x86 -- For Windows or Linux x86 target
- x64 -- For Windows or Linux x64 target
- arm-none -- For arm embedded device target, Windows or Linux Host
- qnx -- For QNX6.6 edition
- qnx7 -- For QNX7 edition
- "
-
- # Un-comment this for debug purposes:
- # echo -e "\n1. prev = $prev, cur = $cur, firstword = $firstword, lastword = $lastword\n"
-
- case "${firstword}" in
- build | rebuild | clean | publish)
- complete_options="$SUBCOMMAND_OPTIONS"
- complete_words="$SUBCOMMAND_OPTIONS"
-
- case "${prev}" in
- -p)
- case "$OSTYPE" in
- linux*)
- complete_words="$LINUX_PLATFORM_OPTIONS"
- ;;
- msys)
- # Make sure this OS is Windows
- if [ "$OS" = "Windows_NT" ]; then
- complete_words="$WIN_PLATFORM_OPTIONS"
- fi
- ;;
- darwin*)
- # Unsupported now
- ;;
- *)
- ;;
- esac
- ;;
- esac
- ;;
-
- *)
- complete_words="$SUBCOMMANDS"
- complete_options="$GLOBAL_OPTIONS"
- ;;
- esac
-
- # Either display words or options, depending on the user input
- local OLDIFS="$IFS"
- local IFS=$'\n'
- if [[ $cur == -* ]] && [[ ${#cur} -gt 1 ]] && [[ ${SUBCOMMANDS_SHORT/${cur}//} != $SUBCOMMANDS_SHORT ]]; then
- COMPREPLY=( $cur )
- else
- COMPREPLY=( $(compgen -W "$complete_words" -- $cur) )
- fi
- IFS="$OLDIFS"
-
- # Only one completion
- if [[ ${#COMPREPLY[*]} -eq 1 ]]; then
- # Remove ' - ' and everything after
- COMPREPLY=( ${COMPREPLY[0]%% -- *} )
- fi
-
- return 0
- }
-
- ## Helper functions ###
-
- # Determines the first non-option word of the command line. This
- # is usually the command
- _admake_get_firstword() {
- local firstword i
-
- firstword=
- for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
- if [[ ${COMP_WORDS[i]} != -* ]]; then
- firstword=${COMP_WORDS[i]}
- break
- fi
- done
-
- echo $firstword
- }
-
- # Determines the last non-option word of the command line. This
- # is usally a sub-command
- _admake_get_lastword() {
- local lastword i
-
- lastword=
- for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
- if [[ ${COMP_WORDS[i]} != -* ]] && [[ -n ${COMP_WORDS[i]} ]] && [[ ${COMP_WORDS[i]} != $cur ]]; then
- lastword=${COMP_WORDS[i]}
- fi
- done
-
- echo $lastword
- }
-
- ## Define bash completions ###
-
- complete -F _admake admake
|