1
|
#!/bin/bash -e
|
2
|
# runscripts: a bash-based replacement for make
|
3
|
# unlike make, supports full bash functionality including multiline commands
|
4
|
# usage: .../run function args
|
5
|
# this usage also applies to all files that include this file
|
6
|
|
7
|
if false; then #### run script template:
|
8
|
#!/bin/bash -e
|
9
|
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/util_or_file_including_util.run "$@"
|
10
|
.rel other_includes
|
11
|
|
12
|
all()
|
13
|
{
|
14
|
echo_func
|
15
|
"$(dirname "${BASH_SOURCE[0]}")"/path_relative_to_self
|
16
|
"$(dirname "${BASH_SOURCE[1]}")"/path_relative_to_caller
|
17
|
"$top_dir"/path_relative_to_outermost_script
|
18
|
}
|
19
|
|
20
|
on_exit # needed if should be runnable as shell-include (with leading ".")
|
21
|
fi ####
|
22
|
|
23
|
. "$(dirname "${BASH_SOURCE[0]}")"/../sh/util.sh
|
24
|
.rel ../sh/make.sh
|
25
|
|
26
|
if self_not_included; then
|
27
|
|
28
|
|
29
|
#### setup
|
30
|
|
31
|
if is_dot_script; then
|
32
|
if test "$1" = .; then set --; fi # $@ wrong: no args->contains script name
|
33
|
include_args=("$@") # from `. .../util.run "$@"`
|
34
|
fi
|
35
|
|
36
|
run_args_cmd() # runs the command line args command
|
37
|
{
|
38
|
if is_dot_script; then set -- "${include_args[@]}"
|
39
|
else eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
40
|
fi
|
41
|
test $# -ge 1 || set -- all
|
42
|
echo_cmd "$top_script" "$@"; time "$@"
|
43
|
}
|
44
|
|
45
|
# users can override run_args_cmd to perform other commands (or no commands)
|
46
|
# after the script is read
|
47
|
on_exit() { test $? -eq 0 || return; trap - EXIT; run_args_cmd; }
|
48
|
trap on_exit EXIT
|
49
|
|
50
|
func_override set_paths__util_sh
|
51
|
set_paths()
|
52
|
{
|
53
|
set_paths__util_sh "$@"
|
54
|
top_file="${top_script%[./]run}"; echo_vars top_file
|
55
|
top_filename="$(basename "$top_file")"; echo_vars top_filename
|
56
|
}
|
57
|
set_paths
|
58
|
|
59
|
|
60
|
#### utils
|
61
|
|
62
|
wrap_fn="$top_script" # usage: sudo "$wrap_fn" fn ...
|
63
|
|
64
|
# usage: to_top_file cmd...
|
65
|
function to_top_file() { echo_func; stdout="$top_file" to_file "$@"; }
|
66
|
alias to_top_file='"to_top_file" ' # last space alias-expands next word
|
67
|
|
68
|
,() # usage: .../run , cmd1 cmd2 ... (like `make cmd1 cmd2 ...`)
|
69
|
# treats each of the command-line args as commands the way make does
|
70
|
# (instead of as args to the same command, the way runscripts do)
|
71
|
{ echo_func; for cmd in "$@"; do time echo_run "$cmd"; done; }
|
72
|
|
73
|
fwd() # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
|
74
|
{
|
75
|
echo_func
|
76
|
: "${subdirs?}"
|
77
|
|
78
|
for subdir in "${subdirs[@]}"; do "$top_dir"/"$subdir"/run "$@"; done
|
79
|
}
|
80
|
|
81
|
fi
|