diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-10-27 07:34:56 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-10-28 08:42:16 +0300 |
commit | 31fcb2bfadc75e64e5c6730d233f753ca49f8997 (patch) | |
tree | 82476035a62db65dc69747e7a103da2cea8c9f7a /tools | |
parent | 19beefd248cd113b341470dca45bd8b57026251a (diff) |
ruby: Move scripts that generate Proto sources into tools directory
We've got multiple scripts that are required to generate Ruby code from
our Protobuf definitions in the `_support` directory. This has multiple
smells:
- It's out-of-line with all the other tools, which nowadays are
located in the `tools` directory.
- It's hard to discover and find out which parts logically form a
unit.
- We are reusing the Gemfile of the Ruby sidecar to pin the
`grpc-tools` dependency to a specific version.
Move the tooling into its own `tools/protogem` directory that's got its
own Gemfile to fix these points. This also allows us to auto-update
dependencies via the Renovate bot like we do for our other tools.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/protogem/Gemfile | 3 | ||||
-rw-r--r-- | tools/protogem/Gemfile.lock | 13 | ||||
-rwxr-xr-x | tools/protogem/generate-proto-ruby | 44 | ||||
-rwxr-xr-x | tools/protogem/publish-gem | 39 | ||||
-rw-r--r-- | tools/protogem/run.rb | 36 |
5 files changed, 135 insertions, 0 deletions
diff --git a/tools/protogem/Gemfile b/tools/protogem/Gemfile new file mode 100644 index 000000000..9cafb7578 --- /dev/null +++ b/tools/protogem/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'grpc-tools', '~> 1.42.0' diff --git a/tools/protogem/Gemfile.lock b/tools/protogem/Gemfile.lock new file mode 100644 index 000000000..57b2bf6f9 --- /dev/null +++ b/tools/protogem/Gemfile.lock @@ -0,0 +1,13 @@ +GEM + remote: https://rubygems.org/ + specs: + grpc-tools (1.42.0) + +PLATFORMS + ruby + +DEPENDENCIES + grpc-tools (~> 1.42.0) + +BUNDLED WITH + 2.3.24 diff --git a/tools/protogem/generate-proto-ruby b/tools/protogem/generate-proto-ruby new file mode 100755 index 000000000..544da294f --- /dev/null +++ b/tools/protogem/generate-proto-ruby @@ -0,0 +1,44 @@ +#!/usr/bin/env ruby + +require 'erb' +require 'fileutils' + +require_relative 'run.rb' + +PROTO_INCLUDE = './proto' +PROTO_FILES = Dir[File.join(PROTO_INCLUDE, '*.proto')].sort.map { |f| File.absolute_path(f) } +RUBY_PREFIX = 'ruby/proto' +RUBY_VERSION_FILE = 'gitaly/version.rb' + +def main + ruby_lib_gitaly = File.join(RUBY_PREFIX, 'gitaly') + FileUtils.rm(Dir[File.join(ruby_lib_gitaly, '**/*_pb.rb')]) + FileUtils.mkdir_p(ruby_lib_gitaly) + + Dir.chdir(File.join(Dir.pwd, 'tools', 'protogem')) do + # Using an absolute path make sure the prefixes match, or the passed in file + # locations. `protoc` requires this. + proto_include_abs = File.absolute_path(File.join('..', '..', PROTO_INCLUDE)) + + run!(%W[bundle exec grpc_tools_ruby_protoc -I #{proto_include_abs} --ruby_out=../../#{ruby_lib_gitaly} --grpc_out=../../#{ruby_lib_gitaly}] + PROTO_FILES) + end + + write_ruby_requires +end + +def write_ruby_requires + requires = Dir.chdir(RUBY_PREFIX) { Dir['gitaly/*_services_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, 'gitaly.rb') + gem_root_template = ERB.new <<~EOT + # This file is generated by #{File.basename($0)}. Do not edit. + $:.unshift(File.expand_path('../gitaly', __FILE__)) + <% requires.each do |f| %> + require '<%= f.sub(/\.rb$/, '') %>' + <% end %> + EOT + open(gem_root, 'w') { |f| f.write(gem_root_template.result(binding)) } +end + +main diff --git a/tools/protogem/publish-gem b/tools/protogem/publish-gem new file mode 100755 index 000000000..f0a1b138e --- /dev/null +++ b/tools/protogem/publish-gem @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby + +require_relative 'run.rb' + +def main(tag) + version = tag.sub(/^v/, '') + + unless version.match?(/\d+\.\d+\.\d+(-rc\d+)?/) + abort "Version string #{version.inspect} does not look like a Gitaly Release tag (e.g. \"v1.0.2\"). Aborting." + end + + ref = capture!(%w[git describe --tag]).chomp + if ref != "v#{version}" + abort "Checkout tag v#{version} to publish.\n\t git checkout v#{version}" + end + + puts 'Testing for changed files' + run!(%w[git diff --quiet --exit-code]) + + puts 'Testing for staged changes' + run!(%w[git diff --quiet --cached --exit-code]) + + gem = "gitaly-#{version}.gem" + run!(['gem', 'build', 'gitaly.gemspec', '--output', gem]) + abort "gem not found: #{gem}" unless File.exist?(gem) + + puts "Proceed to publish version #{tag}? Enter 'Yes' to continue; Ctrl-C to abort" + $stdout.flush + abort unless $stdin.gets.chomp == 'Yes' + + run!(%W[gem push #{gem}]) +end + +unless ARGV.count == 1 + warn "Usage: #{$0} TAG" + abort +end + +main(ARGV[0]) diff --git a/tools/protogem/run.rb b/tools/protogem/run.rb new file mode 100644 index 000000000..e7d29a1e8 --- /dev/null +++ b/tools/protogem/run.rb @@ -0,0 +1,36 @@ +def run!(cmd, chdir='.') + GitalySupport.print_cmd(cmd) + unless system(*cmd, chdir: chdir) + GitalySupport.fail_cmd!(cmd) + end +end + +def run2!(cmd, chdir: '.', out: 1) + GitalySupport.print_cmd(cmd) + unless system(*cmd, chdir: chdir, out: out) + GitalySupport.fail_cmd!(cmd) + end +end + +def capture!(cmd, chdir='.') + GitalySupport.print_cmd(cmd) + output = IO.popen(cmd, chdir: chdir) { |io| io.read } + GitalySupport.fail_cmd!(cmd) unless $?.success? + output +end + +module GitalySupport + class << self + def print_cmd(cmd) + puts '-> ' + printable_cmd(cmd) + end + + def fail_cmd!(cmd) + abort "command failed: #{printable_cmd(cmd)}" + end + + def printable_cmd(cmd) + cmd.join(' ') + end + end +end |