forked from johanhaleby/bash-templater
-
Notifications
You must be signed in to change notification settings - Fork 2
/
templater.sh
executable file
·332 lines (296 loc) · 9.38 KB
/
templater.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
#
# Very simple templating system that replaces {{VAR}} by the value of $VAR.
# Supports default values by writting {{VAR=value}} in the template.
#
# Copyright (c) 2017 Sébastien Lavoie
# Copyright (c) 2017 Johan Haleby
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# See: https://github.com/johanhaleby/bash-templater
# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941
# Replaces all {{VAR}} by the $VAR value in a template file and outputs it
readonly PROGNAME=$(basename $0)
case "$OSTYPE" in
*darwin*)
BSD=1
;;
*linux*)
GNU=1
;;
esac
usage="${PROGNAME} [-h] [-d] [-f] [-s] [-o] [-r] [-q] --
where:
-h, --help
Show this help text
-p, --print
Don't do anything, just print the result of the variable expansion(s)
-f, --file
Specify a file to read variables from
-s, --silent
Don't print warning messages (for example if no variables are found)
-d, --delimiter
Specify a delimiter to separate output from multiple files (defaults to '\n---\n')
-o --output
Specify to write the output to files in the given directory instead of stdout
-r --recursive
Searches recursively for files in the template directory
-q --quiet
Don't output anything but errors (for use with -o)
examples:
VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt
${PROGNAME} test.txt -f my-variables.txt
${PROGNAME} test.txt -f my-variables.txt > new-test.txt"
if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi
if [[ ! -f "${1}" ]] && [[ ! -d "${1}" ]]; then
echo "You need to specify a template file or directory" >&2
echo "$usage"
exit 1
fi
function load_env_file() {
local env_file="$1"
if [[ -f "$env_file" ]]; then
local variables
if [[ "$BSD" ]]; then
variables=$(grep -v '^#' "$env_file" | grep -v '^\w*$' | xargs -0)
else
variables=$(grep -v '^#' "$env_file" | grep -v '^\w*$' | xargs -d '\n')
fi
for var in $variables; do
export "${var?}"
done
fi
}
function errcho() {
echo "$@" 1>&2
}
function parse_args() {
template_path="${1}"
delimiter="\n---\n"
print_only="false"
silent="false"
if [ "$#" -ne 0 ]; then
while [ "$#" -gt 0 ]
do
case "$1" in
-h|--help)
echo "$usage"
exit 0
;;
-p|--print)
print_only="true"
;;
-f|--file)
load_env_file "$2"
;;
-s|--silent)
silent="true"
;;
-d|--delimiter)
delimiter="$2"
;;
-o|--output)
output="$2"
;;
-r|--recursive)
recursive="true"
;;
-q|--quiet)
quiet="true"
;;
--)
break
;;
-*)
echo "Invalid option '$1'. Use --help to see the valid options" >&2
exit 1
;;
# an option argument, continue
*) ;;
esac
shift
done
fi
}
##
# Escape custom characters in a string
# Example: escape "ab'\c" '\' "'" ===> ab\'\\c
#
function escape_chars() {
local content="${1}"
shift
for char in "$@"; do
content="${content//${char}/\\${char}}"
done
echo "${content}"
}
function echo_var() {
local var="${1}"
local content="${2}"
local escaped="$(escape_chars "${content}" "\\" '"')"
echo "${var}=\"${escaped}\""
}
function var_value() {
var="${1}"
eval echo \$"${var}"
}
function perl_match() {
perl - "$TEMPLATE_CONTENT" $1 <<'EOF'
my $string = shift;
my $index = shift;
my $regex = qr/\s*{%\s*if\s*(.*?(?=%}))%}(.*?(?={%))(\s*{%\s*else\s*%})?(.*?(?={%))\s*{%\s*endif\s*%}/sp;
my @matches = ( $string =~ /$regex/ );
if (! @matches) {
exit 1;
}
if ( $index == -1 ){
# if ($string =~ /$regex/ ) {
print "${^MATCH}";
# }
# print "${^PREMATCH}";
# print "${^POSTMATCH}";
}
else{
print "@matches[$index]";
}
EOF
}
function replace_ifs() {
while perl_match -1 /dev/null 2>&1; do
match=$(perl_match -1) > /dev/null
condition=$(perl_match 0) > /dev/null
case_true=$(perl_match 1) > /dev/null
case_false=$(perl_match 3) > /dev/null
if eval "$condition"; then
replace="$case_true"
else
replace="$case_false"
fi
TEMPLATE_CONTENT="${TEMPLATE_CONTENT/"$match"/$replace}"
done
}
function render(){
vars=$(echo "$TEMPLATE_CONTENT" | grep -oE '\{\{[[:space:]]*[A-Za-z0-9_]+[[:space:]]*\}\}' | sort | uniq | sed -e 's/^{{//' -e 's/}}$//')
if [[ -z "$vars" ]]; then
if [[ "$silent" == "false" ]]; then
echo "Warning: No variable was found in $template, syntax is {{VAR}}" >&2
fi
return 0
fi
declare -a replaces
replaces=()
# Reads default values defined as {{VAR=value}} and delete those lines
# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}}
# You can even reference variables defined in the template before
defaults=$(echo "$TEMPLATE_CONTENT" | grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}$' | sed -e 's/^{{//' -e 's/}}$//')
IFS=$'\n'
for default in $defaults; do
var=$(echo "${default}" | grep -oE "^[A-Za-z0-9_]+")
current="$(var_value "${var}")"
# Replace only if var is not set
if [[ -n "$current" ]]; then
eval "$(echo_var "${var}" "${current}")"
else
eval "${default}"
fi
# remove define line
replaces+=("-e")
replaces+=("/^{{${var}=/d")
vars="${vars} ${var}"
done
vars="$(echo "${vars}" | tr " " "\n" | sort | uniq)"
if [[ "$2" = "-h" ]]; then
for var in $vars; do
value="$(var_value "${var}")"
echo_var "${var}" "${value}"
done
exit 0
fi
if [[ "$print_only" == "true" ]]; then
for var in $vars; do
value=$(var_value "$var")
echo "$var=$value"
done
exit 0
fi
# Replace all {{VAR}} by $VAR value
for var in $vars; do
value="$(var_value "${var}")"
if [[ -z "$value" ]] && [[ "$silent" == "false" ]]; then
echo "Warning: $var is not defined and no default is set, replacing by empty" >&2
fi
# Escape slashes
value="$(escape_chars "${value}" "\\" '/' ' ')";
replaces+=("-e")
replaces+=("s/{{[[:space:]]*${var}[[:space:]]*}}/${value}/g")
done
TEMPLATE_CONTENT="$(echo "$TEMPLATE_CONTENT" | sed "${replaces[@]}")"
}
function main() {
[[ $TRACE ]] && set -x
local template_path
template_path="$1"
TEMPLATE_CONTENT="$(cat "$1")"
if [[ -f ".env" ]]; then
load_env_file ".env"
fi
replace_ifs > /dev/null 2>&1
render
if [[ -z "$quiet" ]]; then
echo "$TEMPLATE_CONTENT"
fi
if [[ ! -z "$output" ]]; then
mkdir -p "$(dirname "$output/$template_path")"
echo "$TEMPLATE_CONTENT" >> "$output/$template_path"
fi
}
function template_dir() {
shopt -s globstar nullglob 2>/dev/null
templates=( "$1/"* )
len=${#templates[@]}
len=$((len-1))
needs_delim="false"
for i in $(seq 0 $len); do
if [[ -d "${templates[i]}" ]] && [[ -z "$recursive" ]]; then
needs_delim="false"
continue;
fi
if [[ "$needs_delim" == "true" ]] && [[ -z "$output" ]]; then
echo -e "$delimiter"
fi
if [[ -f "${templates[i]}" ]]; then
main "${templates[i]}"
elif [[ -d "${templates[i]}" ]] && [[ ! -z "$recursive" ]] && [[ "$recursive" == "true" ]]; then
template_dir "${templates[i]}"
fi
if [ $i -lt $len ] && [ -z "$output" ]; then
needs_delim="true";
fi
done
}
parse_args "$@"
if [[ -f "$template_path" ]]; then
main "$template_path"
elif [[ -d "$template_path" ]]; then
template_dir "$template_path"
fi