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 #### runscript template:
|
8
|
#!/bin/bash -e
|
9
|
auto_fwd=1 # optional
|
10
|
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/util_or_file_including_util.run "$@"
|
11
|
.rel other_includes
|
12
|
|
13
|
target()
|
14
|
{
|
15
|
begin_target
|
16
|
#...
|
17
|
}
|
18
|
|
19
|
all()
|
20
|
{
|
21
|
begin_target
|
22
|
target # defined above
|
23
|
"$(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
|
|
28
|
on_exit # needed if should be runnable as shell-include (with leading ".")
|
29
|
fi ####
|
30
|
|
31
|
. "$(dirname "${BASH_SOURCE[0]}")"/../sh/util.sh
|
32
|
.rel ../sh/make.sh
|
33
|
|
34
|
if self_not_included; then
|
35
|
|
36
|
|
37
|
#### setup
|
38
|
|
39
|
if is_dot_script; then
|
40
|
if test "$1" = .; then set --; fi # $@ wrong: no args->contains script name
|
41
|
include_args=("$@") # from `. .../util.run "$@"`
|
42
|
fi
|
43
|
|
44
|
fallback() # overridable handler for targets w/o function
|
45
|
{ echo_func; "$@"; } # by default, generate error that it doesn't exist
|
46
|
|
47
|
gateway() # overridable handler for *all* targets
|
48
|
{ echo_func; if is_callable "$1"; then "$@"; else fallback "$@"; fi; }
|
49
|
|
50
|
run_args_cmd() # runs the command line args command
|
51
|
{
|
52
|
if is_dot_script; then set -- "${include_args[@]}"
|
53
|
else eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
54
|
fi
|
55
|
test $# -ge 1 || set -- main
|
56
|
echo_cmd "$top_script" "$@"
|
57
|
indent; time gateway "$@"
|
58
|
}
|
59
|
|
60
|
# users can override run_args_cmd()/gateway()/fallback() to perform other
|
61
|
# commands (or no commands) after the script is read
|
62
|
on_exit() { test $? -eq 0 || return; run_args_cmd; }
|
63
|
if ! is_dot_script; then trap on_exit EXIT; fi
|
64
|
|
65
|
func_override set_paths__util_sh
|
66
|
set_paths()
|
67
|
{
|
68
|
set_paths__util_sh "$@"
|
69
|
log_local; log++
|
70
|
top_file="${top_script%[./]run}"; echo_vars top_file
|
71
|
top_filename="$(basename "$top_file")"; echo_vars top_filename
|
72
|
}
|
73
|
set_paths
|
74
|
|
75
|
|
76
|
#### utils
|
77
|
|
78
|
# usage: sudo $wrap_fn fn ...
|
79
|
wrap_fn=
|
80
|
if log++ is_world_executable "$top_script"; then
|
81
|
wrap_fn="$(esc "$top_script")"
|
82
|
fi
|
83
|
|
84
|
func_override sudo__util_sh
|
85
|
function sudo() # usage: sudo fn|cmd ... # supports shell functions as well
|
86
|
{
|
87
|
echo_func
|
88
|
if test "$1" = command; then shift # always executable->$wrap_fn not needed
|
89
|
elif is_intern "$1"; then set -- $wrap_fn "$@" # shell function, etc.
|
90
|
fi
|
91
|
sudo__util_sh "$@"
|
92
|
}
|
93
|
|
94
|
# usage: to_top_file cmd...
|
95
|
function to_top_file()
|
96
|
{
|
97
|
echo_func
|
98
|
stdout="$top_file" if_not_exists="$(bool! "$_remake")" to_file "$@"
|
99
|
}
|
100
|
alias to_top_file='"to_top_file" ' # last space alias-expands next word
|
101
|
|
102
|
,() # usage: .../run , cmd1 cmd2 ... (like `make cmd1 cmd2 ...`)
|
103
|
# treats each of the command-line args as commands the way make does
|
104
|
# (instead of as args to the same command, the way runscripts do)
|
105
|
{ begin_target; for cmd in "$@"; do time with_rm echo_run "$cmd"; done; }
|
106
|
|
107
|
: "${auto_ignore=}" #usage: auto_ignore=1 .../__.run target_that_might_not_exist
|
108
|
unexport auto_ignore # don't pass this to invoked scripts except through fwd()
|
109
|
if test "$auto_ignore"; then
|
110
|
fallback() # don't generate error that it doesn't exist
|
111
|
{ echo_func; log_info "ignoring non-existant target: $*"; }
|
112
|
fi
|
113
|
|
114
|
# by default, use all subdirs, including .*
|
115
|
if ! isset subdirs; then subdirs=($(enter_top_dir; wildcard. {.,}*/)); fi
|
116
|
echo_vars subdirs
|
117
|
|
118
|
fwd() # usage: [subdirs=(...);] fwd target args... # or use fwd_self
|
119
|
{
|
120
|
echo_func
|
121
|
: "${subdirs?}"
|
122
|
|
123
|
local_export auto_ignore=1
|
124
|
for subdir in "${subdirs[@]}"; do
|
125
|
local runscript="$top_dir"/"$subdir"/run
|
126
|
if test -f "$runscript"; then "$runscript" "$@"; fi
|
127
|
done
|
128
|
}
|
129
|
alias fwd_self='fwd "$FUNCNAME" "$@"' # usage: target() { ...; fwd_self; }
|
130
|
|
131
|
: "${auto_fwd=}" # usage: auto_fwd=1; . .../file_including_util.run
|
132
|
# must be set *after* auto_ignore's fallback() to overwrite it
|
133
|
if test "$auto_fwd"; then
|
134
|
fallback() { echo_func; fwd "$@"; }
|
135
|
fi
|
136
|
|
137
|
|
138
|
#### `make` compatibility
|
139
|
|
140
|
main() { begin_target; with_rm all; } # support conventional `all` target
|
141
|
|
142
|
fi
|