Class: Protobuf::Compiler
- Inherits:
-
Object
- Object
- Protobuf::Compiler
- Defined in:
- lib/protobuf/compiler/compiler.rb
Class Method Summary collapse
Instance Method Summary collapse
- #compile(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object
- #create_message(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object
- #create_rpc(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object
- #validate_existence(path, base_dir) ⇒ Object
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) 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 27 28 29 30 31 |
# File 'lib/protobuf/compiler/compiler.rb', line 17 def (proto_file, proto_dir='.', out_dir='.', file_create=true) out_dir.sub! %r{/$}, '' proto_dir.sub! %r{/$}, '' rb_file = if proto_file =~ %r{^/} then "#{out_dir}/#{proto_file.split('/').last.sub(/\.proto$/, '')}.pb.rb" else "#{out_dir}/#{proto_file.sub(/\.proto$/, '')}.pb.rb" end proto_path = validate_existence proto_file, proto_dir = Protobuf::Visitor::CreateMessageVisitor.new proto_file, proto_dir, out_dir File.open proto_path, 'r' do |file| .visit Protobuf::ProtoParser.new.parse(file) end .create_files rb_file, out_dir, file_create end |
#create_rpc(proto_file, proto_dir = '.', out_dir = '.', file_create = true) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/protobuf/compiler/compiler.rb', line 33 def create_rpc(proto_file, proto_dir='.', out_dir='.', file_create=true) = "#{out_dir}/#{proto_file.sub(/\.proto$/, '')}.pb.rb" out_dir = "#{out_dir}/#{File.dirname proto_file}" 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 , out_dir, file_create end |
#validate_existence(path, base_dir) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/protobuf/compiler/compiler.rb', line 45 def validate_existence(path, base_dir) if File.exist? path elsif File.exist?(path = "#{base_dir or '.'}/#{path}") else raise ArgumentError.new("File does not exist: #{path}") end path end |