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