chain-client-sdk-dart/tools/generate.sh
flemming-it da10820959 feat: live HubClient with generated proto bindings
Proto codegen wired in. tools/generate.sh wraps protoc against
../fai_platform/proto/, including the well-known types (Empty,
Struct, Timestamp) so they are generated alongside fai's own
messages — protobuf 4.x doesn't ship them as importable Dart
types, so we have to generate them ourselves.

HubClient now exposes typed methods backed by real RPCs:
- healthy() — Hub.Health probe (returns false on connect refused).
- listCapabilities() — HubAdmin.ListCapabilities.
- eventLog() — HubAdmin.EventLog with type filter.
- listApprovals() — HubAdmin.ListApprovals (status filter).
- approve() / reject() — HubAdmin.DecideApproval.

Generated bindings under lib/src/generated/ are excluded from
analyze (out of our style control). protoc_plugin pinned to
22.2.0 in tools/generate.sh comment because newer plugins
generate code against protobuf 6.x, while grpc 4.x still
constrains us to protobuf 4.x.

Bumps fai_dart_sdk 0.1.0 -> 0.2.0.

Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
2026-05-05 16:24:16 +02:00

97 lines
2.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Generate Dart bindings for the F∆I gRPC surface.
#
# Reads .proto files from the sibling fai_platform repo and
# writes Dart code into lib/src/generated/. Run after
# `dart pub global activate protoc_plugin` so that
# `protoc-gen-dart` is on PATH (`$HOME/.pub-cache/bin`).
#
# Usage:
# tools/generate.sh
#
# Env overrides:
# PROTO_ROOT Path to fai_platform/proto/ (default: ../fai_platform/proto)
# OUT_DIR Output dir (default: lib/src/generated)
set -euo pipefail
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
SDK_ROOT="$( cd -- "$SCRIPT_DIR/.." && pwd )"
PROTO_ROOT="${PROTO_ROOT:-$SDK_ROOT/../fai_platform/proto}"
OUT_DIR="${OUT_DIR:-$SDK_ROOT/lib/src/generated}"
if [[ ! -d "$PROTO_ROOT" ]]; then
echo "error: PROTO_ROOT does not exist: $PROTO_ROOT" >&2
echo "Set PROTO_ROOT to the fai_platform/proto directory." >&2
exit 1
fi
if ! command -v protoc >/dev/null 2>&1; then
echo "error: protoc not found on PATH" >&2
echo "Install: brew install protobuf (macOS)" >&2
exit 1
fi
# Make sure protoc-gen-dart is reachable. pub-cache/bin is the
# canonical location after `dart pub global activate protoc_plugin`.
export PATH="$HOME/.pub-cache/bin:$PATH"
if ! command -v protoc-gen-dart >/dev/null 2>&1; then
echo "error: protoc-gen-dart not found" >&2
echo "Install: dart pub global activate protoc_plugin" >&2
exit 1
fi
mkdir -p "$OUT_DIR"
# Find every proto file under the package root and pass them to
# protoc with --dart_out for messages and grpc=true for services.
PROTO_FILES=()
while IFS= read -r -d '' file; do
PROTO_FILES+=("$file")
done < <(find "$PROTO_ROOT" -name '*.proto' -print0)
if [[ ${#PROTO_FILES[@]} -eq 0 ]]; then
echo "error: no .proto files found under $PROTO_ROOT" >&2
exit 1
fi
# Locate the well-known-types include directory. Required so we
# can generate Dart bindings for google.protobuf.Empty,
# google.protobuf.Struct, etc. that the platform's protos import.
WKT_INCLUDE=""
for candidate in \
"/opt/homebrew/include" \
"/usr/local/include" \
"/usr/include"; do
if [[ -f "$candidate/google/protobuf/empty.proto" ]]; then
WKT_INCLUDE="$candidate"
break
fi
done
if [[ -z "$WKT_INCLUDE" ]]; then
echo "error: google/protobuf/empty.proto not found in standard include dirs" >&2
echo "Install protobuf (brew install protobuf) so the well-known types ship alongside protoc." >&2
exit 1
fi
WKT_PROTOS=(
"$WKT_INCLUDE/google/protobuf/empty.proto"
"$WKT_INCLUDE/google/protobuf/struct.proto"
"$WKT_INCLUDE/google/protobuf/timestamp.proto"
)
echo "generating Dart bindings"
echo " protos: ${#PROTO_FILES[@]} files under $PROTO_ROOT"
echo " WKT include: $WKT_INCLUDE"
echo " output: $OUT_DIR"
protoc \
--proto_path="$PROTO_ROOT" \
--proto_path="$WKT_INCLUDE" \
--dart_out=grpc:"$OUT_DIR" \
"${PROTO_FILES[@]}" \
"${WKT_PROTOS[@]}"
echo "done. generated files:"
find "$OUT_DIR" -name '*.dart' | sort