Skip to content
Snippets Groups Projects
Commit 1ed0c774 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Warn on missing replacements; handle Ruby vars with single and double quotes

parent 0d73c943
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@
# ----------------------------------------------------------------------
# 2020-12-15 v0.0 <axel.hahn@iml.unibe.ch>
# 2021-05-07 v1.0 <axel.hahn@iml.unibe.ch> multiple replacement files
# 2021-05-27 v1.1 <axel.hahn@iml.unibe.ch> Warn on missing replacements; handle Ruby vars with single and double quotes
# ======================================================================
# ----------------------------------------------------------------------
......@@ -78,10 +79,11 @@ if [ $? -ne 0 ]; then
exit 2
fi
echo --- Replacements to apply:
grep "^[a-zA-Z]" $replacefile | grep "="
if [ $? -ne 0 ]; then
_err "ERROR: No replacements were found ... wrong syntax? Use KEY = VALUE."
typeset -i iReplacements=$( grep "^[a-zA-Z]" $replacefile | grep "=" | wc -l )
echo --- Replacements to apply: $iReplacements
if [ $iReplacements -eq 0 ]; then
_err "ERROR: No replacements were found ... maybe a wrong syntax?"
_err " Use KEY=VALUE; no spaces in front; now quotes around VALUE"
exit 2
fi
......@@ -94,19 +96,27 @@ do
key=$( echo $line | cut -f 2- -d ":" | cut -f 1 -d "=" )
val=$( echo $line | cut -f 2- -d "=" )
regex="<\%\=\ *\@replace\[\"$key\"\]\ *\%>"
grep "$regex" $tmpfile >/dev/null || echo "WARNING: template [$tplfile] has no placeholder key [$key]"
# Ruby erb template syntax: <% @replace["mykey"] %>
# sed -i "s#<\%\ *\@replace\[\"$key\"\]\ *\%>#$val#g" $tmpfile
sed -i "s#$regex#$val#g" $tmpfile
# Ruby erb template syntax to write a variable: <% @replace["mykey"] %>
# Here are 2 regex to handle single and double quotes
regex1="<\%\=\ *\@replace\[\'$key\'\]\ *\%>"
regex2="<\%\=\ *\@replace\[\"$key\"\]\ *\%>"
# Show a warning if both regex do not match
grep "$regex1" $tmpfile >/dev/null \
|| grep "$regex2" $tmpfile >/dev/null \
|| echo "WARNING: template [$tplfile] has no placeholder key [$key]"
sed -i "s#$regex1#$val#g" $tmpfile
sed -i "s#$regex2#$val#g" $tmpfile
done
# --- verify changes
echo
echo --- changes:
echo --- Changes:
diff "$tplfile" "$tmpfile"
if [ $? -eq 0 ]; then
_err "ERROR: template was unchanged. Check placeholders in $tplfile and replacements:"
echo Content of relacement files:
cat $replacefile
exit 3
fi
......@@ -115,18 +125,19 @@ fi
mv -f "${tmpfile}" "${outfile}"
echo
echo --- output file:
echo --- Output file:
ls -l "${outfile}"
if [ $? -ne 0 ]; then
_err "ERROR: unable to write target file $outfile."
exit 4
fi
grep '<\%\=\ *\@replace\[\"' ${outfile} >/dev/null
echo --- Check missing replacements
grep '<\%\=\ *\@replace\[' ${outfile} >/dev/null
if [ $? -eq 0 ]; then
echo
_warn "WARNING: Maybe you need to verify replacements (can be a false positive)"
grep '<\%\=\ *\@replace\[\"' ${outfile}
_warn "WARNING: Maybe you need to verify replacements (hmmm, could be a false positive too)"
grep '<\%\=\ *\@replace\[' ${outfile}
else
_ok "OK"
fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment