forked from pingcap/kvproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_rust.sh
executable file
·54 lines (43 loc) · 1.31 KB
/
generate_rust.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
#!/bin/bash
. ./common.sh
if ! check_protoc_version; then
exit 1
fi
cargo_install protobuf-codegen 2.0.4
cargo_install grpcio-compiler 0.4.0
echo "generate rust code..."
KVPROTO_ROOT=`pwd`
push proto
GOGO_ROOT=${KVPROTO_ROOT}/vendor/github.com/gogo/protobuf
protoc -I.:${GOGO_ROOT}:../include --rust_out ../src *.proto || exit $?
protoc -I.:${GOGO_ROOT}:../include --grpc_out ../src --plugin=protoc-gen-grpc=`which grpc_rust_plugin` *.proto || exit $?
pop
push src
LIB_RS=`mktemp`
rm -f lib.rs
cat <<EOF > ${LIB_RS}
extern crate futures;
extern crate grpcio;
extern crate protobuf;
extern crate raft;
use raft::eraftpb;
EOF
for file in `ls *.rs`
do
base_name=$(basename $file ".rs")
echo "pub mod $base_name;" >> ${LIB_RS}
done
mv ${LIB_RS} lib.rs
pop
# Use the old way to read protobuf enums.
# TODO: Remove this once stepancheg/rust-protobuf#233 is resolved.
for f in src/*; do
python <<EOF
import re
with open("$f") as reader:
src = reader.read()
res = re.sub('::protobuf::rt::read_proto3_enum_with_unknown_fields_into\(([^,]+), ([^,]+), &mut ([^,]+), [^\)]+\)\?', 'if \\\\1 == ::protobuf::wire_format::WireTypeVarint {\\\\3 = \\\\2.read_enum()?;} else { return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type)); }', src)
with open("$f", "w") as writer:
writer.write(res)
EOF
done