Files
bee/iso/builder/lib/template.sh
T

56 lines
1.5 KiB
Bash

require_template_placeholder() {
file="$1"
placeholder="$2"
if [ -z "$placeholder" ]; then
echo "ERROR: template placeholder must not be empty" >&2
return 1
fi
if ! BEE_TEMPLATE_PLACEHOLDER="$placeholder" awk '
index($0, ENVIRON["BEE_TEMPLATE_PLACEHOLDER"]) { found=1 }
END { exit found ? 0 : 1 }
' "$file"; then
echo "ERROR: required placeholder $placeholder is missing from $file" >&2
return 1
fi
}
replace_literal_in_file() {
file="$1"
placeholder="$2"
replacement="$3"
require_template_placeholder "$file" "$placeholder" || return 1
tmp="$(mktemp "${TMPDIR:-/tmp}/bee-template.XXXXXX")"
if BEE_TEMPLATE_PLACEHOLDER="$placeholder" \
BEE_TEMPLATE_REPLACEMENT="$replacement" \
awk '
function replace_literal(text, needle, value, at) {
while ((at = index(text, needle)) != 0) {
text = substr(text, 1, at - 1) value substr(text, at + length(needle))
}
return text
}
{
print replace_literal($0,
ENVIRON["BEE_TEMPLATE_PLACEHOLDER"],
ENVIRON["BEE_TEMPLATE_REPLACEMENT"])
}
' "$file" > "$tmp" && cat "$tmp" > "$file"; then
status=0
else
status=$?
fi
rm -f "$tmp"
return "$status"
}
validate_project_version() {
version="$1"
case "$version" in
""|[!A-Za-z0-9]*|*[!A-Za-z0-9]|*[!A-Za-z0-9.-]*)
return 1
;;
esac
return 0
}