twirp_rails 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afb781b860d752d9ccf6eff558d3e07a7f93923ecf5b70dbbb3a7df373ff60c8
4
- data.tar.gz: ee22bba018427cb6d0c29be51a89bec3a40c464a7a67856b1169d72afecb73b1
3
+ metadata.gz: ebf7858e259ac7b44fe27db5912c15093db8e4c056278293c7a505f0ce3ba0d9
4
+ data.tar.gz: f52465a773645cb16aa65dbf7b32db9de95f804f1903230d265fd31fcd55ec68
5
5
  SHA512:
6
- metadata.gz: d14ef916943fec1782354d2349ae4c38fd918a98331d0f4827c3b7a24847720c01a2d8e414c09b4d39d99ae31bfeffb27fbe5f2d9e13b2444065cf83e0399dab
7
- data.tar.gz: 7f9ad8ccca240630862a59c107987cf1c3e371392c3e373f5cdb268f04ea24a6db091fe7d5a174bfc58430f1cde21e95e3c74466fd199b647a7bc145c18b9572
6
+ metadata.gz: a6c7dc0ab5860f6c203429536276f51ea8bbc31bc1e243f030c7fa78d6c5a547381fafd66fa4d00b1c2f6681e99f8e5f5cdbf8cf61e4ec22c38c0735f3c64abe
7
+ data.tar.gz: dd9967d8189ad815a01e8aa82dcc612e245fa9041a5e5ca2711b2cf28298bdd39d192f5bf8c23ce49d1a6aa8cddfbeab85e88552668e5aeb324d033d9116118c
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 0.1.6 - 2020-02-11
8
+
9
+ ### Added
10
+ - ```rails g twirp service``` generates swagger file at public/swagger, options to skip it or set output path
11
+ - ```rails g twirp service``` generates handler rspec test
12
+
7
13
  ## 0.1.5 - 2020-02-04
8
14
 
9
15
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twirp_rails (0.1.5)
4
+ twirp_rails (0.1.6)
5
5
  railties (~> 6.0)
6
6
  twirp (~> 1)
7
7
 
@@ -38,7 +38,7 @@ GEM
38
38
  generator_spec (0.9.4)
39
39
  activesupport (>= 3.0.0)
40
40
  railties (>= 3.0.0)
41
- google-protobuf (3.11.2-universal-darwin)
41
+ google-protobuf (3.11.3-universal-darwin)
42
42
  i18n (1.8.2)
43
43
  concurrent-ruby (~> 1.0)
44
44
  loofah (2.4.0)
data/README.md CHANGED
@@ -41,7 +41,8 @@ and run
41
41
  rails g twirp people
42
42
  ```
43
43
 
44
- This command will generate ```lib/twirp/people_pb.rb```, ```lib/twirp/people_twirp.rb``` and ```app/rpc/people_handler.rb``` and add the route.
44
+ This command will add the route and generate ```lib/twirp/people_pb.rb```, ```lib/twirp/people_twirp.rb```,
45
+ ```public/swagger/people.swagger.json```, ```app/rpc/people_handler.rb``` and ```spec/rpc/people_handler_sprc.rb```.
45
46
  ```ruby
46
47
  # app/rpc/people_handler.rb
47
48
 
@@ -3,25 +3,38 @@ require 'rails/generators'
3
3
  class TwirpGenerator < Rails::Generators::NamedBase
4
4
  source_root File.expand_path('templates', __dir__)
5
5
 
6
- PLUGIN_PATH = ENV.fetch('TWIRP_PLUGIN_PATH') { File.expand_path('~/go/bin/protoc-gen-twirp_ruby') }
6
+ class_option :skip_swagger, type: :boolean, default: false
7
+ class_option :swagger_out, type: :string, default: 'public/swagger'
8
+
9
+ GO_BIN_PATH = ENV.fetch('GOPATH') { File.expand_path('~/go/bin') }
10
+ TWIRP_PLUGIN_PATH = ENV.fetch('TWIRP_PLUGIN_PATH') { File.join(GO_BIN_PATH, 'protoc-gen-twirp_ruby') }
11
+ SWAGGER_PLUGIN_PATH = ENV.fetch('SWAGGER_PLUGIN_PATH') { File.join(GO_BIN_PATH, 'protoc-gen-twirp_swagger') }
7
12
  PROTOC_PATH = `which protoc`.chomp
8
13
 
9
14
  def check_requirements
10
15
  in_root do
11
- unless File.exists?(proto_file_name)
16
+ unless File.exist?(proto_file_name)
12
17
  raise "#{proto_file_name} not found #{`pwd`} #{`ls`}"
13
18
  end
14
19
  end
15
20
 
16
- raise 'protoc not found - install protobuf (brew/apt/yum install protobuf)' unless File.exists?(PROTOC_PATH)
21
+ raise 'protoc not found - install protobuf (brew/apt/yum install protobuf)' unless File.exist?(PROTOC_PATH)
17
22
 
18
- unless File.exists?(PLUGIN_PATH)
23
+ unless File.exist?(TWIRP_PLUGIN_PATH)
19
24
  raise <<~TEXT
20
25
  protoc-gen-twirp_ruby not found - install go (brew install go)
21
26
  and run "go get github.com/twitchtv/twirp-ruby/protoc-gen-twirp_ruby
22
27
  or set TWIRP_PLUGIN_PATH environment variable to right location.
23
28
  TEXT
24
29
  end
30
+
31
+ if gen_swagger? && !File.exist?(SWAGGER_PLUGIN_PATH)
32
+ raise <<~TEXT
33
+ protoc-gen-twirp_swagger not found - install go (brew install go)
34
+ and run "go get github.com/elliots/protoc-gen-twirp_swagger
35
+ or set SWAGGER_PLUGIN_PATH environment variable to right location.
36
+ TEXT
37
+ end
25
38
  end
26
39
 
27
40
  def generate_twirp_files
@@ -29,7 +42,9 @@ class TwirpGenerator < Rails::Generators::NamedBase
29
42
  proto_files = Dir.glob 'app/protos/**/*.proto'
30
43
 
31
44
  proto_files.each do |file|
32
- cmd = protoc_cmd(file)
45
+ gen_swagger = gen_swagger? && file =~ %r{/#{file_name}\.proto$}
46
+ pp [gen_swagger, file]
47
+ cmd = protoc_cmd(file, gen_swagger: gen_swagger)
33
48
 
34
49
  `#{cmd}`
35
50
 
@@ -43,10 +58,10 @@ class TwirpGenerator < Rails::Generators::NamedBase
43
58
  def generate_handler
44
59
  methods = proto_content.scan(PROTO_RPC_REGEXP).map do |method, arg_type, result_type|
45
60
  result_type = proto_type_to_ruby(result_type)
46
- <<-RUBY
47
- def #{method.underscore}(req, _env)
48
- #{result_type}.new
49
- end
61
+ optimize_indentation <<~RUBY, 2
62
+ def #{method.underscore}(req, _env)
63
+ #{result_type}.new
64
+ end
50
65
  RUBY
51
66
  end.join("\n")
52
67
 
@@ -63,7 +78,47 @@ class TwirpGenerator < Rails::Generators::NamedBase
63
78
  end
64
79
 
65
80
  def generate_rspec_files
66
- # TODO
81
+ in_root do
82
+ return unless File.exist?('spec')
83
+
84
+ methods = proto_content.scan(PROTO_RPC_REGEXP).map do |method, arg_type, result_type|
85
+ result_type = proto_type_to_ruby(result_type)
86
+ optimize_indentation <<~RUBY, 2
87
+ context '##{method.underscore}' do
88
+ rpc { [:#{method.underscore}, 'arg'] }
89
+
90
+ it do
91
+ expect { #{result_type}.new(subject) }.to_not raise_exception
92
+ should match({})
93
+ end
94
+ end
95
+ RUBY
96
+ end.join("\n")
97
+
98
+ create_file "spec/rpc/#{file_name}_handler_spec.rb", <<~RUBY
99
+ require 'rails_helper'
100
+
101
+ describe #{class_name}Handler do
102
+
103
+ #{methods}end
104
+ RUBY
105
+ end
106
+ end
107
+
108
+ def inject_rspec_helper
109
+ in_root do
110
+ return unless File.exist?('spec/rails_helper.rb')
111
+
112
+ require_sentinel = %r{require 'rspec/rails'\s*\n}m
113
+ include_sentinel = /RSpec\.configure do |config|\s*\n/m
114
+
115
+ inject_into_file 'spec/rails_helper.rb',
116
+ "require 'twirp/rails/rspec/helper'",
117
+ after: require_sentinel, verbose: true, force: false
118
+ inject_into_file 'spec/rails_helper.rb',
119
+ ' config.include TwirpRails::RSpec::Helper, type: :rpc, file_path: %r{spec/rpc}',
120
+ after: include_sentinel, verbose: true, force: false
121
+ end
67
122
  end
68
123
 
69
124
  private
@@ -72,9 +127,19 @@ class TwirpGenerator < Rails::Generators::NamedBase
72
127
  result_type.split('.').map(&:camelize).join('::')
73
128
  end
74
129
 
75
- def protoc_cmd(files)
130
+ def protoc_cmd(files, gen_swagger: gen_swagger?)
76
131
  FileUtils.mkdir_p 'lib/twirp'
77
- flags = "--proto_path=app/protos --ruby_out=lib/twirp --twirp_ruby_out=lib/twirp --plugin=#{PLUGIN_PATH}"
132
+
133
+ flags = '--proto_path=app/protos ' \
134
+ '--ruby_out=lib/twirp --twirp_ruby_out=lib/twirp ' \
135
+ "--plugin=#{TWIRP_PLUGIN_PATH}"
136
+
137
+ if gen_swagger
138
+ FileUtils.mkdir_p options[:swagger_out]
139
+
140
+ flags += " --plugin=#{SWAGGER_PLUGIN_PATH}" \
141
+ " --twirp_swagger_out=#{options[:swagger_out]}"
142
+ end
78
143
 
79
144
  "#{PROTOC_PATH} #{flags} #{files}"
80
145
  end
@@ -91,4 +156,8 @@ class TwirpGenerator < Rails::Generators::NamedBase
91
156
  def proto_file_name
92
157
  "app/protos/#{file_name}.proto"
93
158
  end
159
+
160
+ def gen_swagger?
161
+ !options[:skip_swagger]
162
+ end
94
163
  end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
1
5
  module TwirpRails
2
6
  module RSpec
3
7
  module Helper
@@ -1,3 +1,3 @@
1
1
  module TwirpRails
2
- VERSION = '0.1.5'.freeze
2
+ VERSION = '0.1.6'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twirp_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Zimin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-04 00:00:00.000000000 Z
11
+ date: 2020-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twirp