-
Notifications
You must be signed in to change notification settings - Fork 22
/
ur_dict_updater.sh
executable file
·82 lines (69 loc) · 2.31 KB
/
ur_dict_updater.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
DEBUG=1
EXISTING_FILE="`mktemp`"
NEW_FILE="`mktemp`"
TARGET_FILE=unirec_fields.md
# Check for md file
if ! [ -s "$TARGET_FILE" ]; then
echo "Target MD file does not exist or its empty."
echo "Generating new one.."
echo "# About this file
This file contains a list of UniRec fields collected from all parts of project (including git submodules).
The part of this file is generated automatically, so be careful during any editing.
# List of UniRec fields
" >"$TARGET_FILE"
fi
if fgrep -q "# List of UniRec fields" "$TARGET_FILE"; then
existing=1
else
existing=0
fi
if [ "$existing" -eq 1 ]; then
# we need to retrieve the list of fields before any updates
if [ "$DEBUG" -eq 1 ]; then
echo "Existing section, loading..."
fi
# Find the head of the list
sed -n "/^\# List of UniRec fields\s*$/,/^$/p" "$TARGET_FILE" | tail -n +3 | sed 's/| *\([^ ]*\) *| *\([^ ]*\) *| *\([^|]*\)|/\1 \2 \3/g' >"$EXISTING_FILE"
fi
if [ "$DEBUG" -eq 1 ]; then
echo "Searching for files with UR_FIELDS..."
fi
find . \( -name '*.c' -o -name '*.h' -o -name '*.cpp' \) -exec grep -l "\s*UR_FIELDS\s*" {} \; | #tee /dev/stderr |
# remove line and block comments
xargs -I{} sed 's,\s*//.*$,,;:a; s%\(.*\)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' {} |
# print contents of UR_FIELDS
sed -n '/^\s*UR_FIELDS\s*([^)]*$/,/)/p; /^\s*UR_FIELDS\s*([^)]*$/,/)/p' 2>/dev/null |
# clean output to get fields only
sed 's/^\s*UR_FIELDS\s*(\s*//g; s/)//g; s/,/\n/g; /^\s*$/d; s/^\s*//; s/\s\s*/ /g; s/\s\s*$//' |
# sort by name
sort -k2 -t' ' | uniq >> "$EXISTING_FILE"
#merge temporary files together and sort them
cat "$EXISTING_FILE" |tail -n+2 |sort -bk2,2 -bk3,3r |
awk '
BEGIN {
print "# List of UniRec fields"
print "| Field data type | Field name | Description |"
print "| ----- | ----- | ----- |"
}
/^..*$/ {
if (name != $2) {
type=$1
name=$2
desc=$3
for (i=4; i<=NF; i++) {
desc=desc" "$i
}
print "| "type" | "name" | "desc" |"
} else if (type != $1) {
printf("Conflicting types (%s, %s) of UniRec field (%s)\n", type, $1, name) > "/dev/stderr";
exit 1;
}
}
END {
print ""
}' > "$NEW_FILE"
sed -i "/^\# List of UniRec fields\s*$/r $NEW_FILE
/^\# List of UniRec fields\s*$/,/^$/d;" unirec_fields.md
rm "$EXISTING_FILE" "$NEW_FILE" 2> /dev/null
exit 0;