1
|
#!/bin/bash -e
|
2
|
realpath () { readlink -f -- "$1"; }
|
3
|
|
4
|
include_guard_var () { realpath "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
|
5
|
|
6
|
self_not_included () # usage: if self_not_included; then ... fi
|
7
|
{
|
8
|
test "$#" -ge 1 || set -- "${BASH_SOURCE[1]}"
|
9
|
local include_guard="$(include_guard_var "$1")"
|
10
|
alias self_being_included=false
|
11
|
test -z "${!include_guard+t}" && \
|
12
|
{ eval "$include_guard"=1; alias self_being_included=true; }
|
13
|
}
|
14
|
|
15
|
# to load newly-defined aliases for use in functions in the same file:
|
16
|
## fi # load new aliases
|
17
|
## if self_being_included; then
|
18
|
# this is needed because aliases defined inside an if statement are not
|
19
|
# available inside that if statement
|
20
|
|
21
|
if self_not_included "${BASH_SOURCE[0]}"; then
|
22
|
|
23
|
shopt -s expand_aliases
|
24
|
|
25
|
set_var () { eval "$1"'="$2"'; }
|
26
|
|
27
|
reverse () # usage: array=($(reverse args...))
|
28
|
{
|
29
|
local i
|
30
|
for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
|
31
|
}
|
32
|
|
33
|
: "${verbosity:=$verbose}" "${verbosity:=0}"
|
34
|
|
35
|
echo_cmd () { echo "$PS4$*" >&2; }
|
36
|
|
37
|
echo_run () { echo_cmd "$@"; "$@"; }
|
38
|
|
39
|
canon_rel_path ()
|
40
|
{
|
41
|
local path="$1"
|
42
|
path="$(realpath "$path")" # canonicalize
|
43
|
path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
|
44
|
echo "$path"
|
45
|
}
|
46
|
|
47
|
# usage: echo_func "$@"
|
48
|
echo_func ()
|
49
|
{
|
50
|
local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
|
51
|
echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
|
52
|
}
|
53
|
|
54
|
echo_stdin () # usage: input|echo_stdin|cmd
|
55
|
{
|
56
|
echo ----- >&2
|
57
|
tee -a /dev/stderr;
|
58
|
echo ----- >&2
|
59
|
}
|
60
|
|
61
|
echo_vars () { declare -p "${@%%=*}" >&2; } # usage: echo_vars var...
|
62
|
|
63
|
echo_export ()
|
64
|
{
|
65
|
builtin export "$@"
|
66
|
echo_vars "$@"
|
67
|
}
|
68
|
|
69
|
if test "$verbosity" -ge 2; then
|
70
|
alias export="echo_export" # automatically echo env vars when they are set
|
71
|
fi
|
72
|
|
73
|
usage () { echo "Usage: $1" >&2; (exit 2); }
|
74
|
|
75
|
top_dir="$(dirname "$0")" # outermost script
|
76
|
|
77
|
run_args_cmd () # runs the command line args command
|
78
|
{
|
79
|
test "$?" -eq 0 || return
|
80
|
eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
81
|
test "$#" -ge 1 || set -- all
|
82
|
echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
|
83
|
}
|
84
|
|
85
|
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
|
86
|
{
|
87
|
echo_func "$@"
|
88
|
: "${subdirs?}"
|
89
|
|
90
|
for subdir in "${subdirs[@]}"; do
|
91
|
"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
|
92
|
done
|
93
|
}
|
94
|
|
95
|
make ()
|
96
|
{
|
97
|
echo_func "$@"
|
98
|
echo_run env make --directory="$top_dir" "$@"
|
99
|
}
|
100
|
|
101
|
if false; then ## usage:
|
102
|
inline_make 3<<'EOF'
|
103
|
target:
|
104
|
$(self_dir)/cmd >$@
|
105
|
EOF
|
106
|
# target will be run automatically because it's first in the makefile
|
107
|
fi ##
|
108
|
inline_make ()
|
109
|
{
|
110
|
echo_func "$@"
|
111
|
local self="$(readlink -f "${BASH_SOURCE[1]}")"
|
112
|
local self_dir="$(dirname "$self")"
|
113
|
export self self_dir
|
114
|
|
115
|
make --makefile=<((
|
116
|
cat /dev/fd/3
|
117
|
echo -n "
|
118
|
.SUFFIXES: # turn off built-in suffix rules
|
119
|
.SECONDARY: # don't automatically delete intermediate files
|
120
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
121
|
"
|
122
|
)|echo_stdin) "$@"
|
123
|
}
|
124
|
|
125
|
alias zip="echo_run zip"
|
126
|
alias unzip="echo_run unzip"
|
127
|
alias zip_newer="zip -u"
|
128
|
alias unzip_newer="unzip -u -o" # -o is safe b/c -u only extracts newer files
|
129
|
|
130
|
#### databases
|
131
|
|
132
|
quote='"'
|
133
|
|
134
|
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
135
|
|
136
|
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
|
137
|
|
138
|
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
|
139
|
alias mk_table_esc="local table_esc; mk_esc_name table"
|
140
|
|
141
|
fi # load new aliases
|
142
|
if self_being_included; then
|
143
|
|
144
|
log_sql () { test "$verbosity" -ge 2; }
|
145
|
|
146
|
### MySQL
|
147
|
|
148
|
mysql ()
|
149
|
{
|
150
|
echo_func "$@"
|
151
|
echo_run env mysql --verbose "$@"
|
152
|
}
|
153
|
|
154
|
mysql_ANSI ()
|
155
|
{
|
156
|
echo_func "$@"
|
157
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
158
|
}
|
159
|
|
160
|
### PostgreSQL
|
161
|
|
162
|
pg_copy_to ()
|
163
|
{
|
164
|
echo_func "$@"
|
165
|
: "${table:?}"; mk_table_esc
|
166
|
: "${pg_copy_format=CSV HEADER}"
|
167
|
psql <<<"COPY $table_esc TO STDOUT $pg_copy_format;"
|
168
|
}
|
169
|
|
170
|
pg_header ()
|
171
|
{
|
172
|
echo_func "$@"
|
173
|
local pg_copy_format="CSV HEADER"
|
174
|
pg_copy_to|echo_stdin
|
175
|
}
|
176
|
|
177
|
pg_export_table_no_header ()
|
178
|
{
|
179
|
echo_func "$@"
|
180
|
local pg_copy_format="CSV"
|
181
|
pg_copy_to
|
182
|
}
|
183
|
|
184
|
pg_export_table_to_dir_no_header ()
|
185
|
{
|
186
|
echo_func "$@"
|
187
|
local table="$1"; shift; mk_table_esc
|
188
|
local cols="$(pg_header "$table")"
|
189
|
pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
|
190
|
}
|
191
|
|
192
|
fi
|