Skip to content

Commit

Permalink
Vendoring protobuf gem RBS files
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Jul 13, 2020
1 parent b06f321 commit eec859a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 3 additions & 0 deletions exe/protoc-gen-rbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ translator = case backend
raise NotImplementedError
end

translator.generate_rbs!
translator.vendor_gem_rbs!

response = translator.response
print Google::Protobuf::Compiler::CodeGeneratorResponse.encode(response)
10 changes: 5 additions & 5 deletions lib/rbs_protobuf/translator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ def factory
end

def response
@response ||= generate_response()
@response ||= Google::Protobuf::Compiler::CodeGeneratorResponse.new
end

def generate_response
response = Google::Protobuf::Compiler::CodeGeneratorResponse.new

def generate_rbs!
input.proto_file.each do |file|
response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
name: rbs_name(file.name),
content: rbs_content(file)
)
end
end

response
def vendor_gem_rbs!
raise NotImplementedError
end

def rbs_name(proto_name)
Expand Down
10 changes: 10 additions & 0 deletions lib/rbs_protobuf/translator/protobuf_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,16 @@ def service_to_decl(service, prefix:, source_code_info:, path:)
attributes: [],
overload: false
)

def vendor_gem_rbs!
dir = Pathname(__dir__) + "../../../rbs/protobuf"
dir.each_child do |child|
if child.extname == ".rbs"
response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
name: (Pathname("rbs_protobuf/protobuf") + child.basename).to_s,
content: child.read
)
end
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions test/protobuf_gem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
class ProtobufGemTest < Minitest::Test
include TestHelper

def test_vendor_gem_rbs!
input = read_proto(<<EOP)
syntax = "proto2";
message Foo {}
EOP

translator = RBSProtobuf::Translator::ProtobufGem.new(
input,
upcase_enum: true,
nested_namespace: true
)

translator.vendor_gem_rbs!

translator.response.file.each do |file|
assert_match(/^rbs_protobuf\/protobuf\/.*\.rbs$/, file.name)
end
end

def test_message_with_base_type
input = read_proto(<<EOP)
syntax = "proto2";
Expand Down

0 comments on commit eec859a

Please sign in to comment.