Skip to content

Commit

Permalink
chore: Support building from a piper client with the owlbot script
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma committed Oct 31, 2021
1 parent 4f7b0fc commit 77e7234
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .toys/owlbot-migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
flag :branch_name, "--branch=NAME" do
desc "The branch name"
end
flag :source_repo, "--source-repo=PATH" do
desc "Path to the googleapis-gen source repo"
end

OWLBOT_CONFIG_FILE_NAME = ".OwlBot.yaml"
SYNTH_CONFIG_FILE_NAME = "synth.py"
Expand Down Expand Up @@ -72,10 +75,6 @@ def choose_gems
error "No gemspec found for #{name}"
end
content = File.read File.join(context_directory, name, SYNTH_CONFIG_FILE_NAME)
unless content.include? "gapic = gcp.GAPICBazel()\n"
next if auto_gems
error "Synth script for #{name} doesn't seem to invoke bazel"
end
proto_path = proto_path_from_synth_content name, content
unless proto_path
next if auto_gems
Expand All @@ -88,7 +87,7 @@ def choose_gems

def proto_path_from_synth_content name, content
match = %r{proto_path="([^"]+)/v\d\w*"}.match content
return match[1] if match
return match[1].sub %r{/v\d\w*$}, "" if match
match = %r{gapic\.ruby_library\(\s*"([^"]+)",}.match content
return "google/cloud/#{match[1]}" if match
nil
Expand Down Expand Up @@ -125,6 +124,7 @@ def switch_files name, proto_base

def run_owlbot names
cmd = ["owlbot"]
cmd << "--source-repo" << source_repo if source_repo
cmd << "--pull" if pull
if verbosity < 0
cmd << "-#{'q' * (-verbosity)}"
Expand Down
42 changes: 41 additions & 1 deletion .toys/owlbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
flag :source_repo, "--source-repo=PATH" do
desc "Path to the googleapis-gen source repo"
end
flag :piper_client, "--piper-client=NAME" do
desc "Generate by running Bazel from the given piper clinet rather than using googleapis-gen"
end

OWLBOT_CONFIG_FILE_NAME = ".OwlBot.yaml"
OWLBOT_CLI_IMAGE = "gcr.io/cloud-devrel-public-resources/owlbot-cli"
Expand All @@ -42,6 +45,7 @@

include :exec, e: true
include :terminal
include :fileutils

def run
require "psych"
Expand All @@ -56,7 +60,11 @@ def run
sources = collect_sources gems

pull_images
run_owlbot sources
if piper_client
run_bazel sources
else
run_owlbot sources
end
verify_staging gems
results = process_gems gems
final_output results
Expand Down Expand Up @@ -138,6 +146,30 @@ def run_owlbot sources
docker_run(*cmd)
end

def run_bazel sources
rm_rf STAGING_DIR_NAME
mkdir_p STAGING_DIR_NAME
sources.each do |name, source|
library_path, bazel_target = determine_bazel_target source
exec ["bazel", "build", "//#{library_path}:#{bazel_target}"], chdir: bazel_base_dir
generated_dir = File.join bazel_base_dir, "bazel-bin", library_path, bazel_target
cp_r generated_dir, File.join(STAGING_DIR_NAME, name)
end
end

def determine_bazel_target source_regex
postfix = "/[^/]+-ruby/(.*)"
error "Unexpected source: #{source_regex}" unless source_regex.end_with? postfix
library_path = source_regex[0..(-postfix.size-1)].sub %r{^/}, ""
build_file_path = File.join bazel_base_dir, library_path, "BUILD.bazel"
error "Unable to find #{build_file_path}" unless File.file? build_file_path
build_content = File.read build_file_path
match = /ruby_gapic_assembly_pkg\(\n\s+name\s*=\s*"([\w-]+-ruby)",/.match build_content
error "Unable to find ruby build rule in #{build_file_path}" unless match
bazel_target = match[1]
[library_path, bazel_target]
end

def verify_staging gems
gems.each do |name|
staging_dir = File.join STAGING_DIR_NAME, name
Expand Down Expand Up @@ -210,6 +242,14 @@ def docker_run *args
exec cmd
end

def piper_client_dir
@piper_client_dir ||= capture(["p4", "g4d", piper_client]).strip
end

def bazel_base_dir
@bazel_base_dir ||= File.join piper_client_dir, "third_party", "googleapis", "stable"
end

def error str
logger.error str
exit 1
Expand Down

0 comments on commit 77e7234

Please sign in to comment.