#!/usr/bin/env ruby

require 'fileutils'

require_relative 'run.rb'

def main
  no_changes!
  FileUtils.rm(Dir[File.join('go/gitalypb/*.pb.go')])
  run!(%w[make pb-go-stubs])
  run!(%w[_support/generate-from-proto])
  no_changes!
  puts 'OK! The gRPC / Protobuf clients are up-to-date'
end

def no_changes!
  changes = capture!(%w[git status --porcelain]).chomp.lines
  # _support/Gemfile.lock might change due to the CI Bundler version. Ignore all of _support.
  changes = changes.reject { |l| l =~ /^.. _support/ }

  if changes.any?
    puts changes
    run!(%w[git diff])
    abort 'error: detected changes'
  end
end

main
