Skip to content

Commit

Permalink
Add files_to_generate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoahero committed Oct 2, 2024
1 parent 23a265d commit d0b584a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions exe/protoc-gen-proto-plugin-demo
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ require "proto_plugin"

class Demo < ProtoPlugin::Base
def run
request.file_to_generate.each do |f|
name = File.basename(f, ".proto")
files_to_generate.each do |f|
name = File.basename(f.name, ".proto")
add_file(path: "#{name}.txt", content: <<~TXT)
Parameters: #{parameters}
This file was generated from #{name}.proto!
This file was generated from #{f.name}!
TXT
end
end
Expand Down
16 changes: 16 additions & 0 deletions lib/proto_plugin/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ def parameters
end
end

# Returns an array of `Google::Protobuf::FileDescriptorProto` representing the files that
# were passed to `protoc` to be generated.
#
# @example `protoc --myplugin_out=. input_one.proto input_two.proto`
# [
# <Google::Protobuf::FileDescriptorProto: name: "input_one.proto">,
# <Google::Protobuf::FileDescriptorProto: name: "input_two.proto">
# ]
#
# @return [Array]
def files_to_generate
@files_to_generate ||= request.file_to_generate.filter_map do |filename|
lookup_file(name: filename)
end
end

# Finds a `Google::Protobuf::FileDescriptorProto` with the given `name` attribute.
#
# @return [Google::Protobuf::FileDescriptorProto]
Expand Down
8 changes: 8 additions & 0 deletions test/proto_plugin/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def run
assert_equal("Hello World, Again!", @plugin.response.file.last.content)
end

test "files_to_generate" do
files = @plugin.files_to_generate

assert_equal(1, files.count)
assert_instance_of(Google::Protobuf::FileDescriptorProto, files.first)
assert_equal("test/fixtures/simple/simple.proto", files.first.name)
end

test "lookup_file" do
refute_nil(@plugin.lookup_file(name: "test/fixtures/simple/simple.proto"))
assert_nil(@plugin.lookup_file(name: "test/fixtures/simple/missing.proto"))
Expand Down

0 comments on commit d0b584a

Please sign in to comment.