Project

General

Profile

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 11427 aaronmk
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 11414 aaronmk
44 12699 aaronmk
fallback() # overridable handler for targets w/o function
45
{ echo_func; "$@"; } # by default, generate error that it doesn't exist
46 12692 aaronmk
47
gateway() # overridable handler for *all* targets
48 12694 aaronmk
{ echo_func; if is_callable "$1"; then "$@"; else fallback "$@"; fi; }
49 12692 aaronmk
50 10271 aaronmk
run_args_cmd() # runs the command line args command
51
{
52 11427 aaronmk
	if is_dot_script; then set -- "${include_args[@]}"
53
	else              eval set -- "$(reverse "${BASH_ARGV[@]}")"
54
	fi
55 12964 aaronmk
	test $# -ge 1 || set -- main
56 12863 aaronmk
	echo_cmd "$top_script" "$@"
57
	indent; time gateway "$@"
58 10271 aaronmk
}
59
60 12866 aaronmk
# users can override run_args_cmd()/gateway()/fallback() to perform other
61
# commands (or no commands) after the script is read
62 11428 aaronmk
on_exit() { test $? -eq 0 || return; run_args_cmd; }
63
if ! is_dot_script; then trap on_exit EXIT; fi
64 8464 aaronmk
65 9268 aaronmk
func_override set_paths__util_sh
66
set_paths()
67 9267 aaronmk
{
68 9268 aaronmk
	set_paths__util_sh "$@"
69 9267 aaronmk
	top_file="${top_script%[./]run}"; echo_vars top_file
70
	top_filename="$(basename "$top_file")"; echo_vars top_filename
71
}
72 9268 aaronmk
set_paths
73 8988 aaronmk
74 10271 aaronmk
75
#### utils
76
77 11348 aaronmk
wrap_fn="$top_script" # usage: sudo "$wrap_fn" fn ...
78
79 10766 aaronmk
# usage: to_top_file cmd...
80 12982 aaronmk
function to_top_file()
81
{
82
	echo_func
83
	stdout="$top_file" if_not_exists="$(bool! "$_remake")" to_file "$@"
84
}
85 11358 aaronmk
alias to_top_file='"to_top_file" ' # last space alias-expands next word
86 10766 aaronmk
87 10272 aaronmk
,() # usage: .../run , cmd1 cmd2 ... (like `make cmd1 cmd2 ...`)
88
# treats each of the command-line args as commands the way make does
89
# (instead of as args to the same command, the way runscripts do)
90 12968 aaronmk
{ begin_target; for cmd in "$@"; do time with_rm echo_run "$cmd"; done; }
91 10272 aaronmk
92 12713 aaronmk
: "${auto_ignore=}" #usage: auto_ignore=1 .../__.run target_that_might_not_exist
93 12715 aaronmk
unexport auto_ignore # don't pass this to invoked scripts except through fwd()
94 12713 aaronmk
if test "$auto_ignore"; then
95
fallback() # don't generate error that it doesn't exist
96
{ echo_func; log_info "ignoring non-existant target: $*"; }
97
fi
98
99 12711 aaronmk
# by default, use all subdirs, including .*
100
if ! isset subdirs; then subdirs=($(enter_top_dir; wildcard. {.,}*/)); fi
101 12829 aaronmk
log-- echo_vars subdirs
102 12711 aaronmk
103
fwd() # usage: [subdirs=(...);] fwd target args... # or use fwd_self
104 10271 aaronmk
{
105
	echo_func
106
	: "${subdirs?}"
107
108 12700 aaronmk
	local_export auto_ignore=1
109 12712 aaronmk
	for subdir in "${subdirs[@]}"; do
110
		local runscript="$top_dir"/"$subdir"/run
111
		if test -f "$runscript"; then "$runscript" "$@"; fi
112
	done
113 10271 aaronmk
}
114 12697 aaronmk
alias fwd_self='fwd "$FUNCNAME" "$@"' # usage: target() { ...; fwd_self; }
115 10271 aaronmk
116 12695 aaronmk
: "${auto_fwd=}" # usage: auto_fwd=1; . .../file_including_util.run
117 12713 aaronmk
# must be set *after* auto_ignore's fallback() to overwrite it
118 12695 aaronmk
if test "$auto_fwd"; then
119
fallback() { echo_func; fwd "$@"; }
120 8704 aaronmk
fi
121 12695 aaronmk
122 12964 aaronmk
123
#### `make` compatibility
124
125 12967 aaronmk
main() { begin_target; with_rm all; } # support conventional `all` target
126 12964 aaronmk
127 12695 aaronmk
fi