From c42e8003aeada4d3b8e128e4a051a5acd2791f14 Mon Sep 17 00:00:00 2001 From: Andrew Lazarus Date: Wed, 27 Jan 2016 13:34:54 -0800 Subject: [PATCH] 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 --- bin/protoc-gen-ruby | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/bin/protoc-gen-ruby b/bin/protoc-gen-ruby index a84b1c1c..c15246dd 100755 --- a/bin/protoc-gen-ruby +++ b/bin/protoc-gen-ruby @@ -16,7 +16,23 @@ require 'protobuf/code_generator' STDIN.binmode STDOUT.binmode -request_bytes = STDIN.read +request_bytes = if ENV['PB_LOAD_REQUEST_BYTES'] + File.read(ENV['PB_LOAD_REQUEST_BYTES']) + else + 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)