This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.sh
executable file
·96 lines (90 loc) · 2.77 KB
/
build.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
#!/bin/bash
set -o errexit -o pipefail
# check parameters and print usage if need
usage() {
echo "protoc.sh ACTION"
echo "ACTION: "
echo " build build proto to go files."
echo " clean clean result files of protocol building."
exit 1
}
if [ -z "$1" ]; then
usage
fi
PACKAGE_PATH=$(sed -n '/^module \(.*\)/p' go.mod)
PACKAGE_PATH=${PACKAGE_PATH#"module "}
# build protocol
build_protocol() {
BASE_PATH="../proto"
GEN_ALL_IMPORTS="true"
if [ -n "${MODULE_PATH}" ]; then
BASE_PATH="${BASE_PATH}/${MODULE_PATH}"
echo "base path: $BASE_PATH"
unset GEN_ALL_IMPORTS
fi
if [ -n "$GEN_ALL_IMPORTS" ]; then
echo "generate all.go"
echo "// Code generated by build.sh . DO NOT EDIT."> all.go
echo "" >> all.go
echo "package proto" >> all.go
echo "import (" >> all.go
fi
MODULES=$(find "${BASE_PATH}" -type d);
for path in ${MODULES}; do
HAS_PROTO_FILE=$(eval echo $(bash -c "find "${path}" -maxdepth 1 -name *.proto 2>/dev/null" | wc -l));
if [ ${HAS_PROTO_FILE} -gt 0 ]; then
if [ -z "$(echo ${path#../proto})" ]; then
continue; # skip ../proto
fi
MODULE_PATH=${path#../proto/};
echo "build module ${MODULE_PATH}";
mkdir -p ${MODULE_PATH}/pb;
mkdir -p ${MODULE_PATH}/client;
gohub protoc protocol \
--include=../proto \
--msg_out="${MODULE_PATH}/pb" \
--service_out="${MODULE_PATH}/pb" \
--client_out="${MODULE_PATH}/client" \
--validate=true \
--json=true \
--json_opt=emit_defaults=true \
--json_opt=allow_unknown_fields=true \
${path}/*.proto
if [ -n "$GEN_ALL_IMPORTS" ]; then
echo "_ \"${PACKAGE_PATH}/${MODULE_PATH}/pb\"" >> all.go
fi
fi;
done;
if [ -n "$GEN_ALL_IMPORTS" ]; then
echo ")" >> all.go
gofmt -w -l all.go
goimports -w -l all.go
fi
echo "";
echo "build all proto successfully !";
}
# clean result files of building
clean_result() {
MODULES=$(find "../proto" -type d);
for path in ${MODULES}; do
HAS_PROTO_FILE=$(eval echo $(bash -c "find "${path}" -maxdepth 1 -name *.proto 2>/dev/null" | wc -l));
if [ ${HAS_PROTO_FILE} -gt 0 ]; then
if [ -z "$(echo ${path#../proto})" ]; then
continue; # skip ../proto
fi
MODULE_PATH=${path#../proto/};
rm -rf ${MODULE_PATH}
fi;
done;
rm -rf all.go
}
case "$1" in
"build")
build_protocol
;;
"clean")
clean_result
;;
*)
usage
esac