#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)" API_DIR="${REPO_ROOT}/api" PROTO_DIR="./proto" PROTOC_BIN="${PROTOC:-protoc}" PROTOC_INCLUDE="${PROTOC_INCLUDE:-}" if [ -z "${PROTOC_INCLUDE}" ]; then PROTOC_INCLUDE="$("${PROTOC_BIN}" --print_include_path 2>/dev/null || true)" fi if [ -z "${PROTOC_INCLUDE}" ] || [ ! -f "${PROTOC_INCLUDE}/google/protobuf/timestamp.proto" ]; then for candidate in /usr/include /usr/local/include /usr/lib/include /usr/local/share/include "$(dirname "${PROTOC_BIN}")/../include"; do if [ -f "${candidate}/google/protobuf/timestamp.proto" ]; then PROTOC_INCLUDE="${candidate}" break fi done fi if [ -z "${PROTOC_INCLUDE}" ] || [ ! -f "${PROTOC_INCLUDE}/google/protobuf/timestamp.proto" ]; then echo "[proto] failed to locate google/protobuf well-known types; set PROTOC_INCLUDE" >&2 exit 1 fi if ! command -v "${PROTOC_BIN}" >/dev/null 2>&1; then echo "[proto] protoc binary not found" >&2 exit 1 fi if ! command -v protoc-gen-go >/dev/null 2>&1; then echo "[proto] protoc-gen-go is not installed" >&2 exit 1 fi if ! command -v protoc-gen-go-grpc >/dev/null 2>&1; then echo "[proto] protoc-gen-go-grpc is not installed" >&2 exit 1 fi info() { printf '[proto] %s\n' "$1" } generate_go() { local out_dir="$1" shift mkdir -p "${out_dir}" "${PROTOC_BIN}" \ -I="${PROTOC_INCLUDE}" \ -I="${PROTO_DIR}" \ --go_out="${out_dir}" \ --go_opt=paths=source_relative \ "$@" } generate_go_with_grpc() { local out_dir="$1" shift mkdir -p "${out_dir}" "${PROTOC_BIN}" \ -I="${PROTOC_INCLUDE}" \ -I="${PROTO_DIR}" \ --go_out="${out_dir}" \ --go_opt=paths=source_relative \ --go-grpc_out="${out_dir}" \ --go-grpc_opt=paths=source_relative \ "$@" } clean_pb_files() { local target="$1" if [ -d "${target}" ]; then find "${target}" -type f -name '*.pb.go' -delete fi } cd "${API_DIR}" # Messaging / notification protos (top-level files). mapfile -t messaging_protos < <(find "${PROTO_DIR}" -maxdepth 1 -type f -name '*.proto' | sort) if [ "${#messaging_protos[@]}" -gt 0 ]; then info "Compiling messaging protos" rm -rf ./pkg/messaging/internal/generated generate_go "./pkg/messaging/internal/generated" "${messaging_protos[@]}" fi # Common shared protos mapfile -t common_protos < <(find "${PROTO_DIR}/common" -type f -name '*.proto' | sort) if [ "${#common_protos[@]}" -gt 0 ]; then info "Compiling common shared protos" clean_pb_files "./pkg/proto" generate_go "./pkg/proto" "${common_protos[@]}" fi # Ledger shared protos if [ -f "${PROTO_DIR}/ledger/v1/ledger.proto" ]; then info "Compiling ledger protos" clean_pb_files "./pkg/proto/ledger" generate_go_with_grpc "./pkg/proto/ledger/v1" "${PROTO_DIR}/ledger/v1/ledger.proto" fi # Oracle shared protos if [ -f "${PROTO_DIR}/oracle/v1/oracle.proto" ]; then info "Compiling oracle protos" clean_pb_files "./pkg/proto/oracle" generate_go_with_grpc "./pkg/proto/oracle/v1" "${PROTO_DIR}/oracle/v1/oracle.proto" fi # Chain gateway shared protos if [ -f "${PROTO_DIR}/chain/gateway/v1/gateway.proto" ]; then info "Compiling chain gateway protos" clean_pb_files "./pkg/proto/chain/gateway" generate_go_with_grpc "./pkg/proto/chain/gateway/v1" "${PROTO_DIR}/chain/gateway/v1/gateway.proto" fi # Payments orchestrator shared protos if [ -f "${PROTO_DIR}/payments/orchestrator/v1/orchestrator.proto" ]; then info "Compiling payments orchestrator protos" clean_pb_files "./pkg/proto/payments/orchestrator" generate_go_with_grpc "./pkg/proto/payments/orchestrator/v1" "${PROTO_DIR}/payments/orchestrator/v1/orchestrator.proto" fi # Billing fees shared protos if [ -f "${PROTO_DIR}/billing/fees/v1/fees.proto" ]; then info "Compiling billing fees protos" clean_pb_files "./pkg/proto/billing/fees" generate_go_with_grpc "./pkg/proto/billing/fees/v1" "${PROTO_DIR}/billing/fees/v1/fees.proto" fi info "Protobuf generation completed"