-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen-readme.sh
executable file
·242 lines (230 loc) · 8.48 KB
/
gen-readme.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env bash
set -o nounset
shopt -s extglob # for var=${var##*( )}
# Look away! This is a super-cheesy bash script to generate README.md files
# from the (hopefully one) comment delimited by $flag in the (hopefully
# single) such diderot program per example directory. Normal people would use
# a proper text-processing DSL for this task.
# This is the special flag, within the the top comment, that signifies
# that this is supposed to be put into a markdown file.
flag='=========================================='
function usage {
echo ""
echo "usage: gen-readme.sh [-t] [ex1 ex2 ...]"
echo ""
echo "Generate README.md from Diderot source, for given example subdirectories"
echo "(if given), else for all subdirectories. With the -t option, also"
echo "generates a per-subdirectory .test.sh script for testing."
exit 1
}
gentest=0
while getopts "t" opt; do
case $opt in
t)
gentest=1
;;
\?)
usage
;;
:)
echo "$0: Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
given=0
if [[ "$@" ]]; then
examples="$@"
given=1
else
examples=$(ls -1F | grep ".*/$" | cut -d/ -f 1)
fi
# see documentation in runtests.py
tab=" ";
tabDoTest=" #!$";
tabCompile=" #=diderotc";
tabErrorOk=" #\|\|:";
outFileTol="#>"; # HEY sync with runtests.py
tabForReadBlock=" #R";
tabForTestBlock=" #T";
forTestLine="#_";
forReadLine="#\^";
tabIgnoreOutDotTxt=" #I"; # HEY sync with runtests.py
tmpProgName="#tmp"; # HEY sync with runtests.py
progName="#prog"; # HEY sync with runtests.py
for exdir in $examples; do
echo $0: $exdir ...
ddros=$(ls -1 $exdir/*.diderot 2> /dev/null)
got=""
gotnum=0
for ddr in $ddros; do
if grep -q -F $flag $ddr; then
got=$ddr
((gotnum++))
fi
done
if [ $gotnum -eq 0 ]; then
if [[ $given -eq 1 ]]; then
echo "$0: directory \"$exdir\" had no .diderot files"
fi
continue
fi
if [ $gotnum -gt 1 ]; then
echo "$0: HEY only using last program $got of $gotnum with Markdown delimiter $flag"
fi
progddro=$(basename $got)
prog=$(basename $progddro .diderot)
README=$exdir/README.md
rm -f $README ||:
touch $README
if [[ $gentest -eq 1 ]]; then
TEST=$exdir/.test.sh
rm -f $TEST ||:
touch $TEST # may rm later
fi
printing=0
intabs=0
dotest=0
testing=0
errorok=0
haveDiderotc=0
echo " ... processing $got to create $README"
saveIFS="$IFS"
IFS='' # to preserve whitespace when read'ing lines below
while read -r line; do # reads Diderot source lines from $got
if [[ "$line" =~ $flag ]]; then
# toggle whether we print
printing=$((1 - $printing))
fi
# but don't print line that triggered toggling
if [[ $printing -eq 1 && ! "$line" =~ $flag ]]; then
nfread=0
nftest=0
nfe=0
if [[ $line =~ \<!--.*--\> ]]; then nfe=1; fi # only good for single-line HTML comments
if [[ ! $line =~ ^$tab ]]; then # line didn't start with tab
intabs=0
testing=0
reading=1
else # line did start with tab; may be for test script generation
if [[ $gentest -eq 1 && $intabs -eq 0 && ! $line =~ ^$tabForReadBlock ]]; then
echo "" >> $TEST # separate script command blocks with a blank line
testing=1
fi
if [[ $intabs -eq 0 && $line =~ ^$tabForTestBlock ]]; then
reading=0
fi
intabs=1
if [[ $line =~ ^$tab$outFileTol ]]; then nfread=1; fi
if [[ $line =~ ^$tabCompile ]]; then nfread=1; fi
if [[ $line =~ ^$tab$forTestLine ]]; then nfread=1; fi
if [[ $line =~ ^$tab$forReadLine ]]; then nftest=1; fi
if [[ $line =~ ^$tabDoTest ]]; then dotest=1; nfe=1; fi
if [[ $line =~ ^$tabIgnoreOutDotTxt ]]; then nfread=1; fi
if [[ $line =~ ^$tab$tmpProgName ]]; then nfread=1; fi
if [[ $line =~ ^$tab$progName ]]; then nfread=1; fi
if [[ $line =~ ^$tabForReadBlock ]]; then nfe=1; fi
if [[ $line =~ ^$tabForTestBlock ]]; then nfe=1; fi
if [[ $line =~ ^$tabErrorOk ]]; then nfe=1; errorok=1; fi
if [[ $testing -eq 1 ]]; then
totest=${line#$tab}
if [[ $line =~ ^$tabCompile ]]; then
totest="diderotc --exec $progddro
$progName $progddro"
elif [[ $totest =~ ^$forTestLine ]]; then
totest=${totest#$forTestLine}
fi
if [[ $line =~ ^$tab$outFileTol ]]; then # error checking
# HEY add check that the same outFile isn't used twice
filetol=${totest##$outFileTol*( )}
filetol=${filetol%%*( )}
file=$(echo $filetol | cut -d' ' -f 1) # alas, IFS hacking
tol=$(echo $filetol | cut -d' ' -f 2)
foo=$(echo $filetol | cut -d' ' -f 3)
if [[ ! $filetol =~ " " || -z $tol || ! -z $foo ]]; then
echo "$0: in $got, outFileTol line \"$line\" not of form \"\t$outFileTol OUT EPS\"" >&2
exit 1
fi
fi
if [[ $line =~ ^$tab$tmpProgName ]]; then # error checking
# HEY add check that the same progName isn't used twice
prog=${totest##$tmpProgName*( )}
prog=${prog%%*( )}
if [[ -z $prog ]]; then
echo "$0: in $got, tmpProgName line \"$line\" not of form \"\t$tmpProgName OUT\"" >&2
exit 1
fi
fi
if [[ $line =~ ^$tab$progName ]]; then # error checking; HEY copy+paste from above
# HEY add check that the same progName isn't used twice
prog=${totest##$progName*( )}
prog=${prog%%*( )}
if [[ -z $prog ]]; then
echo "$0: in $got, progName line \"$line\" not of form \"\t$progName OUT\"" >&2
exit 1
fi
fi
if [[ $nftest -eq 0 && $nfe -eq 0 ]]; then
totest=${totest# }
if [[ $errorok -eq 1 ]]; then
totest="$totest ||:"
errorok=0
fi
# add prefix PRFX to anything starting with "./"
totest=$(echo "$totest" | sed 's|^\( *\)./|\1$PRFX ./|g')
# add diderotc flags (may also modify comments)
totest=$(echo "$totest" | sed 's|diderotc|diderotc $DFLG|g')
echo $totest >> $TEST
fi
fi
fi
if [[ $reading -eq 1 && $nfread -eq 0 && $nfe -eq 0 ]]; then
if [[ $line =~ ^$tab$forReadLine ]]; then
line=${line##$tab$forReadLine*( )}
echo "$tab$line" >> $README
else
echo $line >> $README
fi
fi
fi
done <<< $(cat $got) # reading Diderot source lines
IFS="$saveIFS"
if [[ $gentest -eq 0 ]]; then
continue;
fi
if [[ ! -s $TEST ]]; then
# didn't end up saving anything here
rm -f $TEST
elif [[ $dotest -eq 0 ]]; then
rm -f $TEST
else
# add pre-amble to script
echo "#!/bin/bash
JUNK=\"\"
function junk { JUNK=\"\$JUNK \$@\"; }
function cleanup { rm -rf \$JUNK; }
trap cleanup err exit int term
set -o errexit
set -o nounset
shopt -s expand_aliases
DFLG=
if [ ! -z \${DDRO_FLAG+x} ]; then
DFLG=\"\$DDRO_FLAG\"
fi
if [ ! -z \${DDRO_TARG+x} ]; then
if [ \$DDRO_TARG == noop ]; then
alias diderotc=:
elif [ \$DDRO_TARG == pthread ]; then
DFLG=\"\$DFLG --target=pthread\"
fi
fi
PRFX=
if [ ! -z \${DDRO_PRFX+x} ]; then
PRFX=\$DDRO_PRFX
fi
$(cat $TEST)" > $TEST
chmod 755 $TEST
echo " ... also created $TEST"
fi
done