Skip to content

Commit

Permalink
Install gems into separate BUNDLE_PATH and use bundle exec
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Jul 21, 2023
1 parent 0411d43 commit 0c2c290
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/gem/lib/gem/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rb_binary(
main = "add.rb",
deps = [
":add",
"//:gem",
"@bundle",
],
)
Expand Down
17 changes: 15 additions & 2 deletions ruby/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Use a built-in `args` attribute to pass extra arguments to the script.
),
}

def generate_rb_binary_script(ctx, binary, args = []):
def generate_rb_binary_script(ctx, binary, bundler = False, args = []):
windows_constraint = ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]
is_windows = ctx.target_platform_has_constraint(windows_constraint)
toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
Expand All @@ -57,6 +57,11 @@ def generate_rb_binary_script(ctx, binary, args = []):
script = ctx.actions.declare_file("{}.rb.sh".format(ctx.label.name))
template = ctx.file._binary_sh_tpl

if bundler:
bundler_command = "bundle exec"
else:
bundler_command = ""

args = " ".join(args)
args = ctx.expand_location(args)

Expand All @@ -68,15 +73,16 @@ def generate_rb_binary_script(ctx, binary, args = []):
"{args}": args,
"{binary}": binary_path,
"{toolchain_bindir}": toolchain_bindir,
"{bundler_command}": bundler_command,
"{ruby_binary_name}": toolchain.ruby.basename,
},
)

return script

def rb_binary_impl(ctx):
bundler = False
env = {}
script = generate_rb_binary_script(ctx, ctx.executable.main)
transitive_data = get_transitive_data(ctx.files.data, ctx.attr.deps).to_list()
transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list()
tools = []
Expand All @@ -94,9 +100,16 @@ def rb_binary_impl(ctx):
if file.basename == "Gemfile":
env["BUNDLE_GEMFILE"] = file.path

for dep in ctx.attr.deps:
if dep.label.workspace_name == "bundle":
bundler = True
env["BUNDLE_PATH"] = dep.label.workspace_root

runfiles = ctx.runfiles(transitive_data + transitive_srcs + tools)
env.update(ctx.attr.env)

script = generate_rb_binary_script(ctx, ctx.executable.main, bundler)

return [
DefaultInfo(
executable = script,
Expand Down
2 changes: 1 addition & 1 deletion ruby/private/binary/binary.cmd.tpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@set PATH={toolchain_bindir};%PATH%
@{ruby_binary_name} {binary} {args} %*
@{bundler_command} {ruby_binary_name} {binary} {args} %*
2 changes: 1 addition & 1 deletion ruby/private/binary/binary.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

export PATH={toolchain_bindir}:$PATH
{ruby_binary_name} {binary} {args} $@
{bundler_command} {ruby_binary_name} {binary} {args} $@
2 changes: 2 additions & 0 deletions ruby/private/bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _BINSTUB_CMD = """@ruby -x "%~f0" %*

def _rb_bundle_impl(repository_ctx):
binstubs_path = repository_ctx.path("bin")
bundle_path = repository_ctx.path(".")
gemfile = repository_ctx.path(repository_ctx.attr.gemfile)
toolchain_path = repository_ctx.path(Label("@rules_ruby_dist//:BUILD")).dirname

Expand Down Expand Up @@ -37,6 +38,7 @@ def _rb_bundle_impl(repository_ctx):
"BUNDLE_BIN": repr(binstubs_path),
"BUNDLE_GEMFILE": gemfile.basename,
"BUNDLE_IGNORE_CONFIG": "1",
"BUNDLE_PATH": repr(bundle_path),
"BUNDLE_SHEBANG": repr(ruby),
"PATH": path_separator.join([repr(ruby.dirname), repository_ctx.os.environ["PATH"]]),
},
Expand Down
5 changes: 3 additions & 2 deletions ruby/private/gem_push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def _rb_gem_push_impl(ctx):

script = generate_rb_binary_script(
ctx,
ruby_toolchain.gem,
["push", ctx.file.gem.short_path],
binary = ruby_toolchain.gem,
bundler = False,
args = ["push", ctx.file.gem.short_path],
)

runfiles = ctx.runfiles(srcs + tools)
Expand Down

0 comments on commit 0c2c290

Please sign in to comment.