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
echo_cmd () { echo "$PS4$*" >&2; }
22

    
23
echo_run () { echo_cmd "$@"; "$@"; }
24

    
25
canon_rel_path ()
26
{
27
	local path="$1"
28
	path="$(readlink -f -- "$path")" # canonicalize
29
	path="${path#$PWD/}" # remove any shared prefix with the current dir
30
	echo "$path"
31
}
32

    
33
# usage: echo_func "$@"
34
echo_func ()
35
{
36
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
37
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
38
}
39

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

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

    
44
echo_export ()
45
{
46
	export "$@"
47
	echo_vars "$@"
48
}
49

    
50
alias export="echo_export" # automatically echo env vars when they are set
51

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

    
54
top_dir="$(dirname "$0")" # outermost script
55

    
56
run_args_cmd () # runs the command line args command
57
{
58
	test "$?" -eq 0 || return
59
	set -- "${BASH_ARGV[@]}"
60
	test "$#" -ge 1 || set -- all
61
	echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
62
}
63

    
64
# users can override this function to perform other commands (or no commands)
65
# after the script is read
66
on_exit () { run_args_cmd; }
67
trap on_exit EXIT
68

    
69
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
70
{
71
	echo_func "$@"
72
	: "${subdirs?}"
73
	
74
	for subdir in "${subdirs[@]}"; do
75
		"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
76
	done
77
}
78

    
79
make ()
80
{
81
	echo_func "$@"
82
	echo_run env make --directory="$top_dir" "$@"
83
}
84

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

    
109
alias zip="echo_run zip"
110
alias unzip="echo_run unzip"
111
alias zip_newer="zip -u"
112
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
(46-46/51)