/
utilities.sh
executable file
·367 lines (334 loc) · 12 KB
/
utilities.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env bash
set -xeo pipefail
# private functions that should not be called outside this file.
# Used to obtain configuration values from a bazel BUILD file
#
# inspects a $build_file for a certain $rule (e.g. java_gapic_library). If the
# first 15 lines after the declaration of the rule contain $pattern, then
# it will return $if_match if $pattern is found, otherwise $default
__get_config_from_BUILD() {
build_file=$1
rule=$2
pattern=$3
default=$4
if_match=$5
result="${default}"
if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then
result="${if_match}"
fi
echo "${result}"
}
__get_iam_policy_from_BUILD() {
local build_file=$1
local contains_iam_policy
contains_iam_policy=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/iam/v1:iam_policy_proto" \
"false" \
"true"
)
echo "${contains_iam_policy}"
}
__get_locations_from_BUILD() {
local build_file=$1
local contains_locations
contains_locations=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/cloud/location:location_proto" \
"false" \
"true"
)
echo "${contains_locations}"
}
# define utility functions
extract_folder_name() {
local destination_path=$1
local folder_name=${destination_path##*/}
echo "$folder_name"
}
remove_empty_files() {
local category=$1
find "${destination_path}/${category}-${folder_name}/src/main/java" -type f -size 0 | while read -r f; do rm -f "${f}"; done
if [ -d "${destination_path}/${category}-${folder_name}/src/main/java/samples" ]; then
mv "${destination_path}/${category}-${folder_name}/src/main/java/samples" "${destination_path}/${category}-${folder_name}"
fi
}
# Move generated files to folders in destination_path.
mv_src_files() {
local category=$1 # one of gapic, proto, samples
local type=$2 # one of main, test
if [ "${category}" == "samples" ]; then
src_suffix="samples/snippets/generated/src/main/java/com"
folder_suffix="samples/snippets/generated"
elif [ "${category}" == "proto" ]; then
src_suffix="${category}/src/${type}/java"
folder_suffix="${category}-${folder_name}/src/${type}"
else
src_suffix="src/${type}"
folder_suffix="${category}-${folder_name}/src"
fi
mkdir -p "${destination_path}/${folder_suffix}"
cp -r "${destination_path}/java_gapic_srcjar/${src_suffix}" "${destination_path}/${folder_suffix}"
if [ "${category}" != "samples" ]; then
rm -r -f "${destination_path}/${folder_suffix}/java/META-INF"
fi
}
# unzip jar file
unzip_src_files() {
local category=$1
local jar_file=java_${category}.jar
mkdir -p "${destination_path}/${category}-${folder_name}/src/main/java"
unzip -q -o "${destination_path}/${jar_file}" -d "${destination_path}/${category}-${folder_name}/src/main/java"
rm -r -f "${destination_path}/${category}-${folder_name}/src/main/java/META-INF"
}
# get gapic options from .yaml and .json files from proto_path.
get_gapic_opts() {
local gapic_config
local grpc_service_config
local api_service_config
gapic_config=$(find "${proto_path}" -type f -name "*gapic.yaml")
if [ -z "${gapic_config}" ]; then
gapic_config=""
else
gapic_config="gapic-config=${gapic_config},"
fi
grpc_service_config=$(find "${proto_path}" -type f -name "*service_config.json")
api_service_config=$(find "${proto_path}" -maxdepth 1 -type f \( -name "*.yaml" ! -name "*gapic.yaml" \))
if [ "${rest_numeric_enums}" == "true" ]; then
rest_numeric_enums="rest-numeric-enums,"
else
rest_numeric_enums=""
fi
echo "transport=${transport},${rest_numeric_enums}grpc-service-config=${grpc_service_config},${gapic_config}api-service-config=${api_service_config}"
}
remove_grpc_version() {
find "${destination_path}" -type f -name "*Grpc.java" -exec \
sed -i.bak 's/value = \"by gRPC proto compiler.*/value = \"by gRPC proto compiler\",/g' {} \; -exec rm {}.bak \;
}
download_gapic_generator_pom_parent() {
local gapic_generator_version=$1
if [ ! -f "gapic-generator-java-pom-parent-${gapic_generator_version}.pom" ]; then
if [[ "${gapic_generator_version}" == *"-SNAPSHOT" ]]; then
# copy a SNAPSHOT version from maven local repository.
copy_from "$HOME/.m2/repository/com/google/api/gapic-generator-java-pom-parent/${gapic_generator_version}/gapic-generator-java-pom-parent-${gapic_generator_version}.pom" \
"gapic-generator-java-pom-parent-${gapic_generator_version}.pom"
return
fi
# download gapic-generator-java-pom-parent from Google maven central mirror.
download_from \
"https://maven-central.storage-download.googleapis.com/maven2/com/google/api/gapic-generator-java-pom-parent/${gapic_generator_version}/gapic-generator-java-pom-parent-${gapic_generator_version}.pom" \
"gapic-generator-java-pom-parent-${gapic_generator_version}.pom"
fi
# file exists, do not need to download again.
}
get_grpc_version() {
local gapic_generator_version=$1
local grpc_version
# get grpc version from gapic-generator-java-pom-parent/pom.xml
download_gapic_generator_pom_parent "${gapic_generator_version}"
grpc_version=$(grep grpc.version "gapic-generator-java-pom-parent-${gapic_generator_version}.pom" | sed 's/<grpc\.version>\(.*\)<\/grpc\.version>/\1/' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "$grpc_version"
}
get_protobuf_version() {
local gapic_generator_version=$1
local protobuf_version
# get protobuf version from gapic-generator-java-pom-parent/pom.xml
download_gapic_generator_pom_parent "${gapic_generator_version}"
protobuf_version=$(grep protobuf.version "gapic-generator-java-pom-parent-${gapic_generator_version}.pom" | sed 's/<protobuf\.version>\(.*\)<\/protobuf\.version>/\1/' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | cut -d "." -f2-)
echo "${protobuf_version}"
}
download_tools() {
pushd "${output_folder}"
local gapic_generator_version=$1
local protobuf_version=$2
local grpc_version=$3
local os_architecture=$4
download_generator "${gapic_generator_version}"
download_protobuf "${protobuf_version}" "${os_architecture}"
download_grpc_plugin "${grpc_version}" "${os_architecture}"
popd
}
download_generator() {
local gapic_generator_version=$1
if [ ! -f "gapic-generator-java-${gapic_generator_version}.jar" ]; then
if [[ "${gapic_generator_version}" == *"-SNAPSHOT" ]]; then
# copy a SNAPSHOT version from maven local repository.
copy_from "$HOME/.m2/repository/com/google/api/gapic-generator-java/${gapic_generator_version}/gapic-generator-java-${gapic_generator_version}.jar" \
"gapic-generator-java-${gapic_generator_version}.jar"
return
fi
# download gapic-generator-java from Google maven central mirror.
download_from \
"https://maven-central.storage-download.googleapis.com/maven2/com/google/api/gapic-generator-java/${gapic_generator_version}/gapic-generator-java-${gapic_generator_version}.jar" \
"gapic-generator-java-${gapic_generator_version}.jar"
fi
}
download_protobuf() {
local protobuf_version=$1
local os_architecture=$2
if [ ! -d "protobuf-${protobuf_version}" ]; then
# pull proto files and protoc from protobuf repository as maven central
# doesn't have proto files
download_from \
"https://github.com/protocolbuffers/protobuf/releases/download/v${protobuf_version}/protoc-${protobuf_version}-${os_architecture}.zip" \
"protobuf-${protobuf_version}.zip" \
"GitHub"
unzip -o -q "protobuf-${protobuf_version}.zip" -d "protobuf-${protobuf_version}"
cp -r "protobuf-${protobuf_version}/include/google" .
rm "protobuf-${protobuf_version}.zip"
fi
protoc_path="${output_folder}/protobuf-${protobuf_version}/bin"
}
download_grpc_plugin() {
local grpc_version=$1
local os_architecture=$2
if [ ! -f "protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe" ]; then
# download protoc-gen-grpc-java plugin from Google maven central mirror.
download_from \
"https://maven-central.storage-download.googleapis.com/maven2/io/grpc/protoc-gen-grpc-java/${grpc_version}/protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe" \
"protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe"
chmod +x "protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe"
fi
}
download_from() {
local url=$1
local save_as=$2
local repo=$3
# fail-fast, 30 seconds at most, retry 2 times
curl -LJ -o "${save_as}" --fail -m 30 --retry 2 "$url" || download_fail "${save_as}" "${repo}"
}
copy_from() {
local local_repo=$1
local save_as=$2
cp "${local_repo}" "${save_as}" || \
download_fail "${save_as}" "maven local"
}
download_fail() {
local artifact=$1
local repo=${2:-"maven central mirror"}
>&2 echo "Fail to download ${artifact} from ${repo} repository. Please install ${artifact} first if you want to download a SNAPSHOT."
exit 1
}
# Obtains a version from a bazel WORKSPACE file
#
# versions look like "_ggj_version="1.2.3"
# It will return 1.2.3 for such example
get_version_from_WORKSPACE() {
version_key_word=$1
workspace=$2
version=$(\
grep "${version_key_word}" "${workspace}" |\
head -n 1 |\
sed 's/\(.*\) = "\(.*\)"\(.*\)/\2/' |\
sed 's/[a-zA-Z-]*//'
)
echo "${version}"
}
# Apart from proto files in proto_path, additional protos are needed in order
# to generate GAPIC client libraries.
# In most cases, these protos should be within google/ directory, which is
# pulled from googleapis as a prerequisite.
# Get additional protos in BUILD.bazel.
get_gapic_additional_protos_from_BUILD() {
local build_file=$1
local gapic_additional_protos="google/cloud/common_resources.proto"
if [[ $(__get_iam_policy_from_BUILD "${build_file}") == "true" ]]; then
gapic_additional_protos="${gapic_additional_protos} google/iam/v1/iam_policy.proto"
fi
if [[ $(__get_locations_from_BUILD "${build_file}") == "true" ]]; then
gapic_additional_protos="${gapic_additional_protos} google/cloud/location/locations.proto"
fi
echo "${gapic_additional_protos}"
}
get_transport_from_BUILD() {
local build_file=$1
local transport
transport=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"grpc+rest" \
"grpc" \
"grpc+rest"
)
# search again because the transport maybe `rest`.
transport=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"transport = \"rest\"" \
"${transport}" \
"rest"
)
echo "${transport}"
}
get_rest_numeric_enums_from_BUILD() {
local build_file=$1
local rest_numeric_enums
rest_numeric_enums=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"rest_numeric_enums = True" \
"false" \
"true"
)
echo "${rest_numeric_enums}"
}
get_include_samples_from_BUILD() {
local build_file=$1
local include_samples
include_samples=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_assembly_gradle_pkg(" \
"include_samples = True" \
"false" \
"true"
)
echo "${include_samples}"
}
# Convenience function to clone only the necessary folders from a git repository
sparse_clone() {
repo_url=$1
paths=$2
commitish=$3
clone_dir=$(basename "${repo_url%.*}")
rm -rf "${clone_dir}"
git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}"
pushd "${clone_dir}"
if [ -n "${commitish}" ]; then
git checkout "${commitish}"
fi
git sparse-checkout set --no-cone ${paths}
git checkout
popd
}
# takes a versions.txt file and returns its version
get_version_from_versions_txt() {
versions=$1
key=$2
version=$(grep "$key:" "${versions}" | cut -d: -f3) # 3rd field is snapshot
echo "${version}"
}
# gets the output folder where all sources and dependencies will be located. It
# relies on utilities_script_dir which points to the same location as
# `generate_library.sh`
get_output_folder() {
echo "$(pwd)/output"
}
detect_os_architecture() {
local os_type
local os_architecture
os_type=$(uname -sm)
case "${os_type}" in
*"Linux x86_64"*)
os_architecture="linux-x86_64"
;;
*"Darwin x86_64"*)
os_architecture="osx-x86_64"
;;
*)
os_architecture="osx-aarch_64"
;;
esac
echo "${os_architecture}"
}