proto_plugin 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/exe/protoc-gen-proto-plugin-demo +3 -1
- data/lib/proto_plugin/base.rb +57 -2
- data/lib/proto_plugin/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c7fb4f38a0f6f9a22f62228fbd39ac9995319b83b05667f83b510cd3e3407fb
|
4
|
+
data.tar.gz: d6f39e7a5a77c5f541ee2c52f12d114c8fb6795d8e27617542927008911a23dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1a877e5ee1ed9e55c882cbe312abaa83c1fc29bacad7ba4c35db5c668f40cefd19182263fadfe974e458e937fa30df30d8c30257afe682a98e5ad1d071f0412
|
7
|
+
data.tar.gz: 8fc91b1050f5661b616ebd209a116bb8e1edb2e13a2b66b6a20b7be517c78f0fd8b4ee8317af121cc313055e5adc9414855985f70ea754d9494bcc2598a372b8
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
gem install proto_plugin
|
11
11
|
```
|
12
12
|
|
13
|
-
##
|
13
|
+
## Getting Started
|
14
14
|
|
15
15
|
Creating a `protoc` plugin is as simple as creating a new executable script.
|
16
16
|
|
@@ -36,6 +36,11 @@ end
|
|
36
36
|
MyCoolPlugin.run!
|
37
37
|
```
|
38
38
|
|
39
|
+
> [!TIP]
|
40
|
+
> For more details on the available API, see the docs: https://cocoahero.github.io/proto_plugin
|
41
|
+
|
42
|
+
### Usage
|
43
|
+
|
39
44
|
To invoke the plugin, first make sure you have `protoc` [installed](https://github.com/protocolbuffers/protobuf#protobuf-compiler-installation). Then in a terminal, run:
|
40
45
|
|
41
46
|
```bash
|
@@ -7,7 +7,9 @@ class Demo < ProtoPlugin::Base
|
|
7
7
|
def run
|
8
8
|
request.file_to_generate.each do |f|
|
9
9
|
name = File.basename(f, ".proto")
|
10
|
-
add_file(
|
10
|
+
add_file(path: "#{name}.txt", content: <<~TXT)
|
11
|
+
Parameters: #{parameters}
|
12
|
+
|
11
13
|
This file was generated from #{name}.proto!
|
12
14
|
TXT
|
13
15
|
end
|
data/lib/proto_plugin/base.rb
CHANGED
@@ -4,8 +4,31 @@ require "google/protobuf"
|
|
4
4
|
require "google/protobuf/plugin_pb"
|
5
5
|
|
6
6
|
module ProtoPlugin
|
7
|
+
# The primary base class to inherit from when implementing a plugin.
|
8
|
+
#
|
9
|
+
# ```ruby
|
10
|
+
# require 'proto_plugin'
|
11
|
+
#
|
12
|
+
# class MyCoolPlugin < ProtoPlugin::Base
|
13
|
+
# def run
|
14
|
+
# # override to provide your implementation
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# MyCoolPlugin.run!
|
19
|
+
# ````
|
20
|
+
# @abstract
|
7
21
|
class Base
|
8
22
|
class << self
|
23
|
+
##
|
24
|
+
# The preferred way of invoking a plugin.
|
25
|
+
#
|
26
|
+
# Decodes a `Google::Protobuf::Compiler::CodeGeneratorRequest` message
|
27
|
+
# from `input:`, invokes the plugin by calling `#run`, and then encodes
|
28
|
+
# `response` to the stream specified by `output:`.
|
29
|
+
#
|
30
|
+
# @param input [IO] The stream that the request is decoded from.
|
31
|
+
# @param output [IO] The stream that the response is encoded to.
|
9
32
|
def run!(input: $stdin, output: $stdout)
|
10
33
|
plugin = new(
|
11
34
|
request: Google::Protobuf::Compiler::CodeGeneratorRequest.decode(
|
@@ -21,10 +44,16 @@ module ProtoPlugin
|
|
21
44
|
end
|
22
45
|
end
|
23
46
|
|
47
|
+
# The request message the plugin was initialized with.
|
48
|
+
# @return [Google::Protobuf::Compiler::CodeGeneratorRequest]
|
24
49
|
attr_reader :request
|
25
50
|
|
51
|
+
# The response message to be sent back to `protoc`.
|
52
|
+
# @return [Google::Protobuf::Compiler::CodeGeneratorResponse]
|
26
53
|
attr_reader :response
|
27
54
|
|
55
|
+
# Initializes a new instance of the plugin with a given
|
56
|
+
# `Google::Protobuf::Compiler::CodeGeneratorRequest`.
|
28
57
|
def initialize(request:)
|
29
58
|
@request = request
|
30
59
|
@response = Google::Protobuf::Compiler::CodeGeneratorResponse.new(
|
@@ -32,16 +61,42 @@ module ProtoPlugin
|
|
32
61
|
)
|
33
62
|
end
|
34
63
|
|
64
|
+
# Convenience method for accessing the parameters passed to the plugin.
|
65
|
+
#
|
66
|
+
# @example `protoc --myplugin_opt=key=value --myplugin_opt=bare`
|
67
|
+
# {"key" => "value", "bare" => nil}
|
68
|
+
#
|
69
|
+
# @return [Hash]
|
70
|
+
def parameters
|
71
|
+
@parameters ||= request.parameter&.split(",")&.each_with_object({}) do |param, hash|
|
72
|
+
key, value = param.split("=")
|
73
|
+
hash[key] = value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Returns the list of supported `CodeGeneratorResponse::Feature` values by the plugin. The returned
|
78
|
+
# values are bitwise or-ed together and set on `response`.
|
79
|
+
#
|
80
|
+
# Defaults to `CodeGeneratorResponse::Feature::FEATURE_NONE`.
|
35
81
|
def supported_features
|
36
82
|
[Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_NONE]
|
37
83
|
end
|
38
84
|
|
39
|
-
|
85
|
+
# Convenience method for appending a `CodeGeneratorResponse::File` message to `response`.
|
86
|
+
#
|
87
|
+
# The path is relative to the directory specified when invoking `protoc`. For example,
|
88
|
+
# specifiying `--myplugin_out=gen` will result in `gen/:path`.
|
89
|
+
#
|
90
|
+
# @param path [String] The relative path to write the file's content.
|
91
|
+
# @param content [String] The content which will be written to the file.
|
92
|
+
def add_file(path:, content:)
|
40
93
|
@response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
|
41
|
-
name:
|
94
|
+
name: path, content: content,
|
42
95
|
)
|
43
96
|
end
|
44
97
|
|
98
|
+
# The primary entrypoint. Override to provide your plugin's implementation.
|
99
|
+
# @abstract
|
45
100
|
def run
|
46
101
|
end
|
47
102
|
end
|
data/lib/proto_plugin/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proto_plugin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Baker
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-protobuf
|
@@ -44,7 +44,9 @@ licenses:
|
|
44
44
|
metadata:
|
45
45
|
homepage_uri: https://github.com/cocoahero/proto_plugin
|
46
46
|
source_code_uri: https://github.com/cocoahero/proto_plugin
|
47
|
+
documentation_uri: https://cocoahero.github.io/proto_plugin
|
47
48
|
allowed_push_host: https://rubygems.org
|
49
|
+
funding_uri: https://github.com/sponsors/cocoahero
|
48
50
|
post_install_message:
|
49
51
|
rdoc_options: []
|
50
52
|
require_paths:
|
@@ -63,5 +65,5 @@ requirements: []
|
|
63
65
|
rubygems_version: 3.5.11
|
64
66
|
signing_key:
|
65
67
|
specification_version: 4
|
66
|
-
summary: Easily build
|
68
|
+
summary: Easily build protobuf compiler plugins in Ruby.
|
67
69
|
test_files: []
|