Skip to content

Commit 047dd58

Browse files
committed
Add barebones example of a custom protoc plugin that writes a bash script
1 parent beb8a61 commit 047dd58

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.PHONY: build gen
2+
3+
build:
4+
@go build -o ./build/ .
5+
6+
gen: build
7+
@mkdir -p ./pb
8+
@protoc \
9+
--plugin=./build/protoc-gen-list-messages \
10+
-I../proto \
11+
--list-messages_out=./pb \
12+
--list-messages_opt=paths=source_relative \
13+
../proto/echo/v1/*.proto
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "google.golang.org/protobuf/compiler/protogen"
4+
5+
func main() {
6+
opts := protogen.Options{}
7+
opts.Run(func(gen *protogen.Plugin) error {
8+
for _, f := range gen.Files {
9+
if f.Generate {
10+
generateFile(gen, f)
11+
}
12+
}
13+
return nil
14+
})
15+
}
16+
17+
func generateFile(gen *protogen.Plugin, file *protogen.File) {
18+
filename := file.GeneratedFilenamePrefix + "_list-messages.pb.sh"
19+
g := gen.NewGeneratedFile(filename, file.GoImportPath)
20+
g.P("#!/usr/bin/env bash")
21+
g.P("set -euo pipefail")
22+
g.P()
23+
g.P("##########################################################")
24+
g.P("# Code generated by protoc-gen-list-messages. DO NOT EDIT.")
25+
g.P("##########################################################")
26+
g.P()
27+
g.P(`printf 'The messages defined in the provided proto file are:\n'`)
28+
29+
for _, msg := range file.Messages {
30+
g.P(`printf '`, msg.Desc.FullName(), `\n'`)
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
##########################################################
5+
# Code generated by protoc-gen-list-messages. DO NOT EDIT.
6+
##########################################################
7+
8+
printf 'The messages defined in the provided proto file are:\n'
9+
printf 'echo.v1.EchoRequest\n'
10+
printf 'echo.v1.EchoResponse\n'

0 commit comments

Comments
 (0)