Class: Protobuf::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/compiler/compiler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object



8
9
10
# File 'lib/protobuf/compiler/compiler.rb', line 8

def self.compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
  self.new.compile proto_file, proto_dir, out_dir, file_create
end

Instance Method Details

#compile(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object



12
13
14
15
# File 'lib/protobuf/compiler/compiler.rb', line 12

def compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
  create_message proto_file, proto_dir, out_dir, file_create
  create_rpc proto_file, proto_dir, out_dir, file_create
end

#create_message(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/protobuf/compiler/compiler.rb', line 17

def create_message(proto_file, proto_dir='.', out_dir='.', file_create=true)
  rb_file = File.join(out_dir, File.basename(proto_file).sub(/\.[^\0]*\z/, '') + '.pb.rb')
  proto_path = validate_existence proto_file, proto_dir

  message_visitor = Protobuf::Visitor::CreateMessageVisitor.new proto_file, proto_dir, out_dir
  File.open proto_path, 'r' do |file|
    message_visitor.visit Protobuf::ProtoParser.new.parse(file)
  end
  message_visitor.create_files rb_file, out_dir, file_create
end

#create_rpc(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/protobuf/compiler/compiler.rb', line 28

def create_rpc(proto_file, proto_dir='.', out_dir='.', file_create=true)
  message_file = File.join(out_dir, File.basename(proto_file).sub(/\.[^\0]*\z/, '') + '.pb.rb')
  proto_path = validate_existence proto_file, proto_dir

  rpc_visitor = Protobuf::Visitor::CreateRpcVisitor.new
  File.open proto_path, 'r' do |file|
    rpc_visitor.visit Protobuf::ProtoParser.new.parse(file)
  end
  rpc_visitor.create_files message_file, out_dir, file_create
end

#validate_existence(path, base_dir) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protobuf/compiler/compiler.rb', line 39

def validate_existence(path, base_dir)
  if File.exist? path
    path
  else
    newpath = File.join(base_dir, path)
    if File.exist? newpath
      newpath
    else
      raise ArgumentError.new("File does not exist: #{path}")
    end
  end
end