-
Notifications
You must be signed in to change notification settings - Fork 0
/
genfiles.sh
executable file
·67 lines (56 loc) · 1.48 KB
/
genfiles.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
#!/usr/bin/env bash
#
# Generate files.js, which will import the JSON AST format of all found
# Lithp AST's.
#
# Additional paths can be supplied.
FIND_OPTS="-L"
EXT_IN=${EXT_IN:=ast}
EXT_OUT=${EXT_OUT:=json}
REL=${REL:=n}
OUTFILE=${OUTFILE:=files.js}
pushd node_modules/lithp > /dev/null
if [ ! -e run ]; then
ln -s run.js run
fi
# Compile in compact mode
make RUNFLAGS=-cc
popd > /dev/null
content=$(cat << EOF
// $OUTFILE, generated from genfiles.sh\nvar files = {};
EOF
)
getContents () {
path=$1
prefix=`echo $path | sed 's/^..\///g'`
shouldpop=0
if [ "$path"x != "x" ]; then
pushd $path > /dev/null
shouldpop=1
prefix=`echo $prefix | sed 's/^\(\.\.\/\)\+//g'`/
path=./`echo $path | sed 's/^\(\.\.\/\)\+//g'`/
fi
echo "Packaging `find $FIND_OPTS . -name "*.$EXT_IN" 2> /dev/null | wc -l` files in `readlink -e .`"
files=`find $FIND_OPTS . -name "*.$EXT_IN" 2> /dev/null`
for file in $files; do
asJson=`echo $file | sed "s/\.$EXT_IN$/.$EXT_OUT/g"`
if [ "$file" != "$asJson" ]; then
cp $file $asJson
fi
proper=`echo $asJson | sed 's/^..node_modules.//g' | sed 's/^\.\///g'`
properAst=`echo $proper | sed "s/\.$EXT_OUT$/.$EXT_IN/g" | sed 's/^lithp\///g'`
if [ "$REL" == "y" ]; then
proper=./$proper
fi
content+="\nfiles['$prefix$properAst'] = require('$path$proper');"
done
if [ "$shouldpop" -eq 1 ]; then
popd > /dev/null
fi
}
getContents ""
for extra in $@; do
getContents $extra
done
content+="\nmodule.exports = files;\n"
echo -e $content > $OUTFILE