forked from dapr/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobuf.sh
executable file
·120 lines (92 loc) · 2.91 KB
/
protobuf.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
#!/bin/bash
# ------------------------------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------------------------------
# Tool chain installation
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
npm install grpc-tools --save-dev
npm install grpc_tools_node_protoc_ts --save-dev
# Proto buf generation
APPCALLBACK="appcallback"
COMMON="common"
DAPR="dapr"
RUNTIME="runtime"
# Path to store output
PROTO_PATH="src/dapr/proto"
SRC=src
# Http request CLI
HTTP_REQUEST_CLI=curl
checkHttpRequestCLI() {
if type "curl" > /dev/null; then
HTTP_REQUEST_CLI=curl
elif type "wget" > /dev/null; then
HTTP_REQUEST_CLI=wget
else
echo "Either curl or wget is required"
exit 1
fi
}
downloadFile() {
FOLDER_NAME=$1
FILE_NAME=$2
FILE_PATH="${PROTO_PATH}/${FOLDER_NAME}/v1"
# URL for proto file
PROTO_URL="https://raw.githubusercontent.com/dapr/dapr/master/dapr/proto/${FOLDER_NAME}/v1/${FILE_NAME}.proto"
mkdir -p "${FILE_PATH}"
echo "Downloading $PROTO_URL ..."
if [ "$HTTP_REQUEST_CLI" == "curl" ]; then
cd ${FILE_PATH}
curl -SsL "$PROTO_URL" -o "${FILE_NAME}.proto"
cd ../../../../..
else
wget -q -P "$PROTO_URL" "${FILE_PATH}/${FILE_NAME}.proto"
fi
if [ ! -e "${FILE_PATH}/${FILE_NAME}.proto" ]; then
echo "failed to download $PROTO_URL ..."
ret_val=$FILE_NAME
exit 1
fi
}
generateGrpc() {
FOLDER_NAME=$1
FILE_NAME=$2
FILE_PATH="${PROTO_PATH}/${FOLDER_NAME}/v1"
node_modules/grpc-tools/bin/protoc -I=$SRC --js_out=import_style=commonjs,binary:$SRC --grpc_out=$SRC --plugin=protoc-gen-grpc=node_modules/grpc-tools/bin/grpc_node_plugin ${FILE_PATH}/${FILE_NAME}.proto
node_modules/grpc-tools/bin/protoc -I=$SRC --ts_out=$SRC --plugin=protoc-gen-ts=node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts ${FILE_PATH}/${FILE_NAME}.proto
if [ ! -e "${FILE_PATH}/${FILE_NAME}_pb.js" ]; then
echo "failed to generate proto buf $FILE_NAME"
ret_val=$FILE_NAME
exit 1
fi
}
fail_trap() {
result=$?
if [ $result != 0 ]; then
echo "Failed to generate gRPC interface and proto buf: $ret_val"
fi
cleanup
exit $result
}
cleanup() {
find $PROTO_PATH -type f -name '*.proto' -delete
rm -rf protoc
rm -f protoc.zip
}
generateGrpcSuccess() {
echo -e "\ngRPC interface and proto buf generated successfully!"
}
# -----------------------------------------------------------------------------
# main
# -----------------------------------------------------------------------------
trap "fail_trap" EXIT
checkHttpRequestCLI
downloadFile $COMMON $COMMON
generateGrpc $COMMON $COMMON
downloadFile $RUNTIME $DAPR
generateGrpc $RUNTIME $DAPR
downloadFile $RUNTIME $APPCALLBACK
generateGrpc $RUNTIME $APPCALLBACK
cleanup
generateGrpcSuccess