1#!/bin/bash -e 2 3# Run gapic-generator-java as a protoc plugin. 4 5# Usage example here: 6: << 'EXAMPLE' 7 DIR=~/dev/googleapis/google/showcase/v1beta1 8 ./run.sh --g ~/dev/googleapis --p "$DIR"-s "$DIR/showcase_grpc_service_config.json" 9EXAMPLE 10 11source gbash.sh 12 13# Required flags. 14DEFINE_string --alias=p protos '' 'Path to the protos to generate.' 15DEFINE_string --alias=g googleapis '' 'Path to the googleapis directory.' 16DEFINE_string --alias=s service_config '' 'Path to the JSON service config' 17 18# Optional flags. 19DEFINE_bool --alias=c use_cached false 'If true, does not rebuild the plugin.' 20DEFINE_string --alias=o out '/tmp/test' 'Output directory' 21DEFINE_string gapic_config '' 'Path to the config ending in gapic.yaml. Optional' 22 23gbash::init_google "$@" 24 25# Variables. 26PROTOC_INCLUDE_DIR=/usr/local/include/google/protobuf 27 28function echo_error { 29 BOLD="\e[1m" 30 UNSET="\e[0m" 31 WHITE="\e[97m" 32 RED="\e[91m" 33 BACK_MAGENTA="\e[45m" 34 BACK_BLUE="\e[44m" 35 BACK_RED="\e[41m" 36 echo -e "$BOLD $BACK_BLUE $WHITE $1 $UNSET" 37} 38 39function echo_success { 40 BOLD="\e[1m" 41 UNSET="\e[0m" 42 WHITE="\e[97m" 43 BACK_GREEN="\e[42m" 44 BACK_BLUE="\e[44m" 45 echo -e "$BOLD $BACK_BLUE $WHITE $BACK_GREEN $1 $UNSET" 46} 47 48# Flag check. 49if [[ -z "${FLAGS_protos}" ]] 50then 51 echo_error "Required flag --protos must be set." 52 exit 1 53fi 54 55if [[ -z "${FLAGS_googleapis}" ]] 56then 57 echo_error "Required flag --googleapis must be set." 58 exit 1 59fi 60 61# Build if needed. 62if [[ "${FLAGS_use_cached}" == 0 ]] || [[ ! -f bazel-bin/protoc-gen-java_gapic ]] 63then 64 echo_success "Rebuilding the microgenerator..." 65 bazel build :protoc-gen-java_gapic 66 if [[ $? -ne 0 ]] 67 then 68 echo_error "Build failed." 69 exit 1 70 fi 71 72 echo_success "Done" 73fi 74 75# Key values are synced to rules_java_gapic/java_gapic.bzl. 76SERVICE_CONFIG_OPT="" 77if [ -n "$FLAGS_service_config" ] 78then 79 SERVICE_CONFIG_OPT="grpc-service-config=$FLAGS_service_config" 80fi 81 82GAPIC_CONFIG_OPT="" 83if [ -n "$FLAGS_gapic_config" ] 84then 85 GAPIC_CONFIG_OPT="gapic-config=$FLAGS_gapic_config" 86fi 87 88# Run protoc. 89protoc -I="${PROTOC_INCLUDE_DIR}" -I="${FLAGS_googleapis}" -I="${FLAGS_protos}" \ 90 -I="${FLAGS_googleapis}/google/longrunning" \ 91 --include_source_info \ 92 --plugin=bazel-bin/protoc-gen-java_gapic ${FLAGS_protos}/*.proto \ 93 --java_gapic_out="${FLAGS_out}" \ 94 --java_gapic_opt="${SERVICE_CONFIG_OPT},${GAPIC_CONFIG_OPT}" \ 95 --experimental_allow_proto3_optional 96 97echo_success "Output files written to ${FLAGS_out}" 98