-
Notifications
You must be signed in to change notification settings - Fork 47
/
configure.sh
executable file
·107 lines (79 loc) · 2.53 KB
/
configure.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
#!/usr/bin/env bash
# Checked with ShellCheck (https://www.shellcheck.net/)
#/ Initialization script for Kurento documentation.
#/
#/ This shell script prepares the sources for building. The main work done here
#/ is replacing all |PLACEHOLDER| variables with their actual values, as defined
#/ in `VERSIONS.env`.
#/
#/ Arguments: None.
# Configure shell
# ===============
# Bash options for strict error checking.
set -o errexit -o errtrace -o pipefail -o nounset
shopt -s inherit_errexit 2>/dev/null || true
# Trace all commands (to stderr).
set -o xtrace
# Absolute Canonical Path to the directory that contains this script.
SELF_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null && pwd -P)"
# Help message.
# Extracts and prints text from special comments in the script header.
function usage { grep '^#/' "${BASH_SOURCE[-1]}" | cut -c 4-; exit 0; }
if [[ "${1:-}" =~ ^(-h|--help)$ ]]; then usage; fi
# Load VERSIONS file
# ==================
CONF_FILE="$SELF_DIR/VERSIONS.env"
[[ -f "$CONF_FILE" ]] || {
echo "ERROR: Shell config file not found: $CONF_FILE"
exit 1
}
# shellcheck source=VERSIONS.env
source "$CONF_FILE"
# Parse call arguments
# ====================
CFG_SOURCE_PATH="$SELF_DIR"
while [[ $# -gt 0 ]]; do
case "${1-}" in
--source)
if [[ -n "${2-}" ]]; then
CFG_SOURCE_PATH="$(realpath "$2")"
shift
else
log "ERROR: --source expects <Path>"
exit 1
fi
;;
*)
log "ERROR: Unknown argument '${1-}'"
exit 1
;;
esac
shift
done
# Replace placeholders
# ====================
# This expects a Bash Associative Array;
# enumerates all entries to use their name and values.
for NAME in "${!PROJECT_VERSIONS[@]}"; do
VALUE="${PROJECT_VERSIONS[$NAME]}"
# Use long option names for readability. Equivalent to this:
# grep -lIrZ -- '<OldPattern>' | xargs -0 -L1 -r sed -i -e 's/<OldPattern>/<NewPattern>/g'
# grep's exit code 1 means no lines selected, and >1 means an error.
# Thus, exit code 1 must be handled to prevent `-o pipefail` from failing.
{
grep \
--files-with-matches \
--binary-files=without-match \
--recursive \
--null \
"|$NAME|" "$CFG_SOURCE_PATH" \
|| [[ $? == 1 ]]
} | xargs \
--null \
--max-lines=1 \
--no-run-if-empty \
sed \
--in-place --expression="s/|$NAME|/$VALUE/g"
done
echo "Done!"
exit 0