Project

General

Profile

1
#!/bin/bash -e
2
shopt -s expand_aliases
3
# run scripts: a bash-based replacement for make
4
# unlike make, supports full bash functionality including multiline commands
5
# usage: path/to/dir/run cmd args
6

    
7
if false; then #### run script template:
8
#!/bin/bash -e
9
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/util.run or file_including_util.run
10
. "$(dirname "${BASH_SOURCE[0]}")"/other_includes
11

    
12
cmd ()
13
{
14
	echo_func "$@"
15
	"$(dirname "${BASH_SOURCE[0]}")"/path_relative_to_self
16
	"$(dirname "${BASH_SOURCE[1]}")"/path_relative_to_caller
17
	"$top_dir"/path_relative_to_outermost_script
18
}
19
fi ####
20

    
21
include_guard_var () { readlink -f -- "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
22

    
23
self_not_included () # usage: if self_not_included; then ... fi
24
{
25
	test "$#" -ge 1 || set -- "${BASH_SOURCE[1]}"
26
	local include_guard="$(include_guard_var "$1")"
27
	test -z "${!include_guard+t}" && eval "$include_guard"=1
28
}
29

    
30
if self_not_included "${BASH_SOURCE[0]}"; then
31

    
32
set_var () { eval "$1"'="$2"'; }
33

    
34
reverse () # usage: array=($(reverse args...))
35
{
36
	local i
37
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
38
}
39

    
40
echo_cmd () { echo "$PS4$*" >&2; }
41

    
42
echo_run () { echo_cmd "$@"; "$@"; }
43

    
44
canon_rel_path ()
45
{
46
	local path="$1"
47
	path="$(readlink -f -- "$path")" # canonicalize
48
	path="${path#$PWD/}" # remove any shared prefix with the current dir
49
	echo "$path"
50
}
51

    
52
# usage: echo_func "$@"
53
echo_func ()
54
{
55
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
56
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
57
}
58

    
59
echo_stdin () # usage: input|echo_stdin|cmd
60
{
61
	echo ----- >&2
62
	tee -a /dev/stderr;
63
	echo ----- >&2
64
}
65

    
66
echo_vars () { declare -p "${@%%=*}" >&2; } # usage: echo_vars var...
67

    
68
echo_export ()
69
{
70
	builtin export "$@"
71
	echo_vars "$@"
72
}
73

    
74
alias export="echo_export" # automatically echo env vars when they are set
75

    
76
usage () { echo "Usage: $1" >&2; (exit 2); }
77

    
78
top_dir="$(dirname "$0")" # outermost script
79

    
80
run_args_cmd () # runs the command line args command
81
{
82
	test "$?" -eq 0 || return
83
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
84
	test "$#" -ge 1 || set -- all
85
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
86
}
87

    
88
# users can override this function to perform other commands (or no commands)
89
# after the script is read
90
on_exit () { run_args_cmd; }
91
trap on_exit EXIT
92

    
93
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
94
{
95
	echo_func "$@"
96
	: "${subdirs?}"
97
	
98
	for subdir in "${subdirs[@]}"; do
99
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
100
	done
101
}
102

    
103
make ()
104
{
105
	echo_func "$@"
106
	echo_run env make --directory="$top_dir" "$@"
107
}
108

    
109
if false; then ## usage:
110
inline_make 3<<'EOF'
111
target:
112
	$(self_dir)/cmd >$@
113
EOF
114
# target will be run automatically because it's first in the makefile
115
fi ##
116
inline_make ()
117
{
118
	echo_func "$@"
119
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
120
	local self_dir="$(dirname "$self")"
121
	export self self_dir
122
	
123
	make --makefile=<((
124
		cat /dev/fd/3
125
		echo -n "
126
.SUFFIXES: # turn off built-in suffix rules
127
.SECONDARY: # don't automatically delete intermediate files
128
.DELETE_ON_ERROR: # delete target if recipe fails
129
"
130
	)|echo_stdin) "$@"
131
}
132

    
133
alias zip="echo_run zip"
134
alias unzip="echo_run unzip"
135
alias zip_newer="zip -u"
136
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
137

    
138
fi
(47-47/52)