1#!/usr/bin/env ruby
2
3require 'erb'
4require 'fileutils'
5
6require_relative 'run.rb'
7
8PROTO_INCLUDE = './proto'
9PROTO_FILES = Dir[File.join(PROTO_INCLUDE, '*.proto')].sort.map { |f| File.absolute_path(f) }
10RUBY_PREFIX = 'ruby/proto'
11RUBY_VERSION_FILE = 'gitaly/version.rb'
12
13def main
14  ruby_lib_gitaly = File.join(RUBY_PREFIX, 'gitaly')
15  FileUtils.rm(Dir[File.join(ruby_lib_gitaly, '**/*_pb.rb')])
16  FileUtils.mkdir_p(ruby_lib_gitaly)
17
18  Dir.chdir(File.join(Dir.pwd, 'ruby')) do
19    # Using an absolute path make sure the prefixes match, or the passed in file
20    # locations. `protoc` requires this.
21    proto_include_abs = File.absolute_path(File.join('..', PROTO_INCLUDE))
22
23    run!(%W[bundle exec grpc_tools_ruby_protoc -I #{proto_include_abs} --ruby_out=../#{ruby_lib_gitaly} --grpc_out=../#{ruby_lib_gitaly}] + PROTO_FILES)
24  end
25
26  write_ruby_requires
27end
28
29def write_ruby_requires
30  requires = Dir.chdir(RUBY_PREFIX) { Dir['gitaly/*_services_pb.rb'] }.sort
31  abort "No auto-generated Ruby service files found" if requires.empty?
32  requires.unshift(RUBY_VERSION_FILE)
33  gem_root = File.join(RUBY_PREFIX, 'gitaly.rb')
34  gem_root_template = ERB.new <<~EOT
35    # This file is generated by #{File.basename($0)}. Do not edit.
36    $:.unshift(File.expand_path('../gitaly', __FILE__))
37    <% requires.each do |f| %>
38    require '<%= f.sub(/\.rb$/, '') %>'
39    <% end %>
40  EOT
41  open(gem_root, 'w') { |f| f.write(gem_root_template.result(binding)) }
42end
43
44main
45