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
set_var () { eval "$1"'="$2"'; }
22

    
23
reverse () # usage: array=($(reverse args...))
24
{
25
	local i
26
	for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
27
}
28

    
29
echo_cmd () { echo "$PS4$*" >&2; }
30

    
31
echo_run () { echo_cmd "$@"; "$@"; }
32

    
33
canon_rel_path ()
34
{
35
	local path="$1"
36
	path="$(readlink -f -- "$path")" # canonicalize
37
	path="${path#$PWD/}" # remove any shared prefix with the current dir
38
	echo "$path"
39
}
40

    
41
# usage: echo_func "$@"
42
echo_func ()
43
{
44
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
45
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
46
}
47

    
48
echo_stdin () { tee -a /dev/stderr; } # usage: input|echo_stdin|cmd
49

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

    
52
echo_export ()
53
{
54
	export "$@"
55
	echo_vars "$@"
56
}
57

    
58
alias export="echo_export" # automatically echo env vars when they are set
59

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

    
62
top_dir="$(dirname "$0")" # outermost script
63

    
64
run_args_cmd () # runs the command line args command
65
{
66
	test "$?" -eq 0 || return
67
	eval set -- "$(reverse "${BASH_ARGV[@]}")"
68
	test "$#" -ge 1 || set -- all
69
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
70
}
71

    
72
# users can override this function to perform other commands (or no commands)
73
# after the script is read
74
on_exit () { run_args_cmd; }
75
trap on_exit EXIT
76

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

    
87
make ()
88
{
89
	echo_func "$@"
90
	echo_run env make --directory="$top_dir" "$@"
91
}
92

    
93
if false; then ## usage:
94
inline_make 3<<'EOF'
95
target:
96
	$(self_dir)/cmd >$@
97
EOF
98
# target will be run automatically because it's first in the makefile
99
fi ##
100
inline_make ()
101
{
102
	echo_func "$@"
103
	local self="$(readlink -f "${BASH_SOURCE[1]}")"
104
	local self_dir="$(dirname "$self")"
105
	export self self_dir
106
	
107
	make --makefile=<((
108
		cat /dev/fd/3
109
		echo -n "
110
.SUFFIXES: # turn off built-in suffix rules
111
.SECONDARY: # don't automatically delete intermediate files
112
.DELETE_ON_ERROR: # delete target if recipe fails
113
"
114
	)|echo_stdin) "$@"
115
}
116

    
117
alias zip="echo_run zip"
118
alias unzip="echo_run unzip"
119
alias zip_newer="zip -u"
120
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
(46-46/51)