Skip to content

Commit d987f93

Browse files
author
graham jenson
committed
[Feature] install ruby if none exists
1 parent 4189769 commit d987f93

20 files changed

+218
-226
lines changed

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.6.5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.6.5

examples/simple_script/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ruby_binary(
1212
main = "script.rb",
1313
deps = [
1414
"//lib:foo",
15-
"@bundle//:libs",
15+
"@bundle//:awesome_print",
1616
],
1717
)
1818

@@ -26,6 +26,7 @@ ruby_test(
2626
rubyopt = ["-rrspec/autorun"], # require autorun because it is needed
2727
deps = [
2828
"//lib:foo",
29-
"@bundle//:libs",
29+
"@bundle//:awesome_print",
30+
"@bundle//:rspec",
3031
],
3132
)

examples/simple_script/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ source "https://rubygems.org"
55
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
66

77
gem 'rspec', '~> 3.7.0'
8+
gem 'awesome_print'

examples/simple_script/Gemfile.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
awesome_print (1.8.0)
45
diff-lcs (1.3)
56
rspec (3.7.0)
67
rspec-core (~> 3.7.0)
@@ -20,7 +21,8 @@ PLATFORMS
2021
ruby
2122

2223
DEPENDENCIES
24+
awesome_print
2325
rspec (~> 3.7.0)
2426

2527
BUNDLED WITH
26-
1.16.1
28+
1.17.3

examples/simple_script/WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ load(
1515

1616
ruby_rules_dependencies()
1717

18-
ruby_register_toolchains()
18+
ruby_register_toolchains(version = "2.6.5")
1919

2020
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
2121

examples/simple_script/script.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
require 'openssl'
44
require 'lib/foo'
5+
require "awesome_print"
56

67
def oss_rand
78
OpenSSL::BN.rand(512).to_s
89
end
910

1011
puts Foo.aha + " " + oss_rand
12+
13+
puts $LOAD_PATH
14+
15+
ap Class

ruby/private/bundle/bundle.bzl

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
load("//ruby/private:constants.bzl", "RULES_RUBY_WORKSPACE_NAME")
22

3-
def _get_interpreter_label(repository_ctx, ruby_sdk):
4-
# TODO(yugui) Support windows as rules_nodejs does
5-
return Label("%s//:ruby" % ruby_sdk)
3+
_DEFAULT_VERSION = "2.0.2"
64

7-
def _get_bundler_label(repository_ctx, ruby_sdk):
8-
# TODO(yugui) Support windows as rules_nodejs does
9-
return Label("%s//:bundler/exe/bundler" % ruby_sdk)
5+
def install_bundler(ctx, interpreter, install_bundler, dest, version = _DEFAULT_VERSION):
6+
args = ["env", "-i", interpreter, install_bundler, version, dest]
7+
environment = {"RUBYOPT": "--disable-gems"}
108

11-
def _get_bundler_lib_label(repository_ctx, ruby_sdk):
12-
# TODO(yugui) Support windows as rules_nodejs does
13-
return Label("%s//:bundler/lib" % ruby_sdk)
9+
result = ctx.execute(args, environment = environment)
10+
if result.return_code:
11+
message = "Failed to evaluate ruby snippet with {}: {}".format(
12+
interpreter,
13+
result.stderr,
14+
)
15+
fail(message)
1416

1517
def bundle_install_impl(ctx):
1618
ctx.symlink(ctx.attr.gemfile, "Gemfile")
1719
ctx.symlink(ctx.attr.gemfile_lock, "Gemfile.lock")
1820
ctx.symlink(ctx.attr._create_bundle_build_file, "create_bundle_build_file.rb")
21+
ctx.symlink(ctx.attr._install_bundler, "install_bundler.rb")
1922

2023
# TODO(kig) Make Gemspec reference from Gemfile actually work
2124
if ctx.attr.gemspec:
2225
ctx.symlink(ctx.attr.gemspec, ctx.path(ctx.attr.gemspec).basename)
2326

24-
ruby = _get_interpreter_label(ctx, ctx.attr.ruby_sdk)
25-
bundler = _get_bundler_label(ctx, ctx.attr.ruby_sdk)
27+
ruby = ctx.attr.ruby_interpreter
28+
interpreter_path = ctx.path(ruby)
29+
30+
install_bundler(
31+
ctx,
32+
interpreter_path,
33+
"install_bundler.rb",
34+
"bundler",
35+
)
36+
37+
bundler = Label("//:bundler/exe/bundler")
2638

2739
# Install the Gems into the workspace
2840
args = [
@@ -31,8 +43,8 @@ def bundle_install_impl(ctx):
3143
ctx.path(ruby), # ruby
3244
"--disable-gems", # prevent the addition of gem installation directories to the default load path
3345
"-I", # Used to tell Ruby where to load the library scripts
34-
ctx.path(bundler).dirname.dirname.get_child("lib"),
35-
ctx.path(bundler), # run
46+
"bundler/lib",
47+
"bundler/exe/bundler", # run
3648
"install", # > bundle install
3749
"--deployment", # In the deployment mode, gems are dumped to --path and frozen; also .bundle/config file is created
3850
"--standalone", # Makes a bundle that can work without depending on Rubygems or Bundler at runtime.
@@ -58,7 +70,7 @@ def bundle_install_impl(ctx):
5870
ctx.path(ruby), # ruby interpreter
5971
"--disable-gems", # prevent the addition of gem installation directories to the default load path
6072
"-I", # -I lib (adds this folder to $LOAD_PATH where ruby searchesf for things)
61-
ctx.path(bundler).dirname.dirname.get_child("lib"),
73+
"bundler/lib",
6274
"create_bundle_build_file.rb", # The template used to created bundle file
6375
"BUILD.bazel", # Bazel build file (can be empty)
6476
"Gemfile.lock", # Gemfile.lock where we list all direct and transitive dependencies
@@ -85,7 +97,10 @@ bundle_install = repository_rule(
8597
implementation = bundle_install_impl,
8698
attrs = {
8799
"ruby_sdk": attr.string(
88-
default = "@org_ruby_lang_ruby_host",
100+
default = "@org_ruby_lang_ruby_toolchain",
101+
),
102+
"ruby_interpreter": attr.label(
103+
default = "@org_ruby_lang_ruby_toolchain//:ruby",
89104
),
90105
"gemfile": attr.label(
91106
allow_single_file = True,
@@ -99,6 +114,12 @@ bundle_install = repository_rule(
99114
"excludes": attr.string_list_dict(
100115
doc = "List of glob patterns per gem to be excluded from the library",
101116
),
117+
"_install_bundler": attr.label(
118+
default = "%s//ruby/private/bundle:install_bundler.rb" % (
119+
RULES_RUBY_WORKSPACE_NAME
120+
),
121+
allow_single_file = True,
122+
),
102123
"_create_bundle_build_file": attr.label(
103124
default = "%s//ruby/private/bundle:create_bundle_build_file.rb" % (
104125
RULES_RUBY_WORKSPACE_NAME
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load(":constants.bzl", "TOOLCHAIN_TYPE_NAME")
2+
3+
# Gem install is a rule that takes a Gemfile and Gemfile.lock and installs
4+
def _gem_install_impl(ctx):
5+
if not ctx.attr.srcs and not ctx.attr.deps:
6+
fail("At least srcs or deps must be present")
7+
8+
deps = _transitive_deps(ctx)
9+
return [
10+
DefaultInfo(
11+
default_runfiles = deps.default_files,
12+
data_runfiles = deps.data_files,
13+
),
14+
RubyLibrary(
15+
transitive_ruby_srcs = deps.srcs,
16+
ruby_incpaths = deps.incpaths,
17+
rubyopt = deps.rubyopt,
18+
),
19+
]
20+
21+
gem_install = rule(
22+
implementation = _gem_install_impl,
23+
attrs = {
24+
"gemfile": attr.label_list(
25+
allow_files = True,
26+
),
27+
"includes": attr.string_list(),
28+
"rubyopt": attr.string_list(),
29+
"deps": attr.label_list(
30+
providers = [RubyLibrary],
31+
),
32+
"data": attr.label_list(
33+
allow_files = True,
34+
),
35+
},
36+
toolchains = [TOOLCHAIN_TYPE_NAME],
37+
)

0 commit comments

Comments
 (0)