Sha256: 8e3e9530a4af9c837ef2bb93db6fed98069fb544b76efa50f7bb2d1d677923f0

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

#!/usr/bin/env ruby

begin
  require 'rubygems'
rescue LoadError
end

require 'optparse'
require 'tempfile'

ruby_out = "."
include_dirs = []

opts = OptionParser.new
opts.banner = <<BANNER
Usage: #{opts.program_name} [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
BANNER

opts.on("-o", "--ruby_out", "=OUT_DIR",
        "generates ruby code in OUT_DIR", "[defaults to pwd]",
        String) { |val| ruby_out = val }
opts.on("-I", "--proto_path", "=PATH",
        "Specify a directory to search for includes/imports.") { |val| include_dirs << val }
opts.on_tail("-h", "--help") { puts opts; exit }

rest = opts.parse(ARGV)
filenames = rest

(puts "Missing input file.\n\n"; puts opts; exit) if filenames.empty?

require 'protocol_buffers'
require 'protocol_buffers/compiler'

protocfile = Tempfile.new("ruby-protoc")
protocfile.binmode
ProtocolBuffers::Compiler.compile(protocfile.path, filenames, :include_dirs => include_dirs)
descriptor_set = FileDescriptorSet.parse(protocfile)
protocfile.close(true)

if ruby_out
  require 'protocol_buffers/compiler/file_descriptor_to_ruby'
end

descriptor_set.file.each do |file_descriptor|

  if ruby_out
    path = File.join(ruby_out,
                     File.basename(file_descriptor.name, ".proto") + ".pb.rb")
    File.open(path, "wb") do |file|
      dumper = FileDescriptorToRuby.new(file_descriptor)
      dumper.write(file)
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-protocol-buffers-1.0.0 bin/ruby-protoc
ruby-protocol-buffers-0.8.5 bin/ruby-protoc