1 |
8291
|
aaronmk
|
#!/bin/bash -e
|
2 |
8715
|
aaronmk
|
# runscripts: a bash-based replacement for make
|
3 |
8272
|
aaronmk
|
# unlike make, supports full bash functionality including multiline commands
|
4 |
10834
|
aaronmk
|
# usage: .../run function args
|
5 |
10835
|
aaronmk
|
# this usage also applies to all files that include this file
|
6 |
8272
|
aaronmk
|
|
7 |
|
|
if false; then #### run script template:
|
8 |
8291
|
aaronmk
|
#!/bin/bash -e
|
9 |
12695
|
aaronmk
|
auto_fwd=1 # optional
|
10 |
11414
|
aaronmk
|
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/util_or_file_including_util.run "$@"
|
11 |
9854
|
aaronmk
|
.rel other_includes
|
12 |
8272
|
aaronmk
|
|
13 |
11418
|
aaronmk
|
all()
|
14 |
8272
|
aaronmk
|
{
|
15 |
8881
|
aaronmk
|
echo_func
|
16 |
8272
|
aaronmk
|
"$(dirname "${BASH_SOURCE[0]}")"/path_relative_to_self
|
17 |
|
|
"$(dirname "${BASH_SOURCE[1]}")"/path_relative_to_caller
|
18 |
|
|
"$top_dir"/path_relative_to_outermost_script
|
19 |
|
|
}
|
20 |
11417
|
aaronmk
|
|
21 |
|
|
on_exit # needed if should be runnable as shell-include (with leading ".")
|
22 |
8272
|
aaronmk
|
fi ####
|
23 |
|
|
|
24 |
9013
|
aaronmk
|
. "$(dirname "${BASH_SOURCE[0]}")"/../sh/util.sh
|
25 |
9854
|
aaronmk
|
.rel ../sh/make.sh
|
26 |
8703
|
aaronmk
|
|
27 |
8714
|
aaronmk
|
if self_not_included; then
|
28 |
8703
|
aaronmk
|
|
29 |
10271
|
aaronmk
|
|
30 |
|
|
#### setup
|
31 |
|
|
|
32 |
11427
|
aaronmk
|
if is_dot_script; then
|
33 |
|
|
if test "$1" = .; then set --; fi # $@ wrong: no args->contains script name
|
34 |
|
|
include_args=("$@") # from `. .../util.run "$@"`
|
35 |
|
|
fi
|
36 |
11414
|
aaronmk
|
|
37 |
12699
|
aaronmk
|
fallback() # overridable handler for targets w/o function
|
38 |
|
|
{ echo_func; "$@"; } # by default, generate error that it doesn't exist
|
39 |
12692
|
aaronmk
|
|
40 |
|
|
gateway() # overridable handler for *all* targets
|
41 |
12694
|
aaronmk
|
{ echo_func; if is_callable "$1"; then "$@"; else fallback "$@"; fi; }
|
42 |
12692
|
aaronmk
|
|
43 |
10271
|
aaronmk
|
run_args_cmd() # runs the command line args command
|
44 |
|
|
{
|
45 |
11427
|
aaronmk
|
if is_dot_script; then set -- "${include_args[@]}"
|
46 |
|
|
else eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
47 |
|
|
fi
|
48 |
10271
|
aaronmk
|
test $# -ge 1 || set -- all
|
49 |
12692
|
aaronmk
|
echo_cmd "$top_script" "$@"; time gateway "$@"
|
50 |
10271
|
aaronmk
|
}
|
51 |
|
|
|
52 |
8972
|
aaronmk
|
# users can override run_args_cmd to perform other commands (or no commands)
|
53 |
8464
|
aaronmk
|
# after the script is read
|
54 |
11428
|
aaronmk
|
on_exit() { test $? -eq 0 || return; run_args_cmd; }
|
55 |
|
|
if ! is_dot_script; then trap on_exit EXIT; fi
|
56 |
8464
|
aaronmk
|
|
57 |
9268
|
aaronmk
|
func_override set_paths__util_sh
|
58 |
|
|
set_paths()
|
59 |
9267
|
aaronmk
|
{
|
60 |
9268
|
aaronmk
|
set_paths__util_sh "$@"
|
61 |
9267
|
aaronmk
|
top_file="${top_script%[./]run}"; echo_vars top_file
|
62 |
|
|
top_filename="$(basename "$top_file")"; echo_vars top_filename
|
63 |
|
|
}
|
64 |
9268
|
aaronmk
|
set_paths
|
65 |
8988
|
aaronmk
|
|
66 |
10271
|
aaronmk
|
|
67 |
|
|
#### utils
|
68 |
|
|
|
69 |
11348
|
aaronmk
|
wrap_fn="$top_script" # usage: sudo "$wrap_fn" fn ...
|
70 |
|
|
|
71 |
10766
|
aaronmk
|
# usage: to_top_file cmd...
|
72 |
11359
|
aaronmk
|
function to_top_file() { echo_func; stdout="$top_file" to_file "$@"; }
|
73 |
11358
|
aaronmk
|
alias to_top_file='"to_top_file" ' # last space alias-expands next word
|
74 |
10766
|
aaronmk
|
|
75 |
10272
|
aaronmk
|
,() # usage: .../run , cmd1 cmd2 ... (like `make cmd1 cmd2 ...`)
|
76 |
|
|
# treats each of the command-line args as commands the way make does
|
77 |
|
|
# (instead of as args to the same command, the way runscripts do)
|
78 |
|
|
{ echo_func; for cmd in "$@"; do time echo_run "$cmd"; done; }
|
79 |
|
|
|
80 |
12713
|
aaronmk
|
: "${auto_ignore=}" #usage: auto_ignore=1 .../__.run target_that_might_not_exist
|
81 |
12715
|
aaronmk
|
unexport auto_ignore # don't pass this to invoked scripts except through fwd()
|
82 |
12713
|
aaronmk
|
if test "$auto_ignore"; then
|
83 |
|
|
fallback() # don't generate error that it doesn't exist
|
84 |
|
|
{ echo_func; log_info "ignoring non-existant target: $*"; }
|
85 |
|
|
fi
|
86 |
|
|
|
87 |
12711
|
aaronmk
|
# by default, use all subdirs, including .*
|
88 |
|
|
if ! isset subdirs; then subdirs=($(enter_top_dir; wildcard. {.,}*/)); fi
|
89 |
|
|
clog-- echo_vars subdirs
|
90 |
|
|
|
91 |
|
|
fwd() # usage: [subdirs=(...);] fwd target args... # or use fwd_self
|
92 |
10271
|
aaronmk
|
{
|
93 |
|
|
echo_func
|
94 |
|
|
: "${subdirs?}"
|
95 |
|
|
|
96 |
12700
|
aaronmk
|
local_export auto_ignore=1
|
97 |
12712
|
aaronmk
|
for subdir in "${subdirs[@]}"; do
|
98 |
|
|
local runscript="$top_dir"/"$subdir"/run
|
99 |
|
|
if test -f "$runscript"; then "$runscript" "$@"; fi
|
100 |
|
|
done
|
101 |
10271
|
aaronmk
|
}
|
102 |
12697
|
aaronmk
|
alias fwd_self='fwd "$FUNCNAME" "$@"' # usage: target() { ...; fwd_self; }
|
103 |
10271
|
aaronmk
|
|
104 |
12695
|
aaronmk
|
: "${auto_fwd=}" # usage: auto_fwd=1; . .../file_including_util.run
|
105 |
12713
|
aaronmk
|
# must be set *after* auto_ignore's fallback() to overwrite it
|
106 |
12695
|
aaronmk
|
if test "$auto_fwd"; then
|
107 |
|
|
fallback() { echo_func; fwd "$@"; }
|
108 |
8704
|
aaronmk
|
fi
|
109 |
12695
|
aaronmk
|
|
110 |
|
|
fi
|