OILS / devtools / run-task.sh View on Github | oilshell.org

51 lines, 29 significant
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_bash-print-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 shopt -u extdebug
18}
19
20_show-help() {
21 # TODO:
22 # - Use awk to find comments at the top of the file?
23 # - Use OSH to extract docstrings
24
25 echo "Usage: $0 TASK_NAME ARGS..."
26 echo
27 echo "To complete tasks, run:"
28 echo " source devtools/completion.bash"
29 echo
30 echo "Tasks:"
31
32 if command -v column >/dev/null; then
33 _bash-print-funcs | column
34 else
35 _bash-print-funcs
36 fi
37}
38
39run-task() {
40 if [[ $# -eq 0 || $1 =~ ^(--help|-h)$ ]]; then
41 _show-help
42 exit
43 fi
44
45 if ! declare -f "$1" >/dev/null; then
46 echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
47 exit 1
48 fi
49
50 "$@"
51}