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.2.3'

Class Method Summary collapse

Class Method Details

.annotate_stubs(path = 'app/stubs') ⇒ Object



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
72
73
74
75
76
# File 'lib/protobuf_transpiler.rb', line 44

def annotate_stubs path = 'app/stubs'
  require 'active_support/core_ext/string/inflections'

  Dir["#{path}/**/*.rb"]
    .map { |s| File.absolute_path s }
    .each { |f| zeitwerk_original_require f }

  stubs_modules = Dir["#{path}/*.rb"]
                    .map { |s| s.sub(path, '') }
                    .map { |s| s.sub '.rb', '' }
                    .uniq
                    .map { |c| Object.const_get c.camelize }

  stubs_modules.each do |m|
    out                       = m
                                  .constants
                                  .sort
                                  .map { |c| ignore_errors(NameError) { m.const_get c } }
                                  .filter(&:present?)
                                  .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["#{path}/#{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_initializer(stubs_path = 'app/stubs') ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/protobuf_transpiler.rb', line 78

def generate_initializer stubs_path = 'app/stubs'
  file_content = ''

  if path_in_app stubs_path
    # Ignore the path in zeitwerk
    file_content += "Rails.autoloaders.main.ignore '#{stubs_path}/**/*'\n"
  end

  file_content += "Dir[\"\#{Rails.root}/#{stubs_path}/*\"].each { |f| require_relative f }\n"

  File.write "#{Rails.root}/config/initializers/protobuf_transpiler.rb", file_content
end

.generate_stubs(keep_require = false, path = 'app/stubs') ⇒ 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
39
40
41
42
# File 'lib/protobuf_transpiler.rb', line 10

def generate_stubs(keep_require = false, path = 'app/stubs')
  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}/#{path}/"
  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["#{path}/**/*.rb"].each do |fp|
      f = File.read fp
      File.write fp, (f.sub %r{\n(require.*?'\n)+}, '')
    end
  end

  if path_in_app path
    Rails.autoloaders.main.ignore "#{path}/**/*"
  end

  # make zeitwerk happy
  Dir["#{path}/**"]
    .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