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