Module: ProtocolBuffers::Compiler

Defined in:
lib/protocol_buffers/compiler.rb

Class Method Summary collapse

Class Method Details

.compile(output_filename, input_files, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/protocol_buffers/compiler.rb', line 7

def self.compile(output_filename, input_files, opts = {})
  input_files = Array(input_files) unless input_files.is_a?(Array)
  raise(ArgumentError, "Need at least one input file") if input_files.empty?
  other_opts = ""
  (opts[:include_dirs] || []).each { |d| other_opts += " -I#{d}" }
  input_files.each { |f| other_opts += " -I#{File.dirname(f)}" }

  cmd = "protoc #{other_opts} -o#{output_filename} #{input_files.join(' ')}"
  rc = system(cmd)
  raise(CompileError, $?.exitstatus.to_s) unless rc
  true
end

.compile_and_load(input_files, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/protocol_buffers/compiler.rb', line 20

def self.compile_and_load(input_files, opts = {})
  require 'tempfile'
  require 'protocol_buffers/compiler/file_descriptor_to_ruby'

  tempfile = Tempfile.new("protocol_buffers_spec")
  tempfile.binmode
  compile(tempfile.path, input_files, opts)
  descriptor_set = FileDescriptorSet.parse(tempfile)
  tempfile.close(true)
  descriptor_set.file.each do |file|
    parsed = FileDescriptorToRuby.new(file)
    output = Tempfile.new("protocol_buffers_spec_parsed")
    output.binmode
    parsed.write(output)
    output.flush
    load output.path
    output.close(true)
  end
  true
end

.compile_and_load_string(input, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/protocol_buffers/compiler.rb', line 41

def self.compile_and_load_string(input, opts = {})
  require 'tempfile'
  tempfile = Tempfile.new("protocol_buffers_load_string")
  tempfile.binmode
  tempfile.write(input)
  tempfile.flush
  (opts[:include_dirs] ||= []) << File.dirname(tempfile.path)
  compile_and_load(tempfile.path, opts)
end