diff --git a/library_generation/test/generate_library_integration_test.sh b/library_generation/test/generate_library_integration_test.sh index 96429c6e43..a3a498bec2 100755 --- a/library_generation/test/generate_library_integration_test.sh +++ b/library_generation/test/generate_library_integration_test.sh @@ -16,9 +16,9 @@ set -xeo pipefail googleapis_gen_url="git@github.com:googleapis/googleapis-gen.git" script_dir=$(dirname "$(readlink -f "$0")") proto_path_list="${script_dir}/resources/proto_path_list.txt" -source "${script_dir}/../utilities.sh" library_generation_dir="${script_dir}"/.. -output_folder="$(get_output_folder)" +source "${script_dir}/test_utilities.sh" +output_folder="$(pwd)/output" while [[ $# -gt 0 ]]; do key="$1" @@ -39,8 +39,6 @@ esac shift # past argument or value done -script_dir=$(dirname "$(readlink -f "$0")") -source "${script_dir}/../utilities.sh" library_generation_dir="${script_dir}"/.. mkdir -p "${output_folder}" pushd "${output_folder}" diff --git a/library_generation/test/generate_library_unit_tests.sh b/library_generation/test/generate_library_unit_tests.sh index 4af6da36ec..179002b785 100755 --- a/library_generation/test/generate_library_unit_tests.sh +++ b/library_generation/test/generate_library_unit_tests.sh @@ -290,18 +290,6 @@ get_version_from_valid_WORKSPACE_test() { assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}" } -get_generator_version_from_valid_versions_txt_test() { - versions_file="${script_dir}/resources/misc/testversions.txt" - obtained_ggj_version=$(get_version_from_versions_txt "${versions_file}" "gapic-generator-java") - assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}" -} - -get_gax_version_from_valid_versions_txt_test() { - versions_file="${script_dir}/resources/misc/testversions.txt" - obtained_gax_version=$(get_version_from_versions_txt "${versions_file}" "gax") - assertEquals '2.33.1-SNAPSHOT' "${obtained_gax_version}" -} - # Execute tests. # One line per test. test_list=( @@ -340,8 +328,6 @@ test_list=( get_include_samples_from_BUILD_false_test get_include_samples_from_BUILD_empty_test get_version_from_valid_WORKSPACE_test - get_generator_version_from_valid_versions_txt_test - get_gax_version_from_valid_versions_txt_test ) pushd "${script_dir}" diff --git a/library_generation/test/test_utilities.sh b/library_generation/test/test_utilities.sh index 1f3f022a1b..761df77cb3 100755 --- a/library_generation/test/test_utilities.sh +++ b/library_generation/test/test_utilities.sh @@ -10,7 +10,7 @@ succeed_num=0 failed_num=0 failed_tests="" -# Helper functions, they shouldn't be called outside this file. +############# Helper functions, they shouldn't be called outside this file ############# __test_executed() { total_num=$((1 + total_num)) } @@ -25,6 +25,53 @@ __test_failed() { failed_tests="${failed_tests} ${failed_test}" } +# Used to obtain configuration values from a bazel BUILD file +# +# inspects a $build_file for a certain $rule (e.g. java_gapic_library). If the +# first 15 lines after the declaration of the rule contain $pattern, then +# it will return $if_match if $pattern is found, otherwise $default +__get_config_from_BUILD() { + build_file=$1 + rule=$2 + pattern=$3 + default=$4 + if_match=$5 + + result="${default}" + if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then + result="${if_match}" + fi + echo "${result}" +} + +__get_iam_policy_from_BUILD() { + local build_file=$1 + local contains_iam_policy + contains_iam_policy=$(__get_config_from_BUILD \ + "${build_file}" \ + "proto_library_with_info(" \ + "//google/iam/v1:iam_policy_proto" \ + "false" \ + "true" + ) + echo "${contains_iam_policy}" +} + +__get_locations_from_BUILD() { + local build_file=$1 + local contains_locations + contains_locations=$(__get_config_from_BUILD \ + "${build_file}" \ + "proto_library_with_info(" \ + "//google/cloud/location:location_proto" \ + "false" \ + "true" + ) + echo "${contains_locations}" +} + +############# Functions used in test execution ############# + assertEquals() { local expected=$1 local actual=$2 @@ -77,3 +124,102 @@ execute_tests() { echo "Failed test(s): ${failed_tests}." exit 1 } + +############# Utility functions used in `generate_library_integration_tests.sh` ############# + +# Apart from proto files in proto_path, additional protos are needed in order +# to generate GAPIC client libraries. +# In most cases, these protos should be within google/ directory, which is +# pulled from googleapis as a prerequisite. +# Get additional protos in BUILD.bazel. +get_gapic_additional_protos_from_BUILD() { + local build_file=$1 + local gapic_additional_protos="google/cloud/common_resources.proto" + if [[ $(__get_iam_policy_from_BUILD "${build_file}") == "true" ]]; then + gapic_additional_protos="${gapic_additional_protos} google/iam/v1/iam_policy.proto" + fi + if [[ $(__get_locations_from_BUILD "${build_file}") == "true" ]]; then + gapic_additional_protos="${gapic_additional_protos} google/cloud/location/locations.proto" + fi + echo "${gapic_additional_protos}" +} + +get_transport_from_BUILD() { + local build_file=$1 + local transport + transport=$(__get_config_from_BUILD \ + "${build_file}" \ + "java_gapic_library(" \ + "grpc+rest" \ + "grpc" \ + "grpc+rest" + ) + # search again because the transport maybe `rest`. + transport=$(__get_config_from_BUILD \ + "${build_file}" \ + "java_gapic_library(" \ + "transport = \"rest\"" \ + "${transport}" \ + "rest" + ) + echo "${transport}" +} + +get_rest_numeric_enums_from_BUILD() { + local build_file=$1 + local rest_numeric_enums + rest_numeric_enums=$(__get_config_from_BUILD \ + "${build_file}" \ + "java_gapic_library(" \ + "rest_numeric_enums = True" \ + "false" \ + "true" + ) + echo "${rest_numeric_enums}" +} + +get_include_samples_from_BUILD() { + local build_file=$1 + local include_samples + include_samples=$(__get_config_from_BUILD \ + "${build_file}" \ + "java_gapic_assembly_gradle_pkg(" \ + "include_samples = True" \ + "false" \ + "true" + ) + echo "${include_samples}" +} + +# Obtains a version from a bazel WORKSPACE file +# +# versions look like "_ggj_version="1.2.3" +# It will return 1.2.3 for such example +get_version_from_WORKSPACE() { + version_key_word=$1 + workspace=$2 + version=$(\ + grep "${version_key_word}" "${workspace}" |\ + head -n 1 |\ + sed 's/\(.*\) = "\(.*\)"\(.*\)/\2/' |\ + sed 's/[a-zA-Z-]*//' + ) + echo "${version}" +} + +# Convenience function to clone only the necessary folders from a git repository +sparse_clone() { + repo_url=$1 + paths=$2 + commitish=$3 + clone_dir=$(basename "${repo_url%.*}") + rm -rf "${clone_dir}" + git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}" + pushd "${clone_dir}" + if [ -n "${commitish}" ]; then + git checkout "${commitish}" + fi + git sparse-checkout set --no-cone ${paths} + git checkout + popd +} diff --git a/library_generation/utilities.sh b/library_generation/utilities.sh index 2c90cb1554..88d3a13657 100755 --- a/library_generation/utilities.sh +++ b/library_generation/utilities.sh @@ -2,55 +2,7 @@ set -xeo pipefail - -# private functions that should not be called outside this file. - -# Used to obtain configuration values from a bazel BUILD file -# -# inspects a $build_file for a certain $rule (e.g. java_gapic_library). If the -# first 15 lines after the declaration of the rule contain $pattern, then -# it will return $if_match if $pattern is found, otherwise $default -__get_config_from_BUILD() { - build_file=$1 - rule=$2 - pattern=$3 - default=$4 - if_match=$5 - - result="${default}" - if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then - result="${if_match}" - fi - echo "${result}" -} - -__get_iam_policy_from_BUILD() { - local build_file=$1 - local contains_iam_policy - contains_iam_policy=$(__get_config_from_BUILD \ - "${build_file}" \ - "proto_library_with_info(" \ - "//google/iam/v1:iam_policy_proto" \ - "false" \ - "true" - ) - echo "${contains_iam_policy}" -} - -__get_locations_from_BUILD() { - local build_file=$1 - local contains_locations - contains_locations=$(__get_config_from_BUILD \ - "${build_file}" \ - "proto_library_with_info(" \ - "//google/cloud/location:location_proto" \ - "false" \ - "true" - ) - echo "${contains_locations}" -} - -# define utility functions +# Utility functions used in `generate_library.sh` and showcase generation. extract_folder_name() { local destination_path=$1 local folder_name=${destination_path##*/} @@ -236,111 +188,6 @@ download_fail() { exit 1 } -# Obtains a version from a bazel WORKSPACE file -# -# versions look like "_ggj_version="1.2.3" -# It will return 1.2.3 for such example -get_version_from_WORKSPACE() { - version_key_word=$1 - workspace=$2 - version=$(\ - grep "${version_key_word}" "${workspace}" |\ - head -n 1 |\ - sed 's/\(.*\) = "\(.*\)"\(.*\)/\2/' |\ - sed 's/[a-zA-Z-]*//' - ) - echo "${version}" -} - -# Apart from proto files in proto_path, additional protos are needed in order -# to generate GAPIC client libraries. -# In most cases, these protos should be within google/ directory, which is -# pulled from googleapis as a prerequisite. -# Get additional protos in BUILD.bazel. -get_gapic_additional_protos_from_BUILD() { - local build_file=$1 - local gapic_additional_protos="google/cloud/common_resources.proto" - if [[ $(__get_iam_policy_from_BUILD "${build_file}") == "true" ]]; then - gapic_additional_protos="${gapic_additional_protos} google/iam/v1/iam_policy.proto" - fi - if [[ $(__get_locations_from_BUILD "${build_file}") == "true" ]]; then - gapic_additional_protos="${gapic_additional_protos} google/cloud/location/locations.proto" - fi - echo "${gapic_additional_protos}" -} - -get_transport_from_BUILD() { - local build_file=$1 - local transport - transport=$(__get_config_from_BUILD \ - "${build_file}" \ - "java_gapic_library(" \ - "grpc+rest" \ - "grpc" \ - "grpc+rest" - ) - # search again because the transport maybe `rest`. - transport=$(__get_config_from_BUILD \ - "${build_file}" \ - "java_gapic_library(" \ - "transport = \"rest\"" \ - "${transport}" \ - "rest" - ) - echo "${transport}" -} - -get_rest_numeric_enums_from_BUILD() { - local build_file=$1 - local rest_numeric_enums - rest_numeric_enums=$(__get_config_from_BUILD \ - "${build_file}" \ - "java_gapic_library(" \ - "rest_numeric_enums = True" \ - "false" \ - "true" - ) - echo "${rest_numeric_enums}" -} - -get_include_samples_from_BUILD() { - local build_file=$1 - local include_samples - include_samples=$(__get_config_from_BUILD \ - "${build_file}" \ - "java_gapic_assembly_gradle_pkg(" \ - "include_samples = True" \ - "false" \ - "true" - ) - echo "${include_samples}" -} - -# Convenience function to clone only the necessary folders from a git repository -sparse_clone() { - repo_url=$1 - paths=$2 - commitish=$3 - clone_dir=$(basename "${repo_url%.*}") - rm -rf "${clone_dir}" - git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}" - pushd "${clone_dir}" - if [ -n "${commitish}" ]; then - git checkout "${commitish}" - fi - git sparse-checkout set --no-cone ${paths} - git checkout - popd -} - -# takes a versions.txt file and returns its version -get_version_from_versions_txt() { - versions=$1 - key=$2 - version=$(grep "$key:" "${versions}" | cut -d: -f3) # 3rd field is snapshot - echo "${version}" -} - # gets the output folder where all sources and dependencies will be located. It # relies on utilities_script_dir which points to the same location as # `generate_library.sh` diff --git a/showcase/scripts/generate_showcase.sh b/showcase/scripts/generate_showcase.sh index 1b25e3ae68..26ffdd7a45 100755 --- a/showcase/scripts/generate_showcase.sh +++ b/showcase/scripts/generate_showcase.sh @@ -7,12 +7,20 @@ set -ex readonly SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) lib_gen_scripts_dir="${SCRIPT_DIR}/../../library_generation/" -source "${lib_gen_scripts_dir}/utilities.sh" +source "${lib_gen_scripts_dir}/test/test_utilities.sh" readonly perform_cleanup=$1 cd "${SCRIPT_DIR}" mkdir -p "${SCRIPT_DIR}/output" +# takes a versions.txt file and returns its version +get_version_from_versions_txt() { + versions=$1 + key=$2 + version=$(grep "$key:" "${versions}" | cut -d: -f3) # 3rd field is snapshot + echo "${version}" +} + # clone gapic-showcase if [ ! -d schema ]; then if [ -d gapic-showcase ]; then @@ -37,7 +45,6 @@ if [ ! -d google ];then rm -rdf googleapis fi - ggj_version=$(get_version_from_versions_txt ../../versions.txt "gapic-generator-java") gapic_additional_protos="google/iam/v1/iam_policy.proto google/cloud/location/locations.proto" rest_numeric_enums="false"