meshtastic: support
This commit is contained in:
46
protocol/meshtastic/internal/pbgen/README.md
Normal file
46
protocol/meshtastic/internal/pbgen/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Meshtastic Protocol Buffers Generation
|
||||
|
||||
This internal package manages the generation of Go types from the [official Meshtastic protocol buffer definitions](https://github.com/meshtastic/protobufs).
|
||||
|
||||
## Upstream Source
|
||||
|
||||
The protobuf definitions are included as a git submodule at:
|
||||
|
||||
```
|
||||
protocol/meshtastic/internal/pbgen/upstream
|
||||
```
|
||||
|
||||
Tracked repository: `https://github.com/meshtastic/protobufs.git`
|
||||
|
||||
## Generating Go Code
|
||||
|
||||
To regenerate the Go protobuf types after updating the submodule:
|
||||
|
||||
```bash
|
||||
# Update the submodule to latest (optional)
|
||||
git submodule update --remote protocol/meshtastic/internal/pbgen/upstream
|
||||
|
||||
# Regenerate Go code
|
||||
go generate ./protocol/meshtastic/internal/pbgen
|
||||
```
|
||||
|
||||
### Requirements
|
||||
|
||||
- `protoc` (Protocol Buffers compiler)
|
||||
- `protoc-gen-go` (install via `go install google.golang.org/protobuf/cmd/protoc-gen-go@latest`)
|
||||
|
||||
## Generated Package
|
||||
|
||||
Generated Go types are placed in:
|
||||
|
||||
```
|
||||
protocol/meshtastic/pb/
|
||||
```
|
||||
|
||||
Import path:
|
||||
|
||||
```go
|
||||
import meshtasticpb "git.maze.io/go/ham/protocol/meshtastic/pb"
|
||||
```
|
||||
|
||||
All Meshtastic protocol buffer messages (Position, User, Data, etc.) are available from this package.
|
||||
5
protocol/meshtastic/internal/pbgen/generate.go
Normal file
5
protocol/meshtastic/internal/pbgen/generate.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package protobufs
|
||||
|
||||
//go:generate sh ./generate.sh
|
||||
|
||||
const _ = 0
|
||||
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
|
||||
1
protocol/meshtastic/internal/pbgen/upstream
Submodule
1
protocol/meshtastic/internal/pbgen/upstream
Submodule
Submodule protocol/meshtastic/internal/pbgen/upstream added at 2edc5ab7b1
Reference in New Issue
Block a user