Skip to content

Commit 259e9f7

Browse files
chore: bake Java formatter into image (#3271)
In this PR: - Bake Java formatter into image. --------- Co-authored-by: cloud-java-bot <[email protected]>
1 parent cd25b40 commit 259e9f7

File tree

9 files changed

+79
-48
lines changed

9 files changed

+79
-48
lines changed

.cloudbuild/library_generation/library_generation.Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SHELL [ "/bin/bash", "-c" ]
3535
ARG OWLBOT_CLI_COMMITTISH=38fe6f89a2339ee75c77739b31b371f601b01bb3
3636
ARG PROTOC_VERSION=25.5
3737
ARG GRPC_VERSION=1.67.1
38+
ARG JAVA_FORMAT_VERSION=1.7
3839
ENV HOME=/home
3940
ENV OS_ARCHITECTURE="linux-x86_64"
4041

@@ -101,6 +102,11 @@ RUN owl-bot copy-code --version
101102
RUN chmod -R o+rx ${NODE_PATH}
102103
RUN ln -sf ${NODE_PATH}/* /usr/local/bin
103104

105+
# download the Java formatter
106+
ADD https://maven-central.storage-download.googleapis.com/maven2/com/google/googlejavaformat/google-java-format/${JAVA_FORMAT_VERSION}/google-java-format-${JAVA_FORMAT_VERSION}-all-deps.jar \
107+
"${HOME}"/.library_generation/google-java-format.jar
108+
RUN chmod 755 "${HOME}"/.library_generation/google-java-format.jar
109+
104110
# allow users to access the script folders
105111
RUN chmod -R o+rx /src
106112

library_generation/DEVELOPMENT.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ This section explains how to run the entrypoint script
5151
## Assumptions made by the scripts
5252
### The Hermetic Build's well-known folder
5353
Located in `${HOME}/.library_generation`, this folder is assumed by the scripts
54-
to contain the generator JAR.
55-
Please note that this is a recent feature and only this jar is expected to be
56-
there.
54+
to contain certain tools.
55+
5756
Developers must make sure this folder is properly configured before running the
5857
scripts locally.
5958
Note that this relies on the `HOME` en var which is always defined as per
@@ -73,7 +72,16 @@ The generation scripts will assume the jar is there.
7372
mv /path/to/jar "${HOME}/.library_generation/gapic-generator-java.jar"
7473
```
7574

75+
#### Put the java formatter jar in its well-known location
76+
77+
Download google-java-format-{version}-all-deps.jar from [Maven Central](https://central.sonatype.com/artifact/com.google.googlejavaformat/google-java-format)
78+
or [GitHub releases](https://github.com/google/google-java-format/releases).
79+
Then `mv` the jar into the well-known location of the jar.
80+
The generation scripts will assume the jar is there.
7681

82+
```shell
83+
mv /path/to/jar "${HOME}/.library_generation/google-java-format.jar"
84+
```
7785

7886
## Installing prerequisites
7987

library_generation/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ google-cloud-java) from a configuration file.
88

99
- OS: Linux
1010
- Java runtime environment (8 or above)
11-
- Apache Maven (used in formatting source code)
1211
- Python (3.11.6 or above)
1312
- Docker
1413
- Git

library_generation/owlbot/bin/entrypoint.sh

+2-8
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,14 @@ echo "...done"
6565

6666
# write or restore clirr-ignored-differences.xml
6767
echo "Generating clirr-ignored-differences.xml..."
68-
${scripts_root}/owlbot/bin/write_clirr_ignore.sh "${scripts_root}"
68+
"${scripts_root}"/owlbot/bin/write_clirr_ignore.sh "${scripts_root}"
6969
echo "...done"
7070

7171
# fix license headers
7272
echo "Fixing missing license headers..."
7373
python3 "${scripts_root}/owlbot/src/fix-license-headers.py"
7474
echo "...done"
7575

76-
# Ensure formatting on all .java files in the repository.
77-
# Here we manually set the user.home system variable. Unfortunately, Maven
78-
# user.home inference involves the /etc/passwd file (confirmed empirically),
79-
# instead of the presumable $HOME env var, which may not work properly
80-
# when `docker run`ning with the -u flag because we may incur in users
81-
# not registered in the container's /etc/passwd file
8276
echo "Reformatting source..."
83-
mvn fmt:format -Duser.home="${HOME}" -V --batch-mode --no-transfer-progress
77+
"${scripts_root}"/owlbot/bin/format_source.sh "${scripts_root}"
8478
echo "...done"

library_generation/owlbot/bin/format_source.sh

+8-14
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ set -e
2222

2323
# Find all the java files relative to the current directory and format them
2424
# using google-java-format
25-
list="$(find . -name '*.java' -not -path ".*/samples/snippets/generated/**/*" )"
2625
scripts_root=$1
27-
tmpfile=$(mktemp)
26+
27+
source "${scripts_root}/utils/utilities.sh"
28+
list="$(find . -name '*.java' -not -path ".*/samples/snippets/generated/**/*" )"
29+
tmp_file=$(mktemp)
2830

2931
for file in $list;
3032
do
@@ -35,19 +37,11 @@ do
3537
then
3638
echo "File skipped formatting: $file"
3739
else
38-
echo $file >> $tmpfile
40+
echo $file >> $tmp_file
3941
fi
4042
done
4143

42-
# download the google-java-format tool
43-
if [ ! -f "${scripts_root}/owlbot/google-java-format.jar" ]; then
44-
echo 'downloading google-java-format'
45-
java_format_version=$(cat "${scripts_root}/configuration/java-format-version")
46-
wget -O "${scripts_root}/owlbot/google-java-format.jar" https://repo1.maven.org/maven2/com/google/googlejavaformat/google-java-format/${java_format_version}/google-java-format-${java_format_version}-all-deps.jar
47-
48-
fi
49-
50-
# format the source
51-
cat $tmpfile | xargs java -jar "${scripts_root}/owlbot/google-java-format.jar" --replace
44+
# use formatter downloaded in the Dockerfile.
45+
cat $tmp_file | xargs java -jar "$(get_java_formatter_location)" --replace
5246

53-
rm $tmpfile
47+
rm $tmp_file

library_generation/test/generate_library_unit_tests.sh

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ source "${script_dir}"/test_utilities.sh
1212
readonly SIMULATED_HOME=$(mktemp -d)
1313
mkdir "${SIMULATED_HOME}/.library_generation"
1414
touch "${SIMULATED_HOME}/.library_generation/gapic-generator-java.jar"
15+
touch "${SIMULATED_HOME}/.library_generation/google-java-format.jar"
1516
HOME="${SIMULATED_HOME}" source "${script_dir}"/../utils/utilities.sh
1617

1718
# Unit tests

library_generation/test/utilities_unit_tests.py

+8
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ def test_prepare_repo_split_repo_success(self):
303303
self.assertEqual(["misc"], library_path)
304304
shutil.rmtree(repo_config.output_folder)
305305

306+
def test_get_java_generator_location_success(self):
307+
location = util.sh_util("get_gapic_generator_location")
308+
self.assertRegex(location, r"/.library_generation/gapic-generator-java.jar$")
309+
310+
def test_get_java_formatter_location_success(self):
311+
location = util.sh_util("get_java_formatter_location")
312+
self.assertRegex(location, r"/.library_generation/google-java-format.jar$")
313+
306314
def __setup_postprocessing_prerequisite_files(
307315
self,
308316
combination: int,

library_generation/utils/utilities.sh

+25-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -eo pipefail
44
utilities_script_dir=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
55
# The $HOME variable is always set in the OS env as per POSIX specification.
66
GAPIC_GENERATOR_LOCATION="${HOME}/.library_generation/gapic-generator-java.jar"
7+
JAVA_FORMATTER_LOCATION="${HOME}/.library_generation/google-java-format.jar"
78

89
# Utility functions used in `generate_library.sh` and showcase generation.
910
extract_folder_name() {
@@ -137,10 +138,11 @@ get_protoc_version() {
137138
# and "grpc_path" to DOCKER_PROTOC_PATH and DOCKER_GRPC_PATH respectively (no
138139
# download), since the docker image will have downloaded these tools beforehand.
139140
#
140-
# For the case of gapic-generator-java, no env var will be exported for the
141-
# upstream flow. Instead, the jar must be located in the well-known location
142-
# (${HOME}/.library_generation/gapic-generator-java.jar). More information in
143-
# `library_generation/DEVELOPMENT.md`
141+
# For the case of generator and formatter, no env var will be exported for the
142+
# upstream flow.
143+
# Instead, the jar must be located in the well-known location
144+
# (${HOME}/.library_generation/).
145+
# More information in `library_generation/DEVELOPMENT.md`.
144146
download_tools() {
145147
local protoc_version=$1
146148
local grpc_version=$2
@@ -166,18 +168,11 @@ download_tools() {
166168
export grpc_path=$(download_grpc_plugin "${grpc_version}" "${os_architecture}")
167169
fi
168170

169-
# Here we check whether the jar is stored in the expected location.
170-
# The docker image will prepare the jar in this location. Developers must
171-
# prepare their environment by creating
172-
# $HOME/.library_generation/gapic_generator_java.jar
173-
# This check is meant to ensure integrity of the downstream workflow. (i.e.
174-
# ensure the generator wrapper succeeds)
175-
if [[ ! -f "${GAPIC_GENERATOR_LOCATION}" ]]; then
176-
>&2 echo "File ${GAPIC_GENERATOR_LOCATION} not found in the "
177-
>&2 echo "filesystem. Please configure your environment and store the "
178-
>&2 echo "generator jar in this location"
179-
exit 1
180-
fi
171+
# Here we check whether required tools is stored in the expected location.
172+
# The docker image will prepare jar files in this location.
173+
# This check is meant to ensure integrity of the downstream workflow.
174+
error_if_not_exists "${GAPIC_GENERATOR_LOCATION}"
175+
error_if_not_exists "${JAVA_FORMATTER_LOCATION}"
181176
popd
182177
}
183178

@@ -380,3 +375,17 @@ download_googleapis_files_and_folders() {
380375
get_gapic_generator_location() {
381376
echo "${GAPIC_GENERATOR_LOCATION}"
382377
}
378+
379+
get_java_formatter_location() {
380+
echo "${JAVA_FORMATTER_LOCATION}"
381+
}
382+
383+
error_if_not_exists() {
384+
local required_tool=$1
385+
if [[ ! -f "${required_tool}" ]]; then
386+
>&2 echo "File ${required_tool} not found in the filesystem. "
387+
>&2 echo "Please configure your environment and store the "
388+
>&2 echo "required tools in this location."
389+
exit 1
390+
fi
391+
}

showcase/scripts/generate_showcase.sh

+18-6
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,42 @@ if [ ! -d google ];then
5555
rm -rdf googleapis
5656
fi
5757

58-
# copy the generator into its well-known location. For more details,
58+
# copy the generator and formatter into its well-known location.
59+
# For more details,
5960
# refer to library_generation/DEVELOPMENT.md
61+
java_formatter_name="google-java-format.jar"
62+
java_formatter_version="1.7"
6063
well_known_folder="${HOME}/.library_generation"
6164
well_known_generator_jar_location="${well_known_folder}/gapic-generator-java.jar"
65+
well_known_formatter_jar_location="${well_known_folder}/${java_formatter_name}"
6266
if [[ ! -d "${well_known_folder}" ]]; then
6367
mkdir "${well_known_folder}"
6468
fi
6569
if [[ -f "${well_known_generator_jar_location}" ]]; then
6670
echo "replacing well-known generator jar with the latest one"
6771
rm "${well_known_generator_jar_location}"
6872
fi
73+
if [[ -f "${well_known_formatter_jar_location}" ]]; then
74+
echo "replacing well-known formatter jar with the latest one"
75+
rm "${well_known_formatter_jar_location}"
76+
fi
6977
maven_repository="$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)"
7078
generator_version=$(get_version_from_versions_txt "gapic-generator-java")
71-
source_jar_path="${maven_repository}/com/google/api/gapic-generator-java/${generator_version}/gapic-generator-java-${generator_version}.jar"
79+
generator_jar_path="${maven_repository}/com/google/api/gapic-generator-java/${generator_version}/gapic-generator-java-${generator_version}.jar"
7280

73-
if [[ ! -f "${source_jar_path}" ]]; then
81+
if [[ ! -f "${generator_jar_path}" ]]; then
7482
echo "generator jar not found in its assumed location"
75-
echo "in the local repository: ${source_jar_path}"
83+
echo "in the local repository: ${generator_jar_path}"
7684
echo "(did you run mvn install in this repository's root?)"
7785
exit 1
7886
fi
7987
# transfer the snapshot jar into its well-known location
80-
cp "${source_jar_path}" "${well_known_generator_jar_location}"
81-
88+
cp "${generator_jar_path}" "${well_known_generator_jar_location}"
89+
# transfer java formatter to its well-known location
90+
download_from \
91+
"https://maven-central.storage-download.googleapis.com/maven2/com/google/googlejavaformat/google-java-format/${java_formatter_version}/google-java-format-${java_formatter_version}-all-deps.jar" \
92+
"${java_formatter_name}"
93+
cp "${java_formatter_name}" "${well_known_formatter_jar_location}"
8294
gapic_additional_protos="google/iam/v1/iam_policy.proto google/cloud/location/locations.proto"
8395

8496
path_to_generator_parent_pom="${SCRIPT_DIR}/../../gapic-generator-java-pom-parent/pom.xml"

0 commit comments

Comments
 (0)