refactor variable replacements into own function

This commit is contained in:
Václav Uruba 2023-09-10 16:37:36 +02:00
parent 93c88daf78
commit f39ea95089
Signed by: uruba
GPG Key ID: 9D8E987C4B2E1E9C

22
mige.sh
View File

@ -32,6 +32,18 @@ read_options() {
done
}
replace_variables() {
while read -r variable; do
if [[ $variable =~ $REGEX_VARIABLE ]]; then
local variable_name=${BASH_REMATCH[1]}
local variable_value=${BASH_REMATCH[2]}
sed -i "s/\$$variable_name/$variable_value/g;/$variable/d" $1
else
echo -e "\e[38;5;208mWarning: could not match '$variable' as a variable assignment.\e[0m"
fi
done < <(grep -P $REGEX_VARIABLE $1)
}
replace_embeds () {
while read match; do
IFS=$SEPARATOR_EMBED read -ra parts <<< "$match"
@ -58,16 +70,8 @@ process_file () {
mkdir -p $( dirname "$1" | sed "s/$sourcediresc\/$DIR_CONTENT/$sourcediresc\/$DIR_DIST/" )
local file_output=$( echo "$1" | sed "s/$sourcediresc\/$DIR_CONTENT/$sourcediresc\/$DIR_DIST/" )
cat "$SOURCEDIR/$TEMPLATE_HEADER" "$1" "$SOURCEDIR/$TEMPLATE_FOOTER" > $file_output
while read -r variable; do
if [[ $variable =~ $REGEX_VARIABLE ]]; then
local variable_name=${BASH_REMATCH[1]}
local variable_value=${BASH_REMATCH[2]}
sed -i "s/\$$variable_name/$variable_value/g;/$variable/d" $file_output
else
echo -e "\e[38;5;208mWarning: could not match '$variable' as a variable assignment.\e[0m"
fi
done < <(grep -P $REGEX_VARIABLE $file_output)
replace_variables $file_output
replace_embeds $file_output
timep=$((($(date +%s%N) - $timep)/1000000))