Skip to content

Commit

Permalink
Support per-target Ruby toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
sushain97 committed Jul 15, 2024
1 parent 32a63c7 commit 0168948
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/repository_rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ruby/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`.
"env_inherit": attr.string_list(
doc = "List of environment variable names to be inherited by the test runner.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use when running the script.",
providers = [platform_common.ToolchainInfo],
),
"_binary_cmd_tpl": attr.label(
allow_single_file = True,
default = "@rules_ruby//ruby/private/binary:binary.cmd.tpl",
Expand All @@ -63,6 +67,8 @@ Supports `$(location)` expansion for targets from `srcs`, `data` and `deps`.
# buildifier: disable=function-docstring
def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {}, java_bin = ""):
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]

binary_path = ""
locate_binary_in_runfiles = ""
Expand Down
10 changes: 10 additions & 0 deletions ruby/private/bundle_fetch.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _GEM_INSTALL_BUILD_FRAGMENT = """
rb_gem_install(
name = "{name}",
gem = "{cache_path}/{gem}",
ruby = {ruby},
)
"""

Expand All @@ -31,6 +32,7 @@ rb_binary(
name = "{name}",
main = "{path}/{name}",
deps = ["//:{repository_name}"],
ruby = {ruby},
)
"""

Expand Down Expand Up @@ -136,6 +138,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
gem_fragments = []
gem_install_fragments = []
gem_checksums = {}
ruby_toolchain_attr = "None" if repository_ctx.attr.ruby == None else '"{}"'.format(repository_ctx.attr.ruby)
repository_name = _normalize_bzlmod_repository_name(repository_ctx.name)

# Fetch gems and expose them as `rb_gem()` targets.
Expand Down Expand Up @@ -164,6 +167,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
name = gemfile_lock.bundler.full_name,
gem = gemfile_lock.bundler.filename,
cache_path = cache_path,
ruby = ruby_toolchain_attr,
),
)

Expand All @@ -178,6 +182,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
name = executable,
repository_name = repository_name,
path = BINSTUBS_LOCATION.partition("/")[-1],
ruby = ruby_toolchain_attr,
),
)
repository_ctx.template(
Expand All @@ -203,6 +208,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
"{gem_fragments}": "".join(gem_fragments),
"{gem_install_fragments}": "".join(gem_install_fragments),
"{env}": repr(repository_ctx.attr.env),
"{ruby}": ruby_toolchain_attr,
},
)

Expand Down Expand Up @@ -250,6 +256,10 @@ rb_bundle_fetch = repository_rule(
default = {},
doc = "SHA-256 checksums for remote gems. Keys are gem names (e.g. foobar-1.2.3), values are SHA-256 checksums.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use for installation.",
providers = [platform_common.ToolchainInfo],
),
"_build_tpl": attr.label(
allow_single_file = True,
default = "@rules_ruby//:ruby/private/bundle_fetch/BUILD.tpl",
Expand Down
1 change: 1 addition & 0 deletions ruby/private/bundle_fetch/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rb_bundle_install(
gemfile = "{gemfile_path}",
gemfile_lock = "{gemfile_lock_path}",
gems = {gems},
ruby = {ruby},
)

{gem_fragments}
Expand Down
6 changes: 6 additions & 0 deletions ruby/private/bundle_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ load(

def _rb_bundle_install_impl(ctx):
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]

tools = []
tools.extend(toolchain.files)
Expand Down Expand Up @@ -137,6 +139,10 @@ rb_bundle_install = rule(
"env": attr.string_dict(
doc = "Environment variables to use during installation.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use when installing the gem.",
providers = [platform_common.ToolchainInfo],
),
"_runfiles_library": attr.label(
allow_single_file = True,
default = "@bazel_tools//tools/bash/runfiles",
Expand Down
6 changes: 6 additions & 0 deletions ruby/private/gem_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def _rb_gem_install_impl(ctx):
gem = ctx.file.gem
install_dir = ctx.actions.declare_directory(gem.basename[:-4])
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
if ctx.attr.ruby != None:
toolchain = ctx.attr.ruby[platform_common.ToolchainInfo]

env = {}
env.update(toolchain.env)
Expand Down Expand Up @@ -71,6 +73,10 @@ rb_gem_install = rule(
mandatory = True,
doc = "Gem file to install.",
),
"ruby": attr.label(
doc = "Override Ruby toolchain to use when installing the gem.",
providers = [platform_common.ToolchainInfo],
),
"_gem_install_cmd_tpl": attr.label(
allow_single_file = True,
default = "@rules_ruby//ruby/private/gem_install:gem_install.cmd.tpl",
Expand Down

0 comments on commit 0168948

Please sign in to comment.