Skip to content

Commit f5efb0e

Browse files
feat: hermetic build scripts to use a single output/generation folder (#1987)
* move generation tools and generated source to single folder * script fixes * capture exit code from failing script * remove accidental file * wip: fix showcase test * post rebase fixes * adjust showcase failure handling * update readme * fix output path in IT * download googleapis in output folder * Update library_generation/README.md Co-authored-by: Joe Wang <[email protected]> * remove unrelated os detection function * use pushd instead of cd in gen script output cleanup * correct destination_path documentation * Update library_generation/README.md Co-authored-by: Joe Wang <[email protected]> * fix pushd in generate_library.sh * ignore output folder * Update library_generation/README.md Co-authored-by: Joe Wang <[email protected]> * Update library_generation/README.md Co-authored-by: Joe Wang <[email protected]> * spit output to stdout * centralize output folder computation * label popped folders * prevent wrong version unit test from exiting the test suite * resolve output dir using pwd --------- Co-authored-by: Joe Wang <[email protected]>
1 parent 9c0316b commit f5efb0e

9 files changed

+86
-52
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ target/
1515
# Vscode Settings
1616
.vscode/settings.json
1717

18-
*.iml
18+
*.iml
19+
20+
# library generation
21+
output/
22+
library_generation/output/
23+
showcase/scripts/output/

library_generation/README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,38 @@ The script, `generate_library.sh`, allows you to generate a GAPIC client library
77
Use Linux environment and install java runtime environment (8 or above).
88

99
## Prerequisite
10-
Protos referenced by protos in `proto_path` (see `proto_path` below) should be copied to the current
11-
working directory (refers as `$cwd`, a directory contains `generate_library.sh`).
10+
Protos referenced by protos in `proto_path` (see `proto_path` below) should be copied to an `output`
11+
directory located in the working directory, referred as `$cwd`
12+
(for example, `library_generation/output` if planning to call from the same folder).
1213
The directory structure should be the same as import statements in protos.
1314

14-
For example, we want to generate from `folder1/folder2/protoA`, so `proto_path`
15-
should be set to `folder1/folder2` (a relative path from `$cwd`).
15+
For example, you want to generate from `folder1/folder2/protoA`, so `proto_path`
16+
should be set to `folder1/folder2` (a relative path from `output`).
1617
protoA imports protoB as `folder3/folder4/protoB`, then there should
17-
be `folder3/folder4` (containing protoB) in `$cwd`.
18+
be `folder3/folder4` (containing protoB) in `output`.
1819

1920
In order to generate a GAPIC library, you need to pull `google/` from [googleapis](https://github.com/googleapis/googleapis)
20-
and put it into `$cwd` since protos in `google/` are likely referenced by
21+
and put it into `output` since protos in `google/` are likely referenced by
2122
protos from which the library are generated.
2223

2324
## Parameters to run `generate_library.sh`
2425

2526
You need to run the script with the following parameters.
2627

2728
### proto_path
28-
A directory in `$cwd` and copy proto files into it.
29-
The absolute path of `proto_path` is `$cwd/$proto_path`.
29+
A directory in `$cwd/output` and copy proto files into it.
30+
The absolute path of `proto_path` is `$cwd/output/$proto_path`.
3031

3132
Use `-p` or `--proto_path` to specify the value.
3233

3334
### destination_path
34-
A directory within `$cwd`.
35+
A directory within `$cwd/output`.
3536
This is the path in which the generated library will reside.
36-
The absolute path of `destination_path` is `$cwd/$destination_path`.
37+
The absolute path of `destination_path` is `$cwd/output/$destination_path`.
3738

3839
Use `-d` or `--destination_path` to specify the value.
3940

40-
Note that you do not need to create `$detination_path` beforehand.
41+
Note that you do not need to create `$destination_path` beforehand.
4142

4243
The directory structure of the generated library is
4344
```

library_generation/generate_library.sh

+9-4
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ shift # past argument or value
5454
done
5555

5656
script_dir=$(dirname "$(readlink -f "$0")")
57-
# source utility functions
5857
source "${script_dir}"/utilities.sh
58+
output_folder="$(get_output_folder)"
59+
# source utility functions
5960

6061
if [ -z "${protobuf_version}" ]; then
6162
protobuf_version=$(get_protobuf_version "${gapic_generator_version}")
@@ -81,15 +82,17 @@ if [ -z "${os_architecture}" ]; then
8182
os_architecture=$(detect_os_architecture)
8283
fi
8384

84-
mkdir -p "${destination_path}"
85+
86+
mkdir -p "${output_folder}/${destination_path}"
8587
##################### Section 0 #####################
8688
# prepare tooling
8789
#####################################################
8890
# the order of services entries in gapic_metadata.json is relevant to the
8991
# order of proto file, sort the proto files with respect to their name to
9092
# get a fixed order.
91-
proto_files=$(find "${proto_path}" -type f -name "*.proto" | sort)
9293
folder_name=$(extract_folder_name "${destination_path}")
94+
pushd "${output_folder}"
95+
proto_files=$(find "${proto_path}" -type f -name "*.proto" | sort)
9396
# download gapic-generator-java, protobuf and grpc plugin.
9497
download_tools "${gapic_generator_version}" "${protobuf_version}" "${grpc_version}" "${os_architecture}"
9598
##################### Section 1 #####################
@@ -156,9 +159,11 @@ for proto_src in ${proto_files}; do
156159
mkdir -p "${destination_path}/proto-${folder_name}/src/main/proto"
157160
rsync -R "${proto_src}" "${destination_path}/proto-${folder_name}/src/main/proto"
158161
done
162+
popd # output_folder
159163
##################### Section 4 #####################
160164
# rm tar files
161165
#####################################################
162-
cd "${destination_path}"
166+
pushd "${output_folder}/${destination_path}"
163167
rm -rf java_gapic_srcjar java_gapic_srcjar_raw.srcjar.zip java_grpc.jar java_proto.jar temp-codegen.srcjar
168+
popd
164169
set +x

library_generation/test/generate_library_integration_test.sh

+13-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ set -xeo pipefail
1414

1515
# defaults
1616
googleapis_gen_url="[email protected]:googleapis/googleapis-gen.git"
17+
script_dir=$(dirname "$(readlink -f "$0")")
18+
source "${script_dir}/../utilities.sh"
19+
library_generation_dir="${script_dir}"/..
20+
output_folder="$(get_output_folder)"
1721

1822
while [[ $# -gt 0 ]]; do
1923
key="$1"
@@ -38,14 +42,13 @@ esac
3842
shift # past argument or value
3943
done
4044

41-
script_dir=$(dirname "$(readlink -f "$0")")
42-
source "${script_dir}/../utilities.sh"
43-
library_generation_dir="${script_dir}"/..
44-
cd "${library_generation_dir}"
45+
mkdir -p "${output_folder}"
46+
pushd "${output_folder}"
4547
# checkout the master branch of googleapis/google (proto files) and WORKSPACE
4648
echo "Checking out googlapis repository..."
4749
sparse_clone https://github.com/googleapis/googleapis.git "${proto_path} WORKSPACE google/api google/rpc google/cloud/common_resources.proto google/iam/v1 google/type google/longrunning"
48-
cd googleapis
50+
pushd googleapis
51+
cp -r google "${output_folder}"
4952
# parse version of gapic-generator-java, protobuf and grpc from WORKSPACE
5053
gapic_generator_version=$(get_version_from_WORKSPACE "_gapic_generator_java_version" WORKSPACE "=")
5154
echo "The version of gapic-generator-java is ${gapic_generator_version}."
@@ -60,6 +63,8 @@ rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${proto_build_file_path}
6063
include_samples=$(get_include_samples_from_BUILD "${proto_build_file_path}")
6164
echo "GAPIC options are transport=${transport}, rest_numeric_enums=${rest_numeric_enums}, include_samples=${include_samples}."
6265
# generate GAPIC client library
66+
popd
67+
popd
6368
echo "Generating library from ${proto_path}, to ${destination_path}..."
6469
"${library_generation_dir}"/generate_library.sh \
6570
-p "${proto_path}" \
@@ -74,19 +79,19 @@ echo "Generating library from ${proto_path}, to ${destination_path}..."
7479
echo "Generate library finished."
7580
echo "Checking out googleapis-gen repository..."
7681

82+
pushd "${output_folder}"
7783
sparse_clone "${googleapis_gen_url}" "${proto_path}"
7884

7985
echo "Compare generation result..."
8086
RESULT=0
81-
diff -r "googleapis-gen/${proto_path}/${destination_path}" "${destination_path}" -x "*gradle*" || RESULT=$?
87+
diff -r "googleapis-gen/${proto_path}/${destination_path}" "${output_folder}/${destination_path}" -x "*gradle*" || RESULT=$?
8288

8389
if [ ${RESULT} == 0 ] ; then
8490
echo "SUCCESS: Comparison finished, no difference is found."
8591
else
8692
echo "FAILURE: Differences found."
8793
fi
8894

89-
cd ..
90-
rm -rf googleapis
95+
popd # output_folder
9196

9297
exit ${RESULT}

library_generation/test/generate_library_unit_tests.sh

+7-10
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,14 @@ generate_library_failed_with_invalid_generator_version() {
181181
local destination="google-cloud-alloydb-v1-java"
182182
local res=0
183183
cd "${script_dir}/resources"
184-
$("${script_dir}"/../generate_library.sh \
184+
bash "${script_dir}"/../generate_library.sh \
185185
-p google/cloud/alloydb/v1 \
186186
-d ../"${destination}" \
187187
--gapic_generator_version 1.99.0 \
188188
--protobuf_version 23.2 \
189189
--grpc_version 1.55.1 \
190190
--transport grpc+rest \
191-
--rest_numeric_enums true \
192-
--os_architecture "$(__get_os_architecture)") || res=$?
191+
--rest_numeric_enums true || res=$?
193192
assertEquals 1 $((res))
194193
# still need to clean up potential downloaded tooling.
195194
cleanup "${destination}"
@@ -199,15 +198,14 @@ generate_library_failed_with_invalid_protobuf_version() {
199198
local destination="google-cloud-alloydb-v1-java"
200199
local res=0
201200
cd "${script_dir}/resources"
202-
$("${script_dir}"/../generate_library.sh \
201+
bash "${script_dir}"/../generate_library.sh \
203202
-p google/cloud/alloydb/v1 \
204203
-d ../"${destination}" \
205204
--gapic_generator_version 2.24.0 \
206205
--protobuf_version 22.99 \
207206
--grpc_version 1.55.1 \
208207
--transport grpc+rest \
209-
--rest_numeric_enums true \
210-
--os_architecture "$(__get_os_architecture)") || res=$?
208+
--rest_numeric_enums true || res=$?
211209
assertEquals 1 $((res))
212210
# still need to clean up potential downloaded tooling.
213211
cleanup "${destination}"
@@ -217,14 +215,13 @@ generate_library_failed_with_invalid_grpc_version() {
217215
local destination="google-cloud-alloydb-v1-java"
218216
local res=0
219217
cd "${script_dir}/resources"
220-
$("${script_dir}"/../generate_library.sh \
218+
bash "${script_dir}"/../generate_library.sh \
221219
-p google/cloud/alloydb/v1 \
222-
-d ../"${destination}" \
220+
-d ../output/"${destination}" \
223221
--gapic_generator_version 2.24.0 \
224222
--grpc_version 0.99.0 \
225223
--transport grpc+rest \
226-
--rest_numeric_enums true \
227-
--os_architecture "$(__get_os_architecture)") || res=$?
224+
--rest_numeric_enums true || res=$?
228225
assertEquals 1 $((res))
229226
# still need to clean up potential downloaded tooling.
230227
cleanup "${destination}"

library_generation/utilities.sh

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
set -xeo pipefail
44

5+
56
# private functions that should not be called outside this file.
67

78
# Used to obtain configuration values from a bazel BUILD file
@@ -157,13 +158,15 @@ get_protobuf_version() {
157158
}
158159

159160
download_tools() {
161+
pushd "${output_folder}"
160162
local gapic_generator_version=$1
161163
local protobuf_version=$2
162164
local grpc_version=$3
163165
local os_architecture=$4
164166
download_generator "${gapic_generator_version}"
165167
download_protobuf "${protobuf_version}" "${os_architecture}"
166168
download_grpc_plugin "${grpc_version}" "${os_architecture}"
169+
popd
167170
}
168171

169172
download_generator() {
@@ -197,7 +200,7 @@ download_protobuf() {
197200
rm "protobuf-${protobuf_version}.zip"
198201
fi
199202

200-
protoc_path=protobuf-${protobuf_version}/bin
203+
protoc_path="${output_folder}/protobuf-${protobuf_version}/bin"
201204
}
202205

203206
download_grpc_plugin() {
@@ -305,13 +308,13 @@ sparse_clone() {
305308
clone_dir=$(basename "${repo_url%.*}")
306309
rm -rf "${clone_dir}"
307310
git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}"
308-
cd "${clone_dir}"
311+
pushd "${clone_dir}"
309312
if [ -n "${commitish}" ]; then
310313
git checkout "${commitish}"
311314
fi
312315
git sparse-checkout set --no-cone ${paths}
313316
git checkout
314-
cd ..
317+
popd
315318
}
316319

317320
# takes a versions.txt file and returns its version
@@ -322,6 +325,13 @@ get_version_from_versions_txt() {
322325
echo "${version}"
323326
}
324327

328+
# gets the output folder where all sources and dependencies will be located. It
329+
# relies on utilities_script_dir which points to the same location as
330+
# `generate_library.sh`
331+
get_output_folder() {
332+
echo "$(pwd)/output"
333+
}
334+
325335
detect_os_architecture() {
326336
local os_type
327337
local os_architecture

showcase/scripts/generate_showcase.sh

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ source "${lib_gen_scripts_dir}/utilities.sh"
1111
readonly perform_cleanup=$1
1212

1313
cd "${SCRIPT_DIR}"
14+
mkdir -p "${SCRIPT_DIR}/output"
1415

1516
# clone gapic-showcase
1617
if [ ! -d schema ]; then
@@ -23,7 +24,7 @@ if [ ! -d schema ]; then
2324
showcase_version=$(grep -e '<gapic-showcase.version>' "${SCRIPT_DIR}/../gapic-showcase/pom.xml" | cut -d'>' -f2 | cut -d'<' -f1)
2425
sparse_clone https://github.com/googleapis/gapic-showcase.git "schema/google/showcase/v1beta1" "v${showcase_version}"
2526
cd gapic-showcase
26-
mv schema ..
27+
mv schema ../output
2728
cd ..
2829
rm -rdf gapic-showcase
2930
fi
@@ -32,7 +33,7 @@ if [ ! -d google ];then
3233
rm -rdf googleapis
3334
fi
3435
sparse_clone https://github.com/googleapis/googleapis.git "WORKSPACE google/api google/rpc google/cloud/common_resources.proto google/longrunning google/iam/v1 google/cloud/location google/type"
35-
mv googleapis/google .
36+
mv googleapis/google output
3637
rm -rdf googleapis
3738
fi
3839

@@ -41,20 +42,20 @@ ggj_version=$(get_version_from_versions_txt ../../versions.txt "gapic-generator-
4142
rest_numeric_enums="false"
4243
transport="grpc+rest"
4344
include_samples="false"
44-
rm -rdf showcase-output
45-
mkdir showcase-output
45+
rm -rdf output/showcase-output
46+
mkdir output/showcase-output
4647
set +e
4748
bash "${SCRIPT_DIR}/../../library_generation/generate_library.sh" \
4849
--proto_path "schema/google/showcase/v1beta1" \
4950
--destination_path "showcase-output" \
5051
--gapic_generator_version "${ggj_version}" \
5152
--rest_numeric_enums "${rest_numeric_enums}" \
5253
--include_samples "${include_samples}" \
53-
--transport "${transport}" &> out
54+
--transport "${transport}"
5455

5556
exit_code=$?
5657
if [ "${exit_code}" -ne 0 ]; then
57-
rm -rdf showcase-output
58+
rm -rdf output
5859
exit "${exit_code}"
5960
fi
6061
set +x

showcase/scripts/showcase_utilities.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
function cleanup {
44
script_dir=$1
55
cd "${script_dir}"
6-
rm -rdf gapic-generator-java* google schema protobuf-* protoc-gen-grpc-java* showcase-output out
6+
rm -rdf output gapic-generator-java*
77
}

showcase/scripts/verify.sh

+18-8
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@ readonly REPLACE_SOURCE='false'
1212
readonly PERFORM_CLEANUP='false'
1313
bash "${SCRIPT_DIR}/generate_components.sh" "${REPLACE_SOURCE}" "${PERFORM_CLEANUP}"
1414

15-
# compare with proto library
1615
PROTO_PROJECT_DIR='proto-gapic-showcase-v1beta1'
17-
diff -ru "${SHOWCASE_DIR}/${PROTO_PROJECT_DIR}/src" "${SCRIPT_DIR}/showcase-output/proto-showcase-output/src"
18-
19-
# compare with grpc library
2016
GRPC_PROJECT_DIR='grpc-gapic-showcase-v1beta1'
21-
diff -ru "${SHOWCASE_DIR}/${GRPC_PROJECT_DIR}/src/main/java/com" "${SCRIPT_DIR}/showcase-output/grpc-showcase-output/src/main/java/com"
22-
23-
# compare with gapic library
2417
GAPIC_PROJECT_DIR='gapic-showcase'
25-
diff -ru "${SHOWCASE_DIR}/${GAPIC_PROJECT_DIR}/src/main/java" "${SCRIPT_DIR}/showcase-output/gapic-showcase-output/src/main/java"
18+
19+
{
20+
# compare with proto library
21+
(diff -ru "${SHOWCASE_DIR}/${PROTO_PROJECT_DIR}/src" "${SCRIPT_DIR}/output/showcase-output/proto-showcase-output/src") &&
22+
23+
# compare with grpc library
24+
(diff -ru "${SHOWCASE_DIR}/${GRPC_PROJECT_DIR}/src/main/java/com" "${SCRIPT_DIR}/output/showcase-output/grpc-showcase-output/src/main/java/com") &&
25+
26+
# compare with gapic library
27+
(diff -ru "${SHOWCASE_DIR}/${GAPIC_PROJECT_DIR}/src/main/java" "${SCRIPT_DIR}/output/showcase-output/gapic-showcase-output/src/main/java")
28+
} || {
29+
failure="true"
30+
}
2631

2732
cleanup $SCRIPT_DIR
33+
34+
if [ "${failure}" == "true" ]; then
35+
exit 1
36+
fi
37+

0 commit comments

Comments
 (0)