forked from pingcap/kvproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_go.sh
executable file
·68 lines (58 loc) · 1.84 KB
/
generate_go.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
#!/usr/bin/env bash
. ./common.sh
if ! check_protoc_version; then
exit 1
fi
PROGRAM=$(basename "$0")
if [ -z $GOPATH ]; then
printf "Error: the environment variable GOPATH is not set, please set it before running %s\n" $PROGRAM > /dev/stderr
exit 1
fi
GO_PREFIX_PATH=github.com/pingcap/kvproto/pkg
gogo_protobuf_url=github.com/gogo/protobuf
GOGO_ROOT=${GOPATH}/src/${gogo_protobuf_url}
GO_OUT_M=
GO_INSTALL='go install'
echo "install gogoproto code/generator ..."
${GO_INSTALL} ${gogo_protobuf_url}/proto
${GO_INSTALL} ${gogo_protobuf_url}/protoc-gen-gofast
${GO_INSTALL} ${gogo_protobuf_url}/gogoproto
echo "install goimports ..."
${GO_INSTALL} golang.org/x/tools/cmd/goimports
# add the bin path of gogoproto generator into PATH if it's missing
if ! cmd_exists protoc-gen-gofast; then
for path in $(echo "${GOPATH}" | sed -e 's/:/ /g'); do
gogo_proto_bin="${path}/bin/protoc-gen-gofast"
if [ -e "${gogo_proto_bin}" ]; then
export PATH=$(dirname "${gogo_proto_bin}"):$PATH
break
fi
done
fi
cd proto
for file in `ls *.proto`
do
base_name=$(basename $file ".proto")
mkdir -p ../pkg/$base_name
if [ -z $GO_OUT_M ]; then
GO_OUT_M="M$file=$GO_PREFIX_PATH/$base_name"
else
GO_OUT_M="$GO_OUT_M,M$file=$GO_PREFIX_PATH/$base_name"
fi
done
echo "generate go code..."
ret=0
for file in `ls *.proto`
do
base_name=$(basename $file ".proto")
protoc -I.:${GOGO_ROOT}:${GOGO_ROOT}/protobuf --gofast_out=plugins=grpc,$GO_OUT_M:../pkg/$base_name $file || ret=$?
cd ../pkg/$base_name
sed -i.bak -E 's/import _ \"gogoproto\"//g' *.pb.go
sed -i.bak -E 's/import fmt \"fmt\"//g' *.pb.go
sed -i.bak -E 's/import io \"io\"//g' *.pb.go
sed -i.bak -E 's/import math \"math\"//g' *.pb.go
rm -f *.bak
goimports -w *.pb.go
cd ../../proto
done
exit $ret