-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanakin
151 lines (119 loc) · 3.74 KB
/
anakin
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/sh
readonly GO_MIN=1.5
readonly GO_VERSION="$(go version | cut -c14-16)"
readonly OS_VERSION="$(uname -s)"
readonly CUR_DIR=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
readonly XCODE_PATH="/Applications/Xcode.app/Contents/Developer"
readonly XCCLT_PATH="/Library/Developer/CommandLineTools"
function error_exit {
echo "Error: $1" 1>&2
exit 1
}
### CHECK ENVIRONMENT
# check Go version
if [[ $(bc <<< "$GO_VERSION < $GO_MIN") -eq 1 ]]; then
error_exit "Please, update Go: needed at least $GO_MIN, installed $GO_VERSION."
fi
# for Mac OS X install Xcode Command Line Tools if needed
if [[ "$OS_VERSION" == "Darwin" ]] && [[ "$(xcode-select -p)" != "$XCODE_PATH" ]]; then
echo "Xcode Command Line Tools not found, install..."
xcode-select --install
if [ "$(xcode-select -p)" != "$XCCLT_PATH" ] && [ "$(xcode-select -p)" != "$XCODE_PATH" ]; then
error_exit "Failed install Xcode Command Line Tools."
fi
fi
# set GOPATH if it's not already set
if [[ -z ${GOPATH+x} ]]; then
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
fi
echo "Get necessary Go packages..."
go get google.golang.org/grpc
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get golang.org/x/mobile/cmd/gomobile
### READ PARAMS
function usage {
echo "Usage: $0 -P <proto> -O <output> -h <host> -p <port> -android -ios"
}
PROTO=
OUTPUT=$CUR_DIR/gen
HOST=localhost
PORT=50051
ANDROID=0
IOS=0
while [[ $# -gt 0 ]]; do
case "$1" in
-P | --proto) PROTO="$2"; shift;;
-O | --output) OUTPUT="$2"; shift;;
-h | --host) HOST="$2"; shift;;
-p | --port) PORT="$2"; shift;;
-android) ANDROID=1;;
-ios) IOS=1;;
-*) usage; exit 1;;
*) ;; # terminate while loop
esac
shift
done
if [[ ! -f "$PROTO" ]]; then
error_exit "Proto-file $PROTO not found."
fi
### GENERATE GRPC RELATED STUFF
PROTO_URL="https://github.com/google/protobuf/releases/latest"
PROTOC_BUILD=
if [[ "$OS_VERSION" == "Darwin" ]]; then # Mac OS X
PROTOC_BUILD="osx-$(uname -m)"
elif [[ "$OS_VERSION" == "Linux" ]]; then # Linux
PROTOC_BUILD="linux-$(uname -m)"
elif [[ "$OS_VERSION" == CYGWIN* ]] || # Windows
[[ "$OS_VERSION" == MINGW32* ]] ||
[[ "$OS_VERSION" == MSYS* ]]
then
PROTOC_BUILD="win32"
else
error_exit "
Cannot find necessary protoc utility for your OS type.
Please check: $PROTO_URL
If latest release has, please open issue on tool repo page:
https://github.com/Softwee/Anakin
"
fi
PROTOC_VERSION="$(curl $PROTO_URL | cut -c85-89)"
PROTOC_URL="https://github.com/google/protobuf/releases/download/v$PROTOC_VERSION/"
PROTOC_ZIP="protoc-$PROTOC_VERSION-$PROTOC_BUILD.zip"
echo "Download protoc utility..."
curl -O -J -L $PROTOC_URL$PROTOC_ZIP
sudo apt-get install unzip
unzip $PROTOC_ZIP -d protoc
rm $PROTOC_ZIP
GEN_SERVER=$OUTPUT/server
GEN_CLIENT=$OUTPUT/client
GEN_RPC=$OUTPUT/rpc
GEN_RPC_PROTO=$GEN_RPC/rpc.proto
echo "Generate code for gRPC..."
mkdir -p $GEN_SERVER $GEN_CLIENT $GEN_RPC
cp $PROTO $GEN_RPC_PROTO
./protoc/bin/protoc -I $GEN_RPC $GEN_RPC_PROTO --go_out=plugins=grpc:$GEN_RPC
rm -rf protoc
### GENERATE SERVER & CLIENT CODE
echo "Generate server & client code..."
go run src/gen.go $GEN_RPC_PROTO $HOST $PORT
mv server.go $GEN_SERVER/server.go
mv client.go $GEN_CLIENT/client.go
### CREATE CLIENT BINARIES
function build_binaries {
sh $GEN_CLIENT/anakin-build --android $ANDROID --ios $IOS
}
cp src/anakin-build $GEN_CLIENT/anakin-build
if [[ "$ANDROID" -eq 0 ]] && [[ "$IOS" -eq 0 ]]; then
echo "
Done! Server & client code are ready in output directory:
$OUTPUT
Binaries build ignored, you can do this later as follows:
sh anakin-build [--android 1] [--ios 1]
from $GEN_CLIENT directory.
"
exit 0 # no build needed ¯\_(ツ)_/¯
fi
( cd $GEN_CLIENT && build_binaries )
### CLIENT BINARIERS ARE READY
exit 0