Skip to content

Commit

Permalink
teach protoc-gen-ruby PB_{DUMP,LOAD}_REQUEST_BYTES
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
nerdrew authored and embark committed Jul 12, 2017
1 parent 27bb8e2 commit d06fa28
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion bin/protoc-gen-ruby
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ require 'protobuf/code_generator'
STDIN.binmode
STDOUT.binmode

request_bytes = STDIN.read
if ENV['PB_LOAD_REQUEST_BYTES']
request_bytes = File.read(ENV['PB_LOAD_REQUEST_BYTES'])
else
request_bytes = STDIN.read
end
if ENV['PB_DUMP_REQUEST_BYTES']
dump_file = ENV['PB_DUMP_REQUEST_BYTES']
unless File.directory?(File.dirname(dump_file))
warn "PB_DUMP_REQUEST_BYTES=#{dump_file.inspect} is not a valid path for the request bytes"
warn "Usage: PB_DUMP_REQUEST_BYTES=/path/to/desired/dump/file.bin protoc blah.proto"
exit 1
end
File.open(dump_file, 'w') do |f|
f.print(request_bytes)
end
exit 1
end
code_generator = ::Protobuf::CodeGenerator.new(request_bytes)
code_generator.eval_unknown_extensions!
STDOUT.print(code_generator.response_bytes)

0 comments on commit d06fa28

Please sign in to comment.