-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautotemplate.sh
executable file
·59 lines (52 loc) · 1.73 KB
/
autotemplate.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
#! /bin/bash -eu
tmp_file=${TMP_FILE:-'temp.hpp'}
templates=${TEMPLATES:-'../library/templates'}
base_file=${BASE_FILE:-'../library/template.xml'}
config_file=${CONFIG_DIR:-'C_C__.xml'}
function format() {
# compress multiple empty lines, delete empty lines until content, replace end of line with EOL, drop \r or \n
cat -s "$1" | sed '/[^\r\n]/,$!d; s/$/EOL/' | tr -d '\r\n' | \
# escape xml special characters
sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/EOL/\ /g;'
rm "$1"
}
# skip if no changes
if [ $# -eq 0 ]; then
exit 0
fi
# generate templates
for hppfile in "$@"; do
# run only on hpp files
if [[ ! $hppfile == *'.hpp' ]]; then
continue
fi
# expand and trim
if [[ $hppfile == *'template.hpp' ]]; then
cat "$hppfile" > "$tmp_file"
echo skipped expand and trim for: "$hppfile"
else
# expand includes
# /home/rajyan/.pyenv/shims/oj-bundle "$hppfile" 2> /dev/null | \
echo "$(sed -n -r "s/#include \"(.*)\"/${hppfile%/*}\/\1/p" "$hppfile")" "$hppfile" | xargs cat | \
# delete line starting with (#line | #pragma | #include | using (namespace|lint))
sed '/^#line/d; /^#pragma/d; /^#include/d; /^using\ [nl]/d;' > "$tmp_file"
fi
content=$(format "$tmp_file")
filename=${hppfile%.hpp}
filename=${filename##*/}
shortname=${filename/_/}
shortname=${shortname,,}
NAME="my${shortname}" VALUE=${content} envsubst < "$base_file" > "$templates/$filename.xml"
done
# generate templateSet
templateSet='<templateSet group="C/C++">'
for xml in "$templates"/*; do
[[ -e $xml ]] || break
if [[ $xml == *"$config_file" ]]; then
echo "$xml"
continue
fi
templateSet+=$(cat "$xml")
done
templateSet+='</templateSet>'
echo "$templateSet" > "$templates"/"$config_file"