meshtastic: support
This commit is contained in:
75
protocol/meshtastic/internal/pbgen/generate.sh
Normal file
75
protocol/meshtastic/internal/pbgen/generate.sh
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
|
||||
UPSTREAM_DIR="$SCRIPT_DIR/upstream"
|
||||
OUT_DIR="$SCRIPT_DIR/../../pb"
|
||||
MODULE_IMPORT="git.maze.io/go/ham/protocol/meshtastic/pb"
|
||||
|
||||
if ! command -v protoc >/dev/null 2>&1; then
|
||||
echo "error: protoc not found in PATH" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
GOBIN_DIR="$(go env GOPATH 2>/dev/null)/bin"
|
||||
if [ -n "$GOBIN_DIR" ] && [ -d "$GOBIN_DIR" ]; then
|
||||
PATH="$GOBIN_DIR:$PATH"
|
||||
export PATH
|
||||
fi
|
||||
|
||||
if ! command -v protoc-gen-go >/dev/null 2>&1; then
|
||||
echo "error: protoc-gen-go not found in PATH" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -d "$UPSTREAM_DIR/meshtastic" ]; then
|
||||
echo "error: upstream proto directory not found: $UPSTREAM_DIR/meshtastic" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PROTO_FILES=$(find "$UPSTREAM_DIR" -type f -name '*.proto' | LC_ALL=C sort)
|
||||
|
||||
if [ -z "$PROTO_FILES" ]; then
|
||||
echo "error: no proto files found in $UPSTREAM_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
M_OPTS=""
|
||||
for file in $PROTO_FILES; do
|
||||
rel="${file#$UPSTREAM_DIR/}"
|
||||
M_OPTS="$M_OPTS --go_opt=M$rel=$MODULE_IMPORT"
|
||||
done
|
||||
|
||||
rm -rf "$OUT_DIR"/*.pb.go
|
||||
mkdir -p "$OUT_DIR"
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
protoc \
|
||||
-I "$UPSTREAM_DIR" \
|
||||
--go_out="$OUT_DIR" \
|
||||
--go_opt=paths=source_relative \
|
||||
$M_OPTS \
|
||||
$(printf '%s ' $PROTO_FILES)
|
||||
|
||||
# Flatten meshtastic/ subdirectory into pb/ root
|
||||
if [ -d "$OUT_DIR/meshtastic" ]; then
|
||||
mv "$OUT_DIR/meshtastic"/*.pb.go "$OUT_DIR/"
|
||||
rmdir "$OUT_DIR/meshtastic"
|
||||
fi
|
||||
|
||||
# Fix package name from 'generated' to 'pb'
|
||||
for pbfile in "$OUT_DIR"/*.pb.go; do
|
||||
if [ -f "$pbfile" ]; then
|
||||
sed -i.bak 's/^package generated$/package pb/' "$pbfile"
|
||||
rm -f "$pbfile.bak"
|
||||
fi
|
||||
done
|
||||
|
||||
# Convert JSON tags from snake_case to camelCase
|
||||
# This converts json:"field_name,omitempty" to json:"fieldName,omitempty"
|
||||
for pbfile in "$OUT_DIR"/*.pb.go; do
|
||||
if [ -f "$pbfile" ]; then
|
||||
perl -i -pe 's/json:"([a-z0-9_]+)((?:,[^"]*)?)"/my ($f, $r) = ($1, $2); $f =~ s#_([a-z0-9])#uc($1)#ge; "json:\"$f$r\""/gex' "$pbfile"
|
||||
fi
|
||||
done
|
||||
Reference in New Issue
Block a user