/
utilities.sh
executable file
·293 lines (266 loc) · 10.4 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
#!/usr/bin/env bash
set -xeo pipefail
# 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"
}
find_additional_protos_in_yaml() {
local pattern=$1
local find_result
find_result=$(grep --include=\*.yaml -rw "${proto_path}" -e "${pattern}")
if [ -n "${find_result}" ]; then
echo "${find_result}"
fi
}
# 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.
# Search additional protos in .yaml files.
search_additional_protos() {
additional_protos="google/cloud/common_resources.proto" # used by every library
iam_policy=$(find_additional_protos_in_yaml "name: '*google.iam.v1.IAMPolicy'*")
if [ -n "$iam_policy" ]; then
additional_protos="$additional_protos google/iam/v1/iam_policy.proto"
fi
locations=$(find_additional_protos_in_yaml "name: '*google.cloud.location.Locations'*")
if [ -n "${locations}" ]; then
additional_protos="$additional_protos google/cloud/location/locations.proto"
fi
echo "${additional_protos}"
}
# 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() {
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}"
}
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}.zip" ]; 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=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}"
}
# 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 15 "${rule}" "${build_file}" | grep -q "${pattern}"; then
result="${if_match}"
fi
echo "${result}"
}
# 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}"
cd "${clone_dir}"
if [ -n "${commitish}" ]; then
git checkout "${commitish}"
fi
git sparse-checkout set --no-cone ${paths}
git checkout
cd ..
}
# 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}"
}
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}"
}