diff --git a/library_generation/README.md b/library_generation/README.md index 13a2d06114..19f033e0a0 100644 --- a/library_generation/README.md +++ b/library_generation/README.md @@ -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`. diff --git a/library_generation/generate_library.sh b/library_generation/generate_library.sh index 9863a6170e..47931231ae 100755 --- a/library_generation/generate_library.sh +++ b/library_generation/generate_library.sh @@ -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 @@ -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 @@ -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 diff --git a/library_generation/test/generate_library_integration_test.sh b/library_generation/test/generate_library_integration_test.sh index 6697e9f433..910c22415b 100755 --- a/library_generation/test/generate_library_integration_test.sh +++ b/library_generation/test/generate_library_integration_test.sh @@ -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}") @@ -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}" \ diff --git a/library_generation/test/resources/proto_path_list.txt b/library_generation/test/resources/proto_path_list.txt index 7a3973612b..3075927c66 100755 --- a/library_generation/test/resources/proto_path_list.txt +++ b/library_generation/test/resources/proto_path_list.txt @@ -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 diff --git a/library_generation/test/test_utilities.sh b/library_generation/test/test_utilities.sh index 761df77cb3..5620ecd49d 100755 --- a/library_generation/test/test_utilities.sh +++ b/library_generation/test/test_utilities.sh @@ -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. diff --git a/library_generation/utilities.sh b/library_generation/utilities.sh index 1b97f0745f..8897b5c425 100755 --- a/library_generation/utilities.sh +++ b/library_generation/utilities.sh @@ -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