/
generate_library.sh
executable file
·161 lines (149 loc) · 5.25 KB
/
generate_library.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
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
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
set -eo pipefail
set -x
# parse input parameters
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-p|--proto_path)
proto_path="$2"
shift
;;
-d|--destination_path)
destination_path="$2"
shift
;;
--gapic_generator_version)
gapic_generator_version="$2"
# export this variable so that it can be used in gapic-generator-java-wrapper.sh
export gapic_generator_version
shift
;;
--protobuf_version)
protobuf_version="$2"
shift
;;
--grpc_version)
grpc_version="$2"
shift
;;
--transport)
transport="$2"
shift
;;
--rest_numeric_enums)
rest_numeric_enums="$2"
shift
;;
--include_samples)
include_samples="$2"
shift
;;
--os_architecture)
os_architecture="$2"
shift
;;
*)
echo "Invalid option: [$1]"
exit 1
;;
esac
shift # past argument or value
done
script_dir=$(dirname "$(readlink -f "$0")")
# source utility functions
source "${script_dir}"/utilities.sh
if [ -z "${protobuf_version}" ]; then
protobuf_version=$(get_protobuf_version "${gapic_generator_version}")
fi
if [ -z "${grpc_version}" ]; then
grpc_version=$(get_grpc_version "${gapic_generator_version}")
fi
if [ -z "${transport}" ]; then
transport="grpc"
fi
if [ -z "${rest_numeric_enums}" ]; then
rest_numeric_enums="true"
fi
if [ -z "${include_samples}" ]; then
include_samples="true"
fi
if [ -z "${os_architecture}" ]; then
os_architecture=$(detect_os_architecture)
fi
mkdir -p "${destination_path}"
##################### Section 0 #####################
# prepare tooling
#####################################################
# the order of services entries in gapic_metadata.json is relevant to the
# order of proto file, sort the proto files with respect to their name to
# get a fixed order.
proto_files=$(find "${proto_path}" -type f -name "*.proto" | sort)
folder_name=$(extract_folder_name "${destination_path}")
# download gapic-generator-java, protobuf and grpc plugin.
download_tools "${gapic_generator_version}" "${protobuf_version}" "${grpc_version}" "${os_architecture}"
##################### Section 1 #####################
# generate grpc-*/
#####################################################
"${protoc_path}"/protoc "--plugin=protoc-gen-rpc-plugin=protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe" \
"--rpc-plugin_out=:${destination_path}/java_grpc.jar" \
${proto_files} # Do not quote because this variable should not be treated as one long string.
# unzip java_grpc.jar to grpc-*/src/main/java
unzip_src_files "grpc"
# remove empty files in grpc-*/src/main/java
remove_empty_files "grpc"
# remove grpc version in *ServiceGrpc.java file so the content is identical with bazel build.
remove_grpc_version
###################### Section 2 #####################
## generate gapic-*/, part of proto-*/, samples/
######################################################
"$protoc_path"/protoc --experimental_allow_proto3_optional \
"--plugin=protoc-gen-java_gapic=${script_dir}/gapic-generator-java-wrapper" \
"--java_gapic_out=metadata:${destination_path}/java_gapic_srcjar_raw.srcjar.zip" \
"--java_gapic_opt=$(get_gapic_opts)" \
${proto_files} $(search_additional_protos)
unzip -o -q "${destination_path}/java_gapic_srcjar_raw.srcjar.zip" -d "${destination_path}"
# Sync'\''d to the output file name in Writer.java.
unzip -o -q "${destination_path}/temp-codegen.srcjar" -d "${destination_path}/java_gapic_srcjar"
# Resource name source files.
proto_dir=${destination_path}/java_gapic_srcjar/proto/src/main/java
if [ ! -d "${proto_dir}" ]; then
# Some APIs don'\''t have resource name helpers, like BigQuery v2.
# Create an empty file so we can finish building. Gating the resource name rule definition
# on file existences go against Bazel'\''s design patterns, so we'\''ll simply delete all empty
# files during the final packaging process (see java_gapic_pkg.bzl)
mkdir -p "${proto_dir}"
touch "${proto_dir}"/PlaceholderFile.java
fi
# move java_gapic_srcjar/src/main to gapic-*/src.
mv_src_files "gapic" "main"
# remove empty files in gapic-*/src/main/java
remove_empty_files "gapic"
# move java_gapic_srcjar/src/test to gapic-*/src
mv_src_files "gapic" "test"
if [ "${include_samples}" == "true" ]; then
# move java_gapic_srcjar/samples/snippets to samples/snippets
mv_src_files "samples" "main"
fi
##################### Section 3 #####################
# generate proto-*/
#####################################################
"$protoc_path"/protoc "--java_out=${destination_path}/java_proto.jar" ${proto_files}
# move java_gapic_srcjar/proto/src/main/java (generated resource name helper class)
# to proto-*/src/main
mv_src_files "proto" "main"
# unzip java_proto.jar to proto-*/src/main/java
unzip_src_files "proto"
# remove empty files in proto-*/src/main/java
remove_empty_files "proto"
# copy proto files to proto-*/src/main/proto
for proto_src in ${proto_files}; do
mkdir -p "${destination_path}/proto-${folder_name}/src/main/proto"
rsync -R "${proto_src}" "${destination_path}/proto-${folder_name}/src/main/proto"
done
##################### Section 4 #####################
# rm tar files
#####################################################
cd "${destination_path}"
rm -rf java_gapic_srcjar java_gapic_srcjar_raw.srcjar.zip java_grpc.jar java_proto.jar temp-codegen.srcjar
set +x