Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hermetic-build): obtain gapic-generator-java locally on release branch #2023

Merged
merged 9 commits into from Sep 21, 2023
4 changes: 2 additions & 2 deletions library_generation/README.md
Expand Up @@ -72,8 +72,8 @@ You can find the released version of gapic-generator-java in [maven central](htt

Use `--gapic_generator_version` to specify the value.

Note that you can specify a `SNAPSHOT` version as long as you have build the SNAPSHOT of gapic-generator-java in your maven
local repository.
Note that you can specify any non-published version (e.g. a SNAPSHOT) as long as you have installed it in your maven
local repository. The script will search locally first.

### protobuf_version (optional)
You can find the released version of protobuf in [GitHub](https://github.com/protocolbuffers/protobuf/releases/).
Expand Down
32 changes: 16 additions & 16 deletions library_generation/utilities.sh
@@ -1,6 +1,7 @@
#!/usr/bin/env bash

set -xeo pipefail
util_script_dir=$(dirname "${BASH_SOURCE}")

# Utility functions used in `generate_library.sh` and showcase generation.
extract_folder_name() {
Expand Down Expand Up @@ -76,16 +77,15 @@ remove_grpc_version() {
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" \
# first, try to fetch the generator pom locally
bash -c "source ${util_script_dir}/utilities.sh && 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\" \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need source command here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to load the functions into the secondary shell. This is all with the intent to capture the exit code in case the local search doesn't work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we always try to copy files locally before downloading from maven central, I think we can modify copy_from not failing but return a value to indicate whether copy is success or not.

Also, we need to modify the error message in download_fail to indicate the file is not found neither locally or in maven central should the download fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out, makes sense. I modified copy_from to return (echo) a value instead. I added a message to indicate an external repo attempt will be made in case it wasn't found locally. I also created a more generic function for the pom parent and the jar.

\"gapic-generator-java-pom-parent-${gapic_generator_version}.pom\"" || not_found_locally="true"
if [[ "${not_found_locally}" == "true" ]];then
# 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"
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.
}
Expand Down Expand Up @@ -123,16 +123,16 @@ download_tools() {
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" \
# first, try to fetch the generator locally
bash -c "source ${util_script_dir}/utilities.sh && 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\"" || not_found_locally="true"
if [[ "${not_found_locally}" == "true" ]];then
# download gapic-generator-java from Google maven central mirror if not
# found locally
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"
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
}

Expand Down