Skip to content

Commit f9ac8fb

Browse files
authored
feat: generate proto-only library (#2046)
* feat: generate non-gapic library * change parameter to `proto_only` * add doc for `proto_only` * change comments * add proto-only library in integration test
1 parent 0e3724a commit f9ac8fb

File tree

6 files changed

+75
-31
lines changed

6 files changed

+75
-31
lines changed

library_generation/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ Use `--grpc_version` to specify the value.
9191

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

94+
### proto_only (optional)
95+
Whether this is a proto-only library (no `gapic-*` directory in the generated
96+
library).
97+
The default value is `false`.
98+
99+
When set to `true`, the GAPIC generator will not be invoked.
100+
Therefore, GAPIC options (`transport`, `rest_numeric_enums`) and
101+
`gapic_additional_protos` will be ignored.
102+
103+
Use `--proto_only` to specify the value.
104+
94105
### gapic_additional_protos (optional)
95106
Additional protos that pass to the generator.
96107
The default value is `google/cloud/common_resources.proto`.

library_generation/generate_library.sh

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ case $key in
2929
grpc_version="$2"
3030
shift
3131
;;
32+
--proto_only)
33+
proto_only="$2"
34+
shift
35+
;;
3236
--gapic_additional_protos)
3337
gapic_additional_protos="$2"
3438
shift
@@ -69,6 +73,10 @@ if [ -z "${grpc_version}" ]; then
6973
grpc_version=$(get_grpc_version "${gapic_generator_version}")
7074
fi
7175

76+
if [ -z "${proto_only}" ]; then
77+
proto_only="false"
78+
fi
79+
7280
if [ -z "${gapic_additional_protos}" ]; then
7381
gapic_additional_protos="google/cloud/common_resources.proto"
7482
fi
@@ -120,43 +128,46 @@ fi
120128
###################### Section 2 #####################
121129
## generate gapic-*/, part of proto-*/, samples/
122130
######################################################
123-
"$protoc_path"/protoc --experimental_allow_proto3_optional \
124-
"--plugin=protoc-gen-java_gapic=${script_dir}/gapic-generator-java-wrapper" \
125-
"--java_gapic_out=metadata:${destination_path}/java_gapic_srcjar_raw.srcjar.zip" \
126-
"--java_gapic_opt=$(get_gapic_opts)" \
127-
${proto_files} ${gapic_additional_protos}
131+
if [[ "${proto_only}" == "false" ]]; then
132+
"$protoc_path"/protoc --experimental_allow_proto3_optional \
133+
"--plugin=protoc-gen-java_gapic=${script_dir}/gapic-generator-java-wrapper" \
134+
"--java_gapic_out=metadata:${destination_path}/java_gapic_srcjar_raw.srcjar.zip" \
135+
"--java_gapic_opt=$(get_gapic_opts)" \
136+
${proto_files} ${gapic_additional_protos}
128137

129-
unzip -o -q "${destination_path}/java_gapic_srcjar_raw.srcjar.zip" -d "${destination_path}"
130-
# Sync'\''d to the output file name in Writer.java.
131-
unzip -o -q "${destination_path}/temp-codegen.srcjar" -d "${destination_path}/java_gapic_srcjar"
132-
# Resource name source files.
133-
proto_dir=${destination_path}/java_gapic_srcjar/proto/src/main/java
134-
if [ ! -d "${proto_dir}" ]; then
135-
# Some APIs don'\''t have resource name helpers, like BigQuery v2.
136-
# Create an empty file so we can finish building. Gating the resource name rule definition
137-
# on file existences go against Bazel'\''s design patterns, so we'\''ll simply delete all empty
138-
# files during the final packaging process (see java_gapic_pkg.bzl)
139-
mkdir -p "${proto_dir}"
140-
touch "${proto_dir}"/PlaceholderFile.java
141-
fi
142-
143-
# move java_gapic_srcjar/src/main to gapic-*/src.
144-
mv_src_files "gapic" "main"
145-
# remove empty files in gapic-*/src/main/java
146-
remove_empty_files "gapic"
147-
# move java_gapic_srcjar/src/test to gapic-*/src
148-
mv_src_files "gapic" "test"
149-
if [ "${include_samples}" == "true" ]; then
150-
# move java_gapic_srcjar/samples/snippets to samples/snippets
151-
mv_src_files "samples" "main"
138+
unzip -o -q "${destination_path}/java_gapic_srcjar_raw.srcjar.zip" -d "${destination_path}"
139+
# Sync'\''d to the output file name in Writer.java.
140+
unzip -o -q "${destination_path}/temp-codegen.srcjar" -d "${destination_path}/java_gapic_srcjar"
141+
# Resource name source files.
142+
proto_dir=${destination_path}/java_gapic_srcjar/proto/src/main/java
143+
if [ ! -d "${proto_dir}" ]; then
144+
# Some APIs don't have resource name helpers, like BigQuery v2.
145+
# Create an empty file so we can finish building. Gating the resource name rule definition
146+
# on file existences go against Bazel's design patterns, so we'll simply delete all empty
147+
# files during the final packaging process (see java_gapic_pkg.bzl)
148+
mkdir -p "${proto_dir}"
149+
touch "${proto_dir}"/PlaceholderFile.java
150+
fi
151+
# move java_gapic_srcjar/src/main to gapic-*/src.
152+
mv_src_files "gapic" "main"
153+
# remove empty files in gapic-*/src/main/java
154+
remove_empty_files "gapic"
155+
# move java_gapic_srcjar/src/test to gapic-*/src
156+
mv_src_files "gapic" "test"
157+
if [ "${include_samples}" == "true" ]; then
158+
# move java_gapic_srcjar/samples/snippets to samples/snippets
159+
mv_src_files "samples" "main"
160+
fi
152161
fi
153162
##################### Section 3 #####################
154163
# generate proto-*/
155164
#####################################################
156165
"$protoc_path"/protoc "--java_out=${destination_path}/java_proto.jar" ${proto_files}
157-
# move java_gapic_srcjar/proto/src/main/java (generated resource name helper class)
158-
# to proto-*/src/main
159-
mv_src_files "proto" "main"
166+
if [[ "${proto_only}" == "false" ]]; then
167+
# move java_gapic_srcjar/proto/src/main/java (generated resource name helper class)
168+
# to proto-*/src/main
169+
mv_src_files "proto" "main"
170+
fi
160171
# unzip java_proto.jar to proto-*/src/main/java
161172
unzip_src_files "proto"
162173
# remove empty files in proto-*/src/main/java

library_generation/test/generate_library_integration_test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ grep -v '^ *#' < "${proto_path_list}" | while IFS= read -r line; do
6464
# parse GAPIC options from proto_path/BUILD.bazel
6565
pushd "${output_folder}"
6666
proto_build_file_path="${proto_path}/BUILD.bazel"
67+
proto_only=$(get_proto_only_from_BUILD "${proto_build_file_path}")
6768
gapic_additional_protos=$(get_gapic_additional_protos_from_BUILD "${proto_build_file_path}")
6869
transport=$(get_transport_from_BUILD "${proto_build_file_path}")
6970
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${proto_build_file_path}")
@@ -77,6 +78,7 @@ grep -v '^ *#' < "${proto_path_list}" | while IFS= read -r line; do
7778
-d "${destination_path}" \
7879
--gapic_generator_version "${gapic_generator_version}" \
7980
--protobuf_version "${protobuf_version}" \
81+
--proto_only "${proto_only}" \
8082
--gapic_additional_protos "${gapic_additional_protos}" \
8183
--transport "${transport}" \
8284
--rest_numeric_enums "${rest_numeric_enums}" \

library_generation/test/resources/proto_path_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ google/cloud/kms/v1 google-cloud-kms-v1-java
99
google/cloud/redis/v1 google-cloud-redis-v1-java
1010
google/example/library/v1 google-cloud-example-library-v1-java
1111
google/devtools/containeranalysis/v1 google-cloud-devtools-containeranalysis-v1-java
12+
google/firestore/bundle google-cloud-firestore-bundle-v1-java
1213
# google/iam/v1 google-iam-v1-java
1314
google/iam/credentials/v1 google-cloud-iam-credentials-v1-java
1415
google/logging/v2 google-cloud-logging-v2-java

library_generation/test/test_utilities.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ execute_tests() {
126126
}
127127

128128
############# Utility functions used in `generate_library_integration_tests.sh` #############
129+
get_proto_only_from_BUILD() {
130+
local build_file=$1
131+
local proto_only
132+
proto_only=$(__get_config_from_BUILD \
133+
"${build_file}" \
134+
"java_gapic_library(" \
135+
"java_gapic_library" \
136+
"true" \
137+
"false"
138+
)
139+
echo "${proto_only}"
140+
}
129141

130142
# Apart from proto files in proto_path, additional protos are needed in order
131143
# to generate GAPIC client libraries.

library_generation/utilities.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ extract_folder_name() {
1111

1212
remove_empty_files() {
1313
local category=$1
14+
local file_num
1415
find "${destination_path}/${category}-${folder_name}/src/main/java" -type f -size 0 | while read -r f; do rm -f "${f}"; done
16+
# remove the directory if the directory has no files.
17+
file_num=$(find "${destination_path}/${category}-${folder_name}" -type f | wc -l | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
18+
if [[ "${file_num}" == 0 ]]; then
19+
rm -rf "${destination_path}/${category}-${folder_name}"
20+
fi
21+
1522
if [ -d "${destination_path}/${category}-${folder_name}/src/main/java/samples" ]; then
1623
mv "${destination_path}/${category}-${folder_name}/src/main/java/samples" "${destination_path}/${category}-${folder_name}"
1724
fi

0 commit comments

Comments
 (0)