Project

General

Profile

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
	"$(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
}
21

    
22
all()
23
{
24
	begin_target
25
	target # defined above
26
	#...
27
}
28

    
29
on_exit # needed if should be runnable as shell-include (with leading ".")
30
fi ####
31

    
32
. "$(dirname "${BASH_SOURCE[0]}")"/../sh/util.sh
33
.rel ../sh/make.sh
34

    
35
if self_not_included; then
36

    
37

    
38
#### setup
39

    
40
# allow caller to override, eg. to force runscript mode when using $wrap_fn
41
: "${is_runscript=$(ends_with '[/.]run' "$top_script_abs"; exit2bool)}"
42
unexport is_runscript # don't pass to invoked scripts
43

    
44
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

    
49
fallback() # overridable handler for targets w/o function
50
{ echo_func; "$@"; } # by default, generate error that it doesn't exist
51

    
52
gateway() # overridable handler for *all* targets
53
{
54
	echo_func
55
	test $# -gt 0 || set -- main # default target
56
	if is_callable "$1"; then "$@"; else fallback "$@"; fi
57
}
58

    
59
if test ! "$is_runscript"; then # non-runscript
60
gateway() { echo_func; main "$@"; } # pass all args to main()
61
fi
62

    
63
run_args_cmd() # runs the command line args command
64
{
65
	if is_dot_script; then set -- "${include_args[@]}"
66
	else              eval set -- "$(reverse "${BASH_ARGV[@]}")"
67
	fi
68
	echo_cmd "$top_script" "$@"
69
	indent; time gateway "$@"
70
}
71

    
72
# users can override run_args_cmd()/gateway()/fallback() to perform other
73
# commands (or no commands) after the script is read
74
on_exit() { test $? -eq 0 || return; run_args_cmd; }
75
if ! is_dot_script; then trap on_exit EXIT; fi
76

    
77
func_override set_paths__util_sh
78
set_paths()
79
{
80
	set_paths__util_sh "$@"
81
	log_local; log++
82
	top_file="${top_script%[./]run}"; echo_vars top_file
83
	top_filename="$(basename "$top_file")"; echo_vars top_filename
84
}
85
set_paths
86

    
87

    
88
#### utils
89

    
90
# usage: sudo $wrap_fn fn ...
91
wrap_fn="$(esc env is_runscript=1 "$top_script")"
92
	# invoked script must always run as runscript so that wrapped command is run
93
	# if sudo to non-root and $top_script not world-executable, produces error
94

    
95
func_override sudo__util_sh
96
function sudo() # usage: sudo fn|cmd ... # supports shell functions as well
97
{
98
	echo_func
99
	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
	sudo__util_sh "$@"
103
}
104

    
105
# usage: to_top_file cmd...
106
function to_top_file()
107
{
108
	echo_func
109
	stdout="$top_file" if_not_exists="$(bool! "$_remake")" to_file "$@"
110
}
111
alias to_top_file='"to_top_file" ' # last space alias-expands next word
112

    
113
,() # 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
{ begin_target; for cmd in "$@"; do time with_rm echo_run "$cmd"; done; }
117

    
118
: "${auto_ignore=}" #usage: auto_ignore=1 .../__.run target_that_might_not_exist
119
unexport auto_ignore # don't pass this to invoked scripts except through fwd()
120
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
# by default, use all subdirs, including .*
126
if ! isset subdirs; then subdirs=($(enter_top_dir; wildcard. {.,}*/)); fi
127
echo_vars subdirs
128

    
129
fwd() # usage: [subdirs=(...);] fwd target args... # or use fwd_self
130
{
131
	echo_func
132
	: "${subdirs?}"
133
	
134
	local_export auto_ignore=1
135
	for subdir in "${subdirs[@]}"; do
136
		local runscript="$top_dir"/"$subdir"/run
137
		if test -f "$runscript"; then "$runscript" "$@"; fi
138
	done
139
}
140
alias fwd_self='fwd "$FUNCNAME" "$@"' # usage: target() { ...; fwd_self; }
141

    
142
: "${auto_fwd=}" # usage: auto_fwd=1; . .../file_including_util.run
143
# must be set *after* auto_ignore's fallback() to overwrite it
144
if test "$auto_fwd"; then
145
fallback() { echo_func; fwd "$@"; }
146
fi
147

    
148

    
149
#### `make` compatibility
150

    
151
main() { begin_target; with_rm all; } # support conventional `all` target
152

    
153
fi
(16-16/18)