| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Common shell functions for task scripts.
|
| 4 | #
|
| 5 | # Usage:
|
| 6 | # source devtools/run-task.sh
|
| 7 | # ... # define task functions
|
| 8 | # run-task "$@"
|
| 9 |
|
| 10 | # List all functions defined in this file (and not in sourced files).
|
| 11 | _list-funcs() {
|
| 12 | local funcs=($(compgen -A function))
|
| 13 | # extdebug makes `declare -F` print the file path, but, annoyingly, only
|
| 14 | # if you pass the function names as arguments.
|
| 15 | shopt -s extdebug
|
| 16 | declare -F "${funcs[@]}" | grep --fixed-strings " $0" | awk '{print $1}'
|
| 17 | }
|
| 18 |
|
| 19 | run-task() {
|
| 20 | if [[ $# -eq 0 || $1 =~ ^(--help|-h)$ ]]; then
|
| 21 | echo "Usage: $0 TASK_NAME ARGS..."
|
| 22 | echo
|
| 23 | echo "To complete tasks, run:"
|
| 24 | echo " source devtools/completion.bash"
|
| 25 | echo
|
| 26 | echo "Tasks:"
|
| 27 | _list-funcs | column
|
| 28 | exit
|
| 29 | fi
|
| 30 | "$@"
|
| 31 | }
|