Project

General

Profile

1 8291 aaronmk
#!/bin/bash -e
2
shopt -s expand_aliases
3 8272 aaronmk
# 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 8291 aaronmk
#!/bin/bash -e
9 8272 aaronmk
. "$(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 8463 aaronmk
	echo_func "$@"
15 8272 aaronmk
	"$(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 8703 aaronmk
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 8704 aaronmk
if self_not_included "${BASH_SOURCE[0]}"; then
31
32 8694 aaronmk
set_var () { eval "$1"'="$2"'; }
33
34 8691 aaronmk
reverse () # usage: array=($(reverse args...))
35
{
36
	local i
37
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
38
}
39
40 8278 aaronmk
echo_cmd () { echo "$PS4$*" >&2; }
41 8272 aaronmk
42 8278 aaronmk
echo_run () { echo_cmd "$@"; "$@"; }
43
44 8646 aaronmk
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 8463 aaronmk
# usage: echo_func "$@"
53
echo_func ()
54
{
55 8647 aaronmk
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
56
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
57 8463 aaronmk
}
58 8279 aaronmk
59 8702 aaronmk
echo_stdin () # usage: input|echo_stdin|cmd
60
{
61
	echo ----- >&2
62
	tee -a /dev/stderr;
63
	echo ----- >&2
64
}
65 8275 aaronmk
66 8650 aaronmk
echo_vars () { declare -p "${@%%=*}" >&2; } # usage: echo_vars var...
67 8641 aaronmk
68
echo_export ()
69
{
70 8700 aaronmk
	builtin export "$@"
71 8641 aaronmk
	echo_vars "$@"
72
}
73
74 8643 aaronmk
alias export="echo_export" # automatically echo env vars when they are set
75 8642 aaronmk
76 8272 aaronmk
usage () { echo "Usage: $1" >&2; (exit 2); }
77
78
top_dir="$(dirname "$0")" # outermost script
79
80 8465 aaronmk
run_args_cmd () # runs the command line args command
81 8272 aaronmk
{
82 8292 aaronmk
	test "$?" -eq 0 || return
83 8693 aaronmk
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
84 8290 aaronmk
	test "$#" -ge 1 || set -- all
85 8648 aaronmk
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
86 8272 aaronmk
}
87
88 8464 aaronmk
# users can override this function to perform other commands (or no commands)
89
# after the script is read
90 8465 aaronmk
on_exit () { run_args_cmd; }
91 8464 aaronmk
trap on_exit EXIT
92
93 8284 aaronmk
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
94 8272 aaronmk
{
95 8463 aaronmk
	echo_func "$@"
96 8284 aaronmk
	: "${subdirs?}"
97
98 8272 aaronmk
	for subdir in "${subdirs[@]}"; do
99 8280 aaronmk
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
100 8272 aaronmk
	done
101
}
102
103 8280 aaronmk
make ()
104
{
105 8463 aaronmk
	echo_func "$@"
106 8280 aaronmk
	echo_run env make --directory="$top_dir" "$@"
107
}
108 8272 aaronmk
109 8276 aaronmk
if false; then ## usage:
110 8652 aaronmk
inline_make 3<<'EOF'
111 8276 aaronmk
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 8653 aaronmk
	echo_func "$@"
119 8640 aaronmk
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
120
	local self_dir="$(dirname "$self")"
121 8645 aaronmk
	export self self_dir
122 8640 aaronmk
123 8651 aaronmk
	make --makefile=<((
124 8652 aaronmk
		cat /dev/fd/3
125 8651 aaronmk
		echo -n "
126 8276 aaronmk
.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 8651 aaronmk
"
130
	)|echo_stdin) "$@"
131 8276 aaronmk
}
132 8658 aaronmk
133
alias zip="echo_run zip"
134
alias unzip="echo_run unzip"
135 8659 aaronmk
alias zip_newer="zip -u"
136
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
137 8704 aaronmk
138
fi