proto_plugin 0.1.0.pre.alpha.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a1f639a6d10b3b1453fe175009232fc1e1b548c4b93b7075e336b548c9a71bf
4
- data.tar.gz: '008f968744cc7f74d42df472d1b191a71752973ad9ebba255673d4db4c594f28'
3
+ metadata.gz: 3c7fb4f38a0f6f9a22f62228fbd39ac9995319b83b05667f83b510cd3e3407fb
4
+ data.tar.gz: d6f39e7a5a77c5f541ee2c52f12d114c8fb6795d8e27617542927008911a23dc
5
5
  SHA512:
6
- metadata.gz: 97fc2b8b9e3d1687d987696a95b479c816937ab0ff92d20e929974dc9884e9d60ff869392fa29eec69285531965a73da35146a88f62e07bcf320efbda85000be
7
- data.tar.gz: 82546a40d21f5c5f893fcd89df670667743f0b15cc133134cfbe68ee997cf157a9544ee46bb97ea3f180115f89d5e98ae7671c38cdd44fd6f1b7905fa6021d1f
6
+ metadata.gz: a1a877e5ee1ed9e55c882cbe312abaa83c1fc29bacad7ba4c35db5c668f40cefd19182263fadfe974e458e937fa30df30d8c30257afe682a98e5ad1d071f0412
7
+ data.tar.gz: 8fc91b1050f5661b616ebd209a116bb8e1edb2e13a2b66b6a20b7be517c78f0fd8b4ee8317af121cc313055e5adc9414855985f70ea754d9494bcc2598a372b8
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
- # Proto::Plugin
1
+ # ProtoPlugin
2
2
 
3
+ [![Lint](https://github.com/cocoahero/proto_plugin/actions/workflows/lint.yml/badge.svg)](https://github.com/cocoahero/proto_plugin/actions/workflows/lint.yml)
4
+ [![Test](https://github.com/cocoahero/proto_plugin/actions/workflows/test.yml/badge.svg)](https://github.com/cocoahero/proto_plugin/actions/workflows/test.yml)
3
5
  [![Gem Version](https://badge.fury.io/rb/proto_plugin.svg)](https://badge.fury.io/rb/proto_plugin)
4
6
 
5
7
  ## Installation
@@ -8,6 +10,55 @@
8
10
  gem install proto_plugin
9
11
  ```
10
12
 
13
+ ## Getting Started
14
+
15
+ Creating a `protoc` plugin is as simple as creating a new executable script.
16
+
17
+ The name of the file must follow the format `protoc-gen-[plugin-name]`. As an example, the below file could be named `protoc-gen-mycoolplugin`.
18
+
19
+ ```ruby
20
+ #! /usr/bin/env ruby
21
+
22
+ require "proto_plugin"
23
+
24
+ class MyCoolPlugin < ProtoPlugin::Base
25
+ def run
26
+ request.file_to_generate.each do |f|
27
+ name = File.basename(f, ".proto")
28
+
29
+ add_file(name: "#{name}.txt", content: <<~TXT)
30
+ This file was generated from #{name}.proto!
31
+ TXT
32
+ end
33
+ end
34
+ end
35
+
36
+ MyCoolPlugin.run!
37
+ ```
38
+
39
+ > [!TIP]
40
+ > For more details on the available API, see the docs: https://cocoahero.github.io/proto_plugin
41
+
42
+ ### Usage
43
+
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:
45
+
46
+ ```bash
47
+ protoc --plugin=path/to/protoc-gen-mycoolplugin --mycoolplugin_out=. input.proto
48
+ ```
49
+
50
+ If the executable script is in your `$PATH`, for example installed via a gem, you can omit the `--plugin` argument.
51
+
52
+ ```bash
53
+ protoc --mycoolplugin_out=. input.proto
54
+ ```
55
+
56
+ See [`exe/protoc-gen-proto-plugin-demo`](./exe/protoc-gen-proto-plugin-demo) in this repo as another example of a plugin. Since it should be in your `$PATH` (you did install this gem right?) you can invoke it with:
57
+
58
+ ```bash
59
+ protoc --proto-plugin-demo_out=. input.proto
60
+ ```
61
+
11
62
  ## Development
12
63
 
13
64
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,19 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "proto_plugin"
5
+
6
+ class Demo < ProtoPlugin::Base
7
+ def run
8
+ request.file_to_generate.each do |f|
9
+ name = File.basename(f, ".proto")
10
+ add_file(path: "#{name}.txt", content: <<~TXT)
11
+ Parameters: #{parameters}
12
+
13
+ This file was generated from #{name}.proto!
14
+ TXT
15
+ end
16
+ end
17
+ end
18
+
19
+ Demo.run!
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "google/protobuf"
4
+ require "google/protobuf/plugin_pb"
5
+
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
21
+ class Base
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.
32
+ def run!(input: $stdin, output: $stdout)
33
+ plugin = new(
34
+ request: Google::Protobuf::Compiler::CodeGeneratorRequest.decode(
35
+ input.read,
36
+ ),
37
+ )
38
+
39
+ plugin.run
40
+
41
+ result = plugin.response
42
+ output.write(result.to_proto)
43
+ result
44
+ end
45
+ end
46
+
47
+ # The request message the plugin was initialized with.
48
+ # @return [Google::Protobuf::Compiler::CodeGeneratorRequest]
49
+ attr_reader :request
50
+
51
+ # The response message to be sent back to `protoc`.
52
+ # @return [Google::Protobuf::Compiler::CodeGeneratorResponse]
53
+ attr_reader :response
54
+
55
+ # Initializes a new instance of the plugin with a given
56
+ # `Google::Protobuf::Compiler::CodeGeneratorRequest`.
57
+ def initialize(request:)
58
+ @request = request
59
+ @response = Google::Protobuf::Compiler::CodeGeneratorResponse.new(
60
+ supported_features: supported_features.reduce(&:|),
61
+ )
62
+ end
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`.
81
+ def supported_features
82
+ [Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_NONE]
83
+ end
84
+
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:)
93
+ @response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
94
+ name: path, content: content,
95
+ )
96
+ end
97
+
98
+ # The primary entrypoint. Override to provide your plugin's implementation.
99
+ # @abstract
100
+ def run
101
+ end
102
+ end
103
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ProtoPlugin
4
- VERSION = "0.1.0.pre.alpha.2"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/proto_plugin.rb CHANGED
@@ -4,3 +4,4 @@ module ProtoPlugin
4
4
  end
5
5
 
6
6
  require_relative "proto_plugin/version"
7
+ require_relative "proto_plugin/base"
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.1.0.pre.alpha.2
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-21 00:00:00.000000000 Z
11
+ date: 2024-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-protobuf
@@ -27,13 +27,16 @@ dependencies:
27
27
  description:
28
28
  email:
29
29
  - jonathan@jmb.dev
30
- executables: []
30
+ executables:
31
+ - protoc-gen-proto-plugin-demo
31
32
  extensions: []
32
33
  extra_rdoc_files: []
33
34
  files:
34
35
  - LICENSE.txt
35
36
  - README.md
37
+ - exe/protoc-gen-proto-plugin-demo
36
38
  - lib/proto_plugin.rb
39
+ - lib/proto_plugin/base.rb
37
40
  - lib/proto_plugin/version.rb
38
41
  homepage: https://github.com/cocoahero/proto_plugin
39
42
  licenses:
@@ -41,16 +44,18 @@ licenses:
41
44
  metadata:
42
45
  homepage_uri: https://github.com/cocoahero/proto_plugin
43
46
  source_code_uri: https://github.com/cocoahero/proto_plugin
47
+ documentation_uri: https://cocoahero.github.io/proto_plugin
44
48
  allowed_push_host: https://rubygems.org
49
+ funding_uri: https://github.com/sponsors/cocoahero
45
50
  post_install_message:
46
51
  rdoc_options: []
47
52
  require_paths:
48
53
  - lib
49
54
  required_ruby_version: !ruby/object:Gem::Requirement
50
55
  requirements:
51
- - - ">="
56
+ - - "~>"
52
57
  - !ruby/object:Gem::Version
53
- version: 3.0.0
58
+ version: '3.0'
54
59
  required_rubygems_version: !ruby/object:Gem::Requirement
55
60
  requirements:
56
61
  - - ">="
@@ -60,5 +65,5 @@ requirements: []
60
65
  rubygems_version: 3.5.11
61
66
  signing_key:
62
67
  specification_version: 4
63
- summary: Easily build `protoc` plugins in Ruby.
68
+ summary: Easily build protobuf compiler plugins in Ruby.
64
69
  test_files: []