Skip to content

Commit

Permalink
feat: generate proto-only library (#2046)
Browse files Browse the repository at this point in the history
* feat: generate non-gapic library

* change parameter to `proto_only`

* add doc for `proto_only`

* change comments

* add proto-only library in integration test
  • Loading branch information
JoeWang1127 authored Sep 28, 2023
1 parent 0e3724a commit f9ac8fb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 31 deletions.
11 changes: 11 additions & 0 deletions library_generation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ Use `--grpc_version` to specify the value.

Note that if specified, the version should be compatible with gapic-generator-java.

### proto_only (optional)
Whether this is a proto-only library (no `gapic-*` directory in the generated
library).
The default value is `false`.

When set to `true`, the GAPIC generator will not be invoked.
Therefore, GAPIC options (`transport`, `rest_numeric_enums`) and
`gapic_additional_protos` will be ignored.

Use `--proto_only` to specify the value.

### gapic_additional_protos (optional)
Additional protos that pass to the generator.
The default value is `google/cloud/common_resources.proto`.
Expand Down
73 changes: 42 additions & 31 deletions library_generation/generate_library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ case $key in
grpc_version="$2"
shift
;;
--proto_only)
proto_only="$2"
shift
;;
--gapic_additional_protos)
gapic_additional_protos="$2"
shift
Expand Down Expand Up @@ -69,6 +73,10 @@ if [ -z "${grpc_version}" ]; then
grpc_version=$(get_grpc_version "${gapic_generator_version}")
fi

if [ -z "${proto_only}" ]; then
proto_only="false"
fi

if [ -z "${gapic_additional_protos}" ]; then
gapic_additional_protos="google/cloud/common_resources.proto"
fi
Expand Down Expand Up @@ -120,43 +128,46 @@ fi
###################### Section 2 #####################
## generate gapic-*/, part of proto-*/, samples/
######################################################
"$protoc_path"/protoc --experimental_allow_proto3_optional \
"--plugin=protoc-gen-java_gapic=${script_dir}/gapic-generator-java-wrapper" \
"--java_gapic_out=metadata:${destination_path}/java_gapic_srcjar_raw.srcjar.zip" \
"--java_gapic_opt=$(get_gapic_opts)" \
${proto_files} ${gapic_additional_protos}
if [[ "${proto_only}" == "false" ]]; then
"$protoc_path"/protoc --experimental_allow_proto3_optional \
"--plugin=protoc-gen-java_gapic=${script_dir}/gapic-generator-java-wrapper" \
"--java_gapic_out=metadata:${destination_path}/java_gapic_srcjar_raw.srcjar.zip" \
"--java_gapic_opt=$(get_gapic_opts)" \
${proto_files} ${gapic_additional_protos}

unzip -o -q "${destination_path}/java_gapic_srcjar_raw.srcjar.zip" -d "${destination_path}"
# Sync'\''d to the output file name in Writer.java.
unzip -o -q "${destination_path}/temp-codegen.srcjar" -d "${destination_path}/java_gapic_srcjar"
# Resource name source files.
proto_dir=${destination_path}/java_gapic_srcjar/proto/src/main/java
if [ ! -d "${proto_dir}" ]; then
# Some APIs don'\''t have resource name helpers, like BigQuery v2.
# Create an empty file so we can finish building. Gating the resource name rule definition
# on file existences go against Bazel'\''s design patterns, so we'\''ll simply delete all empty
# files during the final packaging process (see java_gapic_pkg.bzl)
mkdir -p "${proto_dir}"
touch "${proto_dir}"/PlaceholderFile.java
fi

# move java_gapic_srcjar/src/main to gapic-*/src.
mv_src_files "gapic" "main"
# remove empty files in gapic-*/src/main/java
remove_empty_files "gapic"
# move java_gapic_srcjar/src/test to gapic-*/src
mv_src_files "gapic" "test"
if [ "${include_samples}" == "true" ]; then
# move java_gapic_srcjar/samples/snippets to samples/snippets
mv_src_files "samples" "main"
unzip -o -q "${destination_path}/java_gapic_srcjar_raw.srcjar.zip" -d "${destination_path}"
# Sync'\''d to the output file name in Writer.java.
unzip -o -q "${destination_path}/temp-codegen.srcjar" -d "${destination_path}/java_gapic_srcjar"
# Resource name source files.
proto_dir=${destination_path}/java_gapic_srcjar/proto/src/main/java
if [ ! -d "${proto_dir}" ]; then
# Some APIs don't have resource name helpers, like BigQuery v2.
# Create an empty file so we can finish building. Gating the resource name rule definition
# on file existences go against Bazel's design patterns, so we'll simply delete all empty
# files during the final packaging process (see java_gapic_pkg.bzl)
mkdir -p "${proto_dir}"
touch "${proto_dir}"/PlaceholderFile.java
fi
# move java_gapic_srcjar/src/main to gapic-*/src.
mv_src_files "gapic" "main"
# remove empty files in gapic-*/src/main/java
remove_empty_files "gapic"
# move java_gapic_srcjar/src/test to gapic-*/src
mv_src_files "gapic" "test"
if [ "${include_samples}" == "true" ]; then
# move java_gapic_srcjar/samples/snippets to samples/snippets
mv_src_files "samples" "main"
fi
fi
##################### Section 3 #####################
# generate proto-*/
#####################################################
"$protoc_path"/protoc "--java_out=${destination_path}/java_proto.jar" ${proto_files}
# move java_gapic_srcjar/proto/src/main/java (generated resource name helper class)
# to proto-*/src/main
mv_src_files "proto" "main"
if [[ "${proto_only}" == "false" ]]; then
# move java_gapic_srcjar/proto/src/main/java (generated resource name helper class)
# to proto-*/src/main
mv_src_files "proto" "main"
fi
# unzip java_proto.jar to proto-*/src/main/java
unzip_src_files "proto"
# remove empty files in proto-*/src/main/java
Expand Down
2 changes: 2 additions & 0 deletions library_generation/test/generate_library_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ grep -v '^ *#' < "${proto_path_list}" | while IFS= read -r line; do
# parse GAPIC options from proto_path/BUILD.bazel
pushd "${output_folder}"
proto_build_file_path="${proto_path}/BUILD.bazel"
proto_only=$(get_proto_only_from_BUILD "${proto_build_file_path}")
gapic_additional_protos=$(get_gapic_additional_protos_from_BUILD "${proto_build_file_path}")
transport=$(get_transport_from_BUILD "${proto_build_file_path}")
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${proto_build_file_path}")
Expand All @@ -77,6 +78,7 @@ grep -v '^ *#' < "${proto_path_list}" | while IFS= read -r line; do
-d "${destination_path}" \
--gapic_generator_version "${gapic_generator_version}" \
--protobuf_version "${protobuf_version}" \
--proto_only "${proto_only}" \
--gapic_additional_protos "${gapic_additional_protos}" \
--transport "${transport}" \
--rest_numeric_enums "${rest_numeric_enums}" \
Expand Down
1 change: 1 addition & 0 deletions library_generation/test/resources/proto_path_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ google/cloud/kms/v1 google-cloud-kms-v1-java
google/cloud/redis/v1 google-cloud-redis-v1-java
google/example/library/v1 google-cloud-example-library-v1-java
google/devtools/containeranalysis/v1 google-cloud-devtools-containeranalysis-v1-java
google/firestore/bundle google-cloud-firestore-bundle-v1-java
# google/iam/v1 google-iam-v1-java
google/iam/credentials/v1 google-cloud-iam-credentials-v1-java
google/logging/v2 google-cloud-logging-v2-java
Expand Down
12 changes: 12 additions & 0 deletions library_generation/test/test_utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ execute_tests() {
}

############# Utility functions used in `generate_library_integration_tests.sh` #############
get_proto_only_from_BUILD() {
local build_file=$1
local proto_only
proto_only=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"java_gapic_library" \
"true" \
"false"
)
echo "${proto_only}"
}

# Apart from proto files in proto_path, additional protos are needed in order
# to generate GAPIC client libraries.
Expand Down
7 changes: 7 additions & 0 deletions library_generation/utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ extract_folder_name() {

remove_empty_files() {
local category=$1
local file_num
find "${destination_path}/${category}-${folder_name}/src/main/java" -type f -size 0 | while read -r f; do rm -f "${f}"; done
# remove the directory if the directory has no files.
file_num=$(find "${destination_path}/${category}-${folder_name}" -type f | wc -l | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [[ "${file_num}" == 0 ]]; then
rm -rf "${destination_path}/${category}-${folder_name}"
fi

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
Expand Down

0 comments on commit f9ac8fb

Please sign in to comment.