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
	#...
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
# allow caller to override, eg. to force runscript mode when using $wrap_fn
40
: "${is_runscript=$(ends_with '[/.]run' "$top_script_abs"; exit2bool)}"
41

    
42
if is_dot_script; then
43
	if test "$1" = .; then set --; fi # $@ wrong: no args->contains script name
44
	include_args=("$@") # from `. .../util.run "$@"`
45
fi
46

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

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

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

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

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

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

    
85

    
86
#### utils
87

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

    
94
func_override sudo__util_sh
95
function sudo() # usage: sudo fn|cmd ... # supports shell functions as well
96
{
97
	echo_func
98
	if test "$1" = command; then shift # always executable->$wrap_fn not needed 
99
	elif is_intern "$1"; then set -- $wrap_fn "$@" # shell function, etc.
100
	fi
101
	sudo__util_sh "$@"
102
}
103

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

    
112
,() # usage: .../run , cmd1 cmd2 ... (like `make cmd1 cmd2 ...`)
113
# treats each of the command-line args as commands the way make does
114
# (instead of as args to the same command, the way runscripts do)
115
{ begin_target; for cmd in "$@"; do time with_rm echo_run "$cmd"; done; }
116

    
117
: "${auto_ignore=}" #usage: auto_ignore=1 .../__.run target_that_might_not_exist
118
unexport auto_ignore # don't pass this to invoked scripts except through fwd()
119
if test "$auto_ignore"; then
120
fallback() # don't generate error that it doesn't exist
121
{ echo_func; log_info "ignoring non-existant target: $*"; }
122
fi
123

    
124
# by default, use all subdirs, including .*
125
if ! isset subdirs; then subdirs=($(enter_top_dir; wildcard. {.,}*/)); fi
126
echo_vars subdirs
127

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

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

    
147

    
148
#### `make` compatibility
149

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

    
152
fi
(16-16/18)