File: generate-proto-ruby

package info (click to toggle)
ruby-spamcheck 1.0.0%2Bgit20220819.662e6bf-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 620 kB
  • sloc: python: 828; ruby: 422; makefile: 40
file content (51 lines) | stat: -rwxr-xr-x 1,712 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
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
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby

require 'erb'
require 'fileutils'

require_relative 'run.rb'

PROTO_INCLUDE = 'api/v1'
PROTO_FILES = Dir[File.join(PROTO_INCLUDE, '*.proto')].sort.map { |f| File.absolute_path(f) }
RUBY_PREFIX = File.join("ruby")
RUBY_VERSION_FILE = 'spamcheck/version.rb'
GOPATH = ENV["GOPATH"]

GO_PROTO_LIBS = [
  "#{GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis",
  "#{GOPATH}/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0/third_party/googleapis"
]

def main
  ruby_lib_spamcheck = File.join(RUBY_PREFIX, 'spamcheck')
  FileUtils.rm(Dir[File.join(ruby_lib_spamcheck, '**/*_pb.rb')])
  FileUtils.mkdir_p(ruby_lib_spamcheck)

  Dir.chdir(File.join(Dir.pwd, 'ruby')) do
    # Using an absolute path make sure the prefixes match, or the passed in file
    # locations. `protoc` requires this.
    GO_PROTO_LIBS << File.absolute_path(File.join('..', PROTO_INCLUDE))
    includes = GO_PROTO_LIBS.join(":")

    run!(%W[grpc_tools_ruby_protoc --proto_path=#{includes} --ruby_out=../#{ruby_lib_spamcheck} --grpc_out=../#{ruby_lib_spamcheck}] + PROTO_FILES)
  end

  write_ruby_requires
end

def write_ruby_requires
  requires = Dir.chdir(RUBY_PREFIX) { Dir['**/*_pb.rb'] }.sort
  abort "No auto-generated Ruby service files found" if requires.empty?
  requires.unshift(RUBY_VERSION_FILE)
  gem_root = File.join(RUBY_PREFIX, 'spamcheck.rb')
  gem_root_template = ERB.new <<~EOT
    # This file is generated by #{File.basename($0)}. Do not edit.
    $:.unshift(File.expand_path('../spamcheck', __FILE__))
    <% requires.each do |f| %>
    require '<%= f.chomp('.rb') %>'
    <% end %>
  EOT
  open(gem_root, 'w') { |f| f.write(gem_root_template.result(binding)) }
end

main