Class: ProtoPlugin::Base Abstract
- Inherits:
-
Object
- Object
- ProtoPlugin::Base
- Defined in:
- lib/proto_plugin/base.rb
Overview
The primary base class to inherit from when implementing a plugin.
“‘ruby require ’proto_plugin’
class MyCoolPlugin < ProtoPlugin::Base def run # override to provide your implementation end end
MyCoolPlugin.run! ““
Instance Attribute Summary collapse
-
#context ⇒ Context
readonly
The context for the current invocation of the plugin.
-
#request ⇒ Google::Protobuf::Compiler::CodeGeneratorRequest
readonly
The request message the plugin was initialized with.
-
#response ⇒ Google::Protobuf::Compiler::CodeGeneratorResponse
readonly
The response message to be sent back to
protoc
.
Class Method Summary collapse
-
.run!(input: $stdin, output: $stdout) ⇒ Object
The preferred way of invoking a plugin.
Instance Method Summary collapse
-
#add_file(path:, content:) ⇒ Object
Convenience method for appending a
CodeGeneratorResponse::File
message toresponse
. -
#files_to_generate ⇒ Array<FileDescriptor>
Returns an array of
ProtoPlugin::FileDescriptor
representing the files that were passed toprotoc
to be generated. -
#initialize(request:) ⇒ Base
constructor
Initializes a new instance of the plugin with a given
Google::Protobuf::Compiler::CodeGeneratorRequest
. -
#lookup_file(name:) ⇒ ProtoPlugin::FileDescriptor?
Finds an imported file descriptor with the given
name
attribute. -
#parameters ⇒ Hash
Convenience method for accessing the parameters passed to the plugin.
-
#run ⇒ Object
abstract
The primary entrypoint.
-
#supported_features ⇒ Object
Returns the list of supported
CodeGeneratorResponse::Feature
values by the plugin.
Constructor Details
#initialize(request:) ⇒ Base
Initializes a new instance of the plugin with a given Google::Protobuf::Compiler::CodeGeneratorRequest
.
61 62 63 64 65 66 67 |
# File 'lib/proto_plugin/base.rb', line 61 def initialize(request:) @request = request @context = Context.new(request: request) @response = Google::Protobuf::Compiler::CodeGeneratorResponse.new( supported_features: supported_features.reduce(&:|), ) end |
Instance Attribute Details
#context ⇒ Context (readonly)
The context for the current invocation of the plugin.
53 54 55 |
# File 'lib/proto_plugin/base.rb', line 53 def context @context end |
#request ⇒ Google::Protobuf::Compiler::CodeGeneratorRequest (readonly)
The request message the plugin was initialized with.
49 50 51 |
# File 'lib/proto_plugin/base.rb', line 49 def request @request end |
#response ⇒ Google::Protobuf::Compiler::CodeGeneratorResponse (readonly)
The response message to be sent back to protoc
.
57 58 59 |
# File 'lib/proto_plugin/base.rb', line 57 def response @response end |
Class Method Details
.run!(input: $stdin, output: $stdout) ⇒ Object
The preferred way of invoking a plugin.
Decodes a Google::Protobuf::Compiler::CodeGeneratorRequest
message from input:
, invokes the plugin by calling #run
, and then encodes response
to the stream specified by output:
.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/proto_plugin/base.rb', line 32 def run!(input: $stdin, output: $stdout) plugin = new( request: Google::Protobuf::Compiler::CodeGeneratorRequest.decode( input.read, ), ) plugin.run result = plugin.response output.write(result.to_proto) result end |
Instance Method Details
#add_file(path:, content:) ⇒ Object
Convenience method for appending a CodeGeneratorResponse::File
message to response
.
The path is relative to the directory specified when invoking protoc
. For example, specifiying --myplugin_out=gen
will result in gen/:path
.
121 122 123 124 125 |
# File 'lib/proto_plugin/base.rb', line 121 def add_file(path:, content:) @response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new( name: path, content: content, ) end |
#files_to_generate ⇒ Array<FileDescriptor>
Returns an array of ProtoPlugin::FileDescriptor
representing the files that were passed to protoc
to be generated.
92 93 94 95 96 |
# File 'lib/proto_plugin/base.rb', line 92 def files_to_generate @files_to_generate ||= request.file_to_generate.filter_map do |filename| lookup_file(name: filename) end end |
#lookup_file(name:) ⇒ ProtoPlugin::FileDescriptor?
Finds an imported file descriptor with the given name
attribute.
102 103 104 |
# File 'lib/proto_plugin/base.rb', line 102 def lookup_file(name:) context.file_by_filename(name) end |
#parameters ⇒ Hash
Convenience method for accessing the parameters passed to the plugin.
75 76 77 78 79 80 |
# File 'lib/proto_plugin/base.rb', line 75 def parameters @parameters ||= request.parameter&.split(",")&.each_with_object({}) do |param, hash| key, value = param.split("=") hash[key] = value end end |
#run ⇒ Object
The primary entrypoint. Override to provide your plugin’s implementation.
129 130 |
# File 'lib/proto_plugin/base.rb', line 129 def run end |
#supported_features ⇒ Object
Returns the list of supported CodeGeneratorResponse::Feature
values by the plugin. The returned values are bitwise or-ed together and set on response
.
Defaults to CodeGeneratorResponse::Feature::FEATURE_NONE
.
110 111 112 |
# File 'lib/proto_plugin/base.rb', line 110 def supported_features [Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_NONE] end |