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 |
12778
|
aaronmk
|
if false; then #### runscript 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 |
12778
|
aaronmk
|
target()
|
14 |
|
|
{
|
15 |
|
|
begin_target
|
16 |
14718
|
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 |
12778
|
aaronmk
|
#...
|
20 |
|
|
}
|
21 |
|
|
|
22 |
11418
|
aaronmk
|
all()
|
23 |
8272
|
aaronmk
|
{
|
24 |
12778
|
aaronmk
|
begin_target
|
25 |
|
|
target # defined above
|
26 |
14718
|
aaronmk
|
#...
|
27 |
8272
|
aaronmk
|
}
|
28 |
11417
|
aaronmk
|
|
29 |
|
|
on_exit # needed if should be runnable as shell-include (with leading ".")
|
30 |
8272
|
aaronmk
|
fi ####
|
31 |
|
|
|
32 |
9013
|
aaronmk
|
. "$(dirname "${BASH_SOURCE[0]}")"/../sh/util.sh
|
33 |
9854
|
aaronmk
|
.rel ../sh/make.sh
|
34 |
8703
|
aaronmk
|
|
35 |
8714
|
aaronmk
|
if self_not_included; then
|
36 |
8703
|
aaronmk
|
|
37 |
10271
|
aaronmk
|
|
38 |
|
|
#### setup
|
39 |
|
|
|
40 |
13311
|
aaronmk
|
# allow caller to override, eg. to force runscript mode when using $wrap_fn
|
41 |
|
|
: "${is_runscript=$(ends_with '[/.]run' "$top_script_abs"; exit2bool)}"
|
42 |
13473
|
aaronmk
|
unexport is_runscript # don't pass to invoked scripts
|
43 |
13311
|
aaronmk
|
|
44 |
11427
|
aaronmk
|
if is_dot_script; then
|
45 |
|
|
if test "$1" = .; then set --; fi # $@ wrong: no args->contains script name
|
46 |
|
|
include_args=("$@") # from `. .../util.run "$@"`
|
47 |
|
|
fi
|
48 |
11414
|
aaronmk
|
|
49 |
12699
|
aaronmk
|
fallback() # overridable handler for targets w/o function
|
50 |
|
|
{ echo_func; "$@"; } # by default, generate error that it doesn't exist
|
51 |
12692
|
aaronmk
|
|
52 |
|
|
gateway() # overridable handler for *all* targets
|
53 |
13465
|
aaronmk
|
{
|
54 |
|
|
echo_func
|
55 |
|
|
test $# -gt 0 || set -- main # default target
|
56 |
|
|
if is_callable "$1"; then "$@"; else fallback "$@"; fi
|
57 |
|
|
}
|
58 |
12692
|
aaronmk
|
|
59 |
13313
|
aaronmk
|
if test ! "$is_runscript"; then # non-runscript
|
60 |
|
|
gateway() { echo_func; main "$@"; } # pass all args to main()
|
61 |
|
|
fi
|
62 |
|
|
|
63 |
10271
|
aaronmk
|
run_args_cmd() # runs the command line args command
|
64 |
|
|
{
|
65 |
11427
|
aaronmk
|
if is_dot_script; then set -- "${include_args[@]}"
|
66 |
|
|
else eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
67 |
|
|
fi
|
68 |
12863
|
aaronmk
|
echo_cmd "$top_script" "$@"
|
69 |
|
|
indent; time gateway "$@"
|
70 |
10271
|
aaronmk
|
}
|
71 |
|
|
|
72 |
12866
|
aaronmk
|
# users can override run_args_cmd()/gateway()/fallback() to perform other
|
73 |
|
|
# commands (or no commands) after the script is read
|
74 |
11428
|
aaronmk
|
on_exit() { test $? -eq 0 || return; run_args_cmd; }
|
75 |
|
|
if ! is_dot_script; then trap on_exit EXIT; fi
|
76 |
8464
|
aaronmk
|
|
77 |
9268
|
aaronmk
|
func_override set_paths__util_sh
|
78 |
|
|
set_paths()
|
79 |
9267
|
aaronmk
|
{
|
80 |
9268
|
aaronmk
|
set_paths__util_sh "$@"
|
81 |
13203
|
aaronmk
|
log_local; log++
|
82 |
9267
|
aaronmk
|
top_file="${top_script%[./]run}"; echo_vars top_file
|
83 |
|
|
top_filename="$(basename "$top_file")"; echo_vars top_filename
|
84 |
|
|
}
|
85 |
9268
|
aaronmk
|
set_paths
|
86 |
8988
|
aaronmk
|
|
87 |
10271
|
aaronmk
|
|
88 |
|
|
#### utils
|
89 |
|
|
|
90 |
13181
|
aaronmk
|
# usage: sudo $wrap_fn fn ...
|
91 |
13310
|
aaronmk
|
wrap_fn="$(esc "$top_script")"
|
92 |
|
|
# if sudo to non-root and $top_script not world-executable, produces error
|
93 |
13312
|
aaronmk
|
# invoked script must always run as runscript so that wrapped command is run
|
94 |
|
|
if test ! "$is_runscript"; then wrap_fn="env is_runscript=1 $wrap_fn"; fi
|
95 |
11348
|
aaronmk
|
|
96 |
13182
|
aaronmk
|
func_override sudo__util_sh
|
97 |
|
|
function sudo() # usage: sudo fn|cmd ... # supports shell functions as well
|
98 |
|
|
{
|
99 |
|
|
echo_func
|
100 |
13268
|
aaronmk
|
if test "$1" = command; then shift # always executable->$wrap_fn not needed
|
101 |
|
|
elif is_intern "$1"; then set -- $wrap_fn "$@" # shell function, etc.
|
102 |
|
|
fi
|
103 |
13182
|
aaronmk
|
sudo__util_sh "$@"
|
104 |
|
|
}
|
105 |
|
|
|
106 |
10766
|
aaronmk
|
# usage: to_top_file cmd...
|
107 |
12982
|
aaronmk
|
function to_top_file()
|
108 |
|
|
{
|
109 |
|
|
echo_func
|
110 |
|
|
stdout="$top_file" if_not_exists="$(bool! "$_remake")" to_file "$@"
|
111 |
|
|
}
|
112 |
11358
|
aaronmk
|
alias to_top_file='"to_top_file" ' # last space alias-expands next word
|
113 |
10766
|
aaronmk
|
|
114 |
10272
|
aaronmk
|
,() # usage: .../run , cmd1 cmd2 ... (like `make cmd1 cmd2 ...`)
|
115 |
|
|
# treats each of the command-line args as commands the way make does
|
116 |
|
|
# (instead of as args to the same command, the way runscripts do)
|
117 |
12968
|
aaronmk
|
{ begin_target; for cmd in "$@"; do time with_rm echo_run "$cmd"; done; }
|
118 |
10272
|
aaronmk
|
|
119 |
12713
|
aaronmk
|
: "${auto_ignore=}" #usage: auto_ignore=1 .../__.run target_that_might_not_exist
|
120 |
12715
|
aaronmk
|
unexport auto_ignore # don't pass this to invoked scripts except through fwd()
|
121 |
12713
|
aaronmk
|
if test "$auto_ignore"; then
|
122 |
|
|
fallback() # don't generate error that it doesn't exist
|
123 |
|
|
{ echo_func; log_info "ignoring non-existant target: $*"; }
|
124 |
|
|
fi
|
125 |
|
|
|
126 |
12711
|
aaronmk
|
# by default, use all subdirs, including .*
|
127 |
|
|
if ! isset subdirs; then subdirs=($(enter_top_dir; wildcard. {.,}*/)); fi
|
128 |
13216
|
aaronmk
|
echo_vars subdirs
|
129 |
12711
|
aaronmk
|
|
130 |
|
|
fwd() # usage: [subdirs=(...);] fwd target args... # or use fwd_self
|
131 |
10271
|
aaronmk
|
{
|
132 |
|
|
echo_func
|
133 |
|
|
: "${subdirs?}"
|
134 |
|
|
|
135 |
12700
|
aaronmk
|
local_export auto_ignore=1
|
136 |
12712
|
aaronmk
|
for subdir in "${subdirs[@]}"; do
|
137 |
|
|
local runscript="$top_dir"/"$subdir"/run
|
138 |
|
|
if test -f "$runscript"; then "$runscript" "$@"; fi
|
139 |
|
|
done
|
140 |
10271
|
aaronmk
|
}
|
141 |
12697
|
aaronmk
|
alias fwd_self='fwd "$FUNCNAME" "$@"' # usage: target() { ...; fwd_self; }
|
142 |
10271
|
aaronmk
|
|
143 |
12695
|
aaronmk
|
: "${auto_fwd=}" # usage: auto_fwd=1; . .../file_including_util.run
|
144 |
12713
|
aaronmk
|
# must be set *after* auto_ignore's fallback() to overwrite it
|
145 |
12695
|
aaronmk
|
if test "$auto_fwd"; then
|
146 |
|
|
fallback() { echo_func; fwd "$@"; }
|
147 |
8704
|
aaronmk
|
fi
|
148 |
12695
|
aaronmk
|
|
149 |
12964
|
aaronmk
|
|
150 |
|
|
#### `make` compatibility
|
151 |
|
|
|
152 |
12967
|
aaronmk
|
main() { begin_target; with_rm all; } # support conventional `all` target
|
153 |
12964
|
aaronmk
|
|
154 |
12695
|
aaronmk
|
fi
|