-
Notifications
You must be signed in to change notification settings - Fork 12
/
proto.sh
executable file
·52 lines (43 loc) · 1.18 KB
/
proto.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
#!/bin/bash
set -e
set -o pipefail
# Outputs proto descriptors to proto.js
# Note that this requires modification to protobuf.js:
# all of the regexes must support $ in proto names.
# Maybe we should just use @export or externs here.
node_modules/protobufjs/bin/pbjs src/grid.proto \
--target json --quiet \
> dist/proto.json
OUTFILE=dist/proto.js
cat <<EOF > $OUTFILE
goog.provide('windmill.GridProto');
goog.require('goog.object');
goog.scope(function() {
/** @enum {number} */
var Fields = {
EOF
# Mapping from name to number
cat dist/proto.json \
| grep name \
| grep -o '[^"]*",' \
| sed -e 's/..$//' \
| sort \
| uniq \
| awk '{print " " $0 ": " NR "," }' \
>> $OUTFILE
# Reverse mapping
cat <<EOF >> $OUTFILE
};
/** @const {map<string, string>} */
var FromFields = goog.object.transpose(Fields);
var proto =
EOF
# JSON with field name derived from reverse mapping
cat dist/proto.json \
| sed -e 's/\(.*\)"name": "\(.*\)"/\1"name": FromFields[Fields.\2]/' \
-e 's/\(.*\)"type": "\([A-Z].*\)"/\1"type": FromFields[Fields.\2]/' \
>> $OUTFILE
cat <<EOF >> $OUTFILE
windmill.GridProto = dcodeIO.ProtoBuf.newBuilder({})['import'](proto).build()['GridProto'];
});
EOF