Module: ProtobufTranspiler
- Defined in:
- lib/railtie.rb,
lib/protobuf_transpiler.rb,
lib/protobuf_transpiler/version.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
'1.1.2'
Class Method Summary collapse
Class Method Details
.annotate_stubs ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/protobuf_transpiler.rb', line 40 def annotate_stubs require 'active_support/core_ext/string/inflections' Dir['app/stubs/**/*.rb'] .map { |s| File.absolute_path s } .each { |f| require f } stubs_modules = Dir['app/stubs/*.rb'] .map { |s| s.sub('app/stubs/', '') } .map { |s| s.sub '.rb', '' } .uniq .map { |c| Object.const_get c.camelize } stubs_modules.each do |m| out = m .constants .sort .map { |c| m.const_get c } .each_with_object({ messages: [], services: [] }) { |c, acc| if c.is_a? Class acc[:messages] << class_annotations(c) else acc[:services] << module_annotations(c) end } types_file, services_file = Dir["app/stubs/#{m.name.underscore}/*.rb"] .sort_by { |s| s.scan('services').count } [types_file, services_file] .zip([out[:messages], out[:services]]) .each { |file, content| annotate_file file, content } end end |
.generate_stubs(keep_require = false) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/protobuf_transpiler.rb', line 10 def generate_stubs(keep_require = false) paths = $LOAD_PATH.map { |p| "#{p}/**/public/**/*.proto" } proto_files = Dir[*paths].join ' ' proto_paths = proto_files .split.map { |p| p.sub %r{(?<=public).*}, '' } .uniq.join ' ' out_path = "#{Rails.root}/app/stubs/" FileUtils.mkdir_p out_path `grpc_tools_ruby_protoc --ruby_out=#{out_path} --grpc_out=#{out_path} #{proto_files} -I #{proto_paths}` # remove possibly useless require from stub file unless keep_require Dir['app/stubs/**/*.rb'].each do |fp| f = File.read fp File.write fp, (f.sub %r{\n(require.*?'\n)+}, '') end end # make zeitwerk happy Dir['app/stubs/**'] .filter { |f| File.directory? f } .each { |dir| requires = Dir.chdir dir do curr_dir = Dir.pwd.split('/').last Dir['*.rb'].map { |s| "require_relative './#{curr_dir}/#{s.sub %r{.rb$}, ''}'" } end File.write "#{dir}.rb", requires.join("\n") } end |