Skip to content

Commit c42e800

Browse files
nerdrewembark
authored andcommitted
teach protoc-gen-ruby PB_{DUMP,LOAD}_REQUEST_BYTES
`PB_DUMP_REQUEST_BYTES=foo.bin protoc foo.proto --plugin=protoc-gen-gem=bin/protoc-gen-ruby --gem_out=.` will dump the raw proto request bytes generated by protoc for foo.proto to foo.bin `PB_LOAD_REQUEST_BYTES=foo.bin bin/protoc-gen-ruby` will read the raw request bytes from foo.bin and print the generated code (actually it is the code gen response proto, but it is still readable) to STDOUT
1 parent 27bb8e2 commit c42e800

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

bin/protoc-gen-ruby

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,23 @@ require 'protobuf/code_generator'
1616
STDIN.binmode
1717
STDOUT.binmode
1818

19-
request_bytes = STDIN.read
19+
request_bytes = if ENV['PB_LOAD_REQUEST_BYTES']
20+
File.read(ENV['PB_LOAD_REQUEST_BYTES'])
21+
else
22+
STDIN.read
23+
end
24+
if ENV['PB_DUMP_REQUEST_BYTES']
25+
dump_file = ENV['PB_DUMP_REQUEST_BYTES']
26+
unless File.directory?(File.dirname(dump_file))
27+
warn "PB_DUMP_REQUEST_BYTES=#{dump_file.inspect} is not a valid path for the request bytes"
28+
warn "Usage: PB_DUMP_REQUEST_BYTES=/path/to/desired/dump/file.bin protoc blah.proto"
29+
exit 1
30+
end
31+
File.open(dump_file, 'w') do |f|
32+
f.print(request_bytes)
33+
end
34+
exit 1
35+
end
2036
code_generator = ::Protobuf::CodeGenerator.new(request_bytes)
2137
code_generator.eval_unknown_extensions!
2238
STDOUT.print(code_generator.response_bytes)

0 commit comments

Comments
 (0)