Sha256: d5f29fd93073bb15ce13596d7218eb067f468761f63ba3a7803e83a97999468b

Contents?: true

Size: 1.77 KB

Versions: 17

Compression:

Stored size: 1.77 KB

Contents

require 'fileutils'
require 'protobuf/compiler/proto_parser'
require 'protobuf/compiler/nodes'
require 'protobuf/compiler/visitors'

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

    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

    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 = Visitor::CreateMessageVisitor.new(proto_file, proto_dir, out_dir)
      File.open(proto_path) do |file|
        message_visitor.visit(ProtoParser.new.parse(file))
      end
      message_visitor.create_files(rb_file, out_dir, file_create)
    end

    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 = Visitor::CreateRpcVisitor.new
      File.open(proto_path) do |file|
        rpc_visitor.visit(ProtoParser.new.parse(file))
      end
      rpc_visitor.create_files(message_file, out_dir, file_create)
    end

    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, "File does not exist: #{path}"
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
protobuf-1.4.2 lib/protobuf/compiler/compiler.rb
protobuf-1.4.1 lib/protobuf/compiler/compiler.rb
protobuf-1.4.0 lib/protobuf/compiler/compiler.rb
protobuf-1.3.0 lib/protobuf/compiler/compiler.rb
protobuf-1.1.3 lib/protobuf/compiler/compiler.rb
protobuf-1.1.2 lib/protobuf/compiler/compiler.rb
protobuf-1.1.1 lib/protobuf/compiler/compiler.rb
protobuf-1.1.0.beta2 lib/protobuf/compiler/compiler.rb
protobuf-1.1.0.beta1 lib/protobuf/compiler/compiler.rb
protobuf-1.1.0.beta0 lib/protobuf/compiler/compiler.rb
protobuf-1.0.1 lib/protobuf/compiler/compiler.rb
protobuf-1.0.0 lib/protobuf/compiler/compiler.rb
ruby_protobuf-0.4.7 lib/protobuf/compiler/compiler.rb
ruby_protobuf-0.4.6 lib/protobuf/compiler/compiler.rb
ruby_protobuf-0.4.5 lib/protobuf/compiler/compiler.rb
ruby_protobuf-0.4.4 lib/protobuf/compiler/compiler.rb
ruby_protobuf-0.4.1 lib/protobuf/compiler/compiler.rb