Skip to content

Commit

Permalink
feat: support RBE
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Jan 15, 2024
1 parent f68794b commit c4180ae
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
7 changes: 4 additions & 3 deletions ruby/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {},
rlocation_function = BATCH_RLOCATION_FUNCTION
script = ctx.actions.declare_file("{}.rb.cmd".format(ctx.label.name))
template = ctx.file._binary_cmd_tpl
environment.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
environment.update({"PATH": _normalize_path(ctx, toolchain.ruby.dirname) + ";%PATH%"})
else:
rlocation_function = BASH_RLOCATION_FUNCTION
script = ctx.actions.declare_file("{}.rb.sh".format(ctx.label.name))
template = ctx.file._binary_sh_tpl
environment.update({"PATH": "%s:$PATH" % toolchain.bindir})
environment.update({"PATH": "%s:$PATH" % toolchain.ruby.dirname})

if bundler:
bundler_command = "bundle exec"
Expand Down Expand Up @@ -114,7 +114,8 @@ def rb_binary_impl(ctx):
transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list()

ruby_toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
tools = [ruby_toolchain.ruby, ruby_toolchain.bundle, ruby_toolchain.gem, ctx.file._runfiles_library]
tools = [ctx.file._runfiles_library]
tools.extend(ruby_toolchain.files)

if ruby_toolchain.version.startswith("jruby"):
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
Expand Down
7 changes: 4 additions & 3 deletions ruby/private/bundle_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ load(
def _rb_bundle_install_impl(ctx):
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]

tools = [toolchain.ruby, toolchain.bundle]
tools = []
tools.extend(toolchain.files)
bundler_exe = toolchain.bundle.path

for gem in ctx.attr.gems:
Expand All @@ -35,11 +36,11 @@ def _rb_bundle_install_impl(ctx):
if _is_windows(ctx):
script = ctx.actions.declare_file("bundle_install_{}.cmd".format(ctx.label.name))
template = ctx.file._bundle_install_cmd_tpl
env.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
env.update({"PATH": _normalize_path(ctx, toolchain.ruby.dirname) + ";%PATH%"})
else:
script = ctx.actions.declare_file("bundle_install_{}.sh".format(ctx.label.name))
template = ctx.file._bundle_install_sh_tpl
env.update({"PATH": "%s:$PATH" % toolchain.bindir})
env.update({"PATH": "%s:$PATH" % toolchain.ruby.dirname})

# Calculate relative location between BUNDLE_GEMFILE and BUNDLE_PATH.
relative_dir = "../../"
Expand Down
1 change: 0 additions & 1 deletion ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def _rb_download_impl(repository_ctx):
repository_ctx.attr._build_tpl,
executable = False,
substitutions = {
"{bindir}": repr(repository_ctx.path("dist/bin")),
"{version}": version,
"{ruby_binary_name}": ruby_binary_name,
"{gem_binary_name}": gem_binary_name,
Expand Down
2 changes: 1 addition & 1 deletion ruby/private/download/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ filegroup(

rb_toolchain(
name = "toolchain",
bindir = "{bindir}",
bundle = ":bundle",
env = {env},
gem = ":gem",
ruby = ":ruby",
version = "{version}",
files = glob(["dist/**/*"]),
)

# vim: ft=bzl
9 changes: 6 additions & 3 deletions ruby/private/gem_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def _rb_gem_install_impl(ctx):

env = {}
env.update(toolchain.env)
tools = [toolchain.gem]

tools = []
tools.extend(toolchain.files)

if toolchain.version.startswith("jruby"):
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"]
tools.extend(java_toolchain.java_runtime.files.to_list())
Expand All @@ -24,11 +27,11 @@ def _rb_gem_install_impl(ctx):
if _is_windows(ctx):
gem_install = ctx.actions.declare_file("gem_install_{}.cmd".format(ctx.label.name))
template = ctx.file._gem_install_cmd_tpl
env.update({"PATH": _normalize_path(ctx, toolchain.bindir) + ";%PATH%"})
env.update({"PATH": _normalize_path(ctx, toolchain.ruby.dirname) + ";%PATH%"})
else:
gem_install = ctx.actions.declare_file("gem_install_{}.sh".format(ctx.label.name))
template = ctx.file._gem_install_sh_tpl
env.update({"PATH": "%s:$PATH" % toolchain.bindir})
env.update({"PATH": "%s:$PATH" % toolchain.ruby.dirname})

ctx.actions.expand_template(
template = template,
Expand Down
9 changes: 5 additions & 4 deletions ruby/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ def _rb_toolchain_impl(ctx):
ruby = ctx.executable.ruby,
bundle = ctx.executable.bundle,
gem = ctx.executable.gem,
bindir = ctx.attr.bindir,
version = ctx.attr.version,
env = ctx.attr.env,
files = ctx.files.files,
)

rb_toolchain = rule(
Expand All @@ -31,14 +31,15 @@ rb_toolchain = rule(
executable = True,
cfg = "exec",
),
"bindir": attr.string(
doc = "Path to Ruby bin/ directory",
),
"version": attr.string(
doc = "Ruby version",
),
"env": attr.string_dict(
doc = "Environment variables required by an interpreter",
),
"files": attr.label_list(
allow_files = True,
doc = "All files necessary for toolchain",
),
},
)

0 comments on commit c4180ae

Please sign in to comment.