-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·50 lines (41 loc) · 1.1 KB
/
build.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
#!/usr/bin/env bash
HOME=$(pwd)
OUTPUT=${HOME}/output
# Check commands exist
if [[ ! "$(command -v yq)" ]]; then
echo "Please install yq first: https://github.com/mikefarah/yq"
exit 1
fi
# Check config yaml file
if [[ ! -f "$1" ]]; then
echo "Config file $1 doesn't exist!"
echo "./build.sh <config.yaml>"
exit 1
fi
# Create directory for output
mkdir -p "$OUTPUT"
for filename in $(yq '.[] | key' "$1"); do
echo "Generating ${filename}..."
if [[ ! -f "$filename" ]]; then
echo "$filename doesn't exist!"
break
fi
if [[ -n "$2" && "$2" != *"$filename"* ]]; then
echo "Skip $filename!"
continue
fi
if [[ "$filename" = *"/"* ]]; then
mkdir -p "$OUTPUT/$(dirname "$filename")"
fi
cmd="sed"
for key in $(yq '.["'$filename'"][] | key' "$1"); do
var=$(yq '.["'$filename'"]["'$key'"]' "$1")
echo " Key: $key, Value: $var"
cmd="$cmd -e 's/%$key%/$var/g'"
done
cmd="$cmd $HOME/$filename > $OUTPUT/$filename"
eval "$cmd"
done
# Show unreplaced variables
echo
grep -rE '%\w\w+%' "$OUTPUT"/*