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 #### run script 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
all()
14
{
15
	echo_func
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
on_exit # needed if should be runnable as shell-include (with leading ".")
22
fi ####
23

    
24
. "$(dirname "${BASH_SOURCE[0]}")"/../sh/util.sh
25
.rel ../sh/make.sh
26

    
27
if self_not_included; then
28

    
29

    
30
#### setup
31

    
32
if is_dot_script; then
33
	if test "$1" = .; then set --; fi # $@ wrong: no args->contains script name
34
	include_args=("$@") # from `. .../util.run "$@"`
35
fi
36

    
37
fallback() { echo_func; "$@"; } # overridable handler for targets w/o function
38

    
39
gateway() # overridable handler for *all* targets
40
{ echo_func; if is_callable "$1"; then "$@"; else fallback "$@"; fi; }
41

    
42
run_args_cmd() # runs the command line args command
43
{
44
	if is_dot_script; then set -- "${include_args[@]}"
45
	else              eval set -- "$(reverse "${BASH_ARGV[@]}")"
46
	fi
47
	test $# -ge 1 || set -- all
48
	echo_cmd "$top_script" "$@"; time gateway "$@"
49
}
50

    
51
# users can override run_args_cmd to perform other commands (or no commands)
52
# after the script is read
53
on_exit() { test $? -eq 0 || return; run_args_cmd; }
54
if ! is_dot_script; then trap on_exit EXIT; fi
55

    
56
func_override set_paths__util_sh
57
set_paths()
58
{
59
	set_paths__util_sh "$@"
60
	top_file="${top_script%[./]run}"; echo_vars top_file
61
	top_filename="$(basename "$top_file")"; echo_vars top_filename
62
}
63
set_paths
64

    
65

    
66
#### utils
67

    
68
wrap_fn="$top_script" # usage: sudo "$wrap_fn" fn ...
69

    
70
# usage: to_top_file cmd...
71
function to_top_file() { echo_func; stdout="$top_file" to_file "$@"; }
72
alias to_top_file='"to_top_file" ' # last space alias-expands next word
73

    
74
,() # usage: .../run , cmd1 cmd2 ... (like `make cmd1 cmd2 ...`)
75
# treats each of the command-line args as commands the way make does
76
# (instead of as args to the same command, the way runscripts do)
77
{ echo_func; for cmd in "$@"; do time echo_run "$cmd"; done; }
78

    
79
fwd() # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
80
{
81
	echo_func
82
	: "${subdirs?}"
83
	
84
	for subdir in "${subdirs[@]}"; do "$top_dir"/"$subdir"/run "$@"; done
85
}
86

    
87
: "${auto_fwd=}" # usage: auto_fwd=1; . .../file_including_util.run
88
if test "$auto_fwd"; then
89
fallback() { echo_func; fwd "$@"; }
90
fi
91

    
92
fi
(14-14/16)