Skip to content

Commit 3a554f4

Browse files
author
Graham Jenson
authored
Merge pull request #34 from bazelruby/graham/install-ruby-if
[Feature] install ruby if none exists
2 parents e473106 + 2b01905 commit 3a554f4

File tree

19 files changed

+185
-227
lines changed

19 files changed

+185
-227
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: 2 additions & 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

@@ -27,4 +27,5 @@ bundle_install(
2727
name = "bundle",
2828
gemfile = "//:Gemfile",
2929
gemfile_lock = "//:Gemfile.lock",
30+
version = "2.0.2",
3031
)

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: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
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+
def install_bundler(ctx, interpreter, install_bundler, dest, version):
4+
args = ["env", "-i", interpreter, install_bundler, version, dest]
5+
environment = {"RUBYOPT": "--disable-gems"}
66

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)
10-
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)
7+
result = ctx.execute(args, environment = environment)
8+
if result.return_code:
9+
message = "Failed to evaluate ruby snippet with {}: {}".format(
10+
interpreter,
11+
result.stderr,
12+
)
13+
fail(message)
1414

1515
def bundle_install_impl(ctx):
1616
ctx.symlink(ctx.attr.gemfile, "Gemfile")
1717
ctx.symlink(ctx.attr.gemfile_lock, "Gemfile.lock")
1818
ctx.symlink(ctx.attr._create_bundle_build_file, "create_bundle_build_file.rb")
19+
ctx.symlink(ctx.attr._install_bundler, "install_bundler.rb")
1920

2021
# TODO(kig) Make Gemspec reference from Gemfile actually work
2122
if ctx.attr.gemspec:
2223
ctx.symlink(ctx.attr.gemspec, ctx.path(ctx.attr.gemspec).basename)
2324

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

2738
# Install the Gems into the workspace
2839
args = [
@@ -31,8 +42,8 @@ def bundle_install_impl(ctx):
3142
ctx.path(ruby), # ruby
3243
"--disable-gems", # prevent the addition of gem installation directories to the default load path
3344
"-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
45+
"bundler/lib",
46+
"bundler/exe/bundler", # run
3647
"install", # > bundle install
3748
"--deployment", # In the deployment mode, gems are dumped to --path and frozen; also .bundle/config file is created
3849
"--standalone", # Makes a bundle that can work without depending on Rubygems or Bundler at runtime.
@@ -58,7 +69,7 @@ def bundle_install_impl(ctx):
5869
ctx.path(ruby), # ruby interpreter
5970
"--disable-gems", # prevent the addition of gem installation directories to the default load path
6071
"-I", # -I lib (adds this folder to $LOAD_PATH where ruby searchesf for things)
61-
ctx.path(bundler).dirname.dirname.get_child("lib"),
72+
"bundler/lib",
6273
"create_bundle_build_file.rb", # The template used to created bundle file
6374
"BUILD.bazel", # Bazel build file (can be empty)
6475
"Gemfile.lock", # Gemfile.lock where we list all direct and transitive dependencies
@@ -85,20 +96,32 @@ bundle_install = repository_rule(
8596
implementation = bundle_install_impl,
8697
attrs = {
8798
"ruby_sdk": attr.string(
88-
default = "@org_ruby_lang_ruby_host",
99+
default = "@org_ruby_lang_ruby_toolchain",
100+
),
101+
"ruby_interpreter": attr.label(
102+
default = "@org_ruby_lang_ruby_toolchain//:ruby",
89103
),
90104
"gemfile": attr.label(
91105
allow_single_file = True,
92106
),
93107
"gemfile_lock": attr.label(
94108
allow_single_file = True,
95109
),
110+
"version": attr.string(
111+
default = "2.0.2",
112+
),
96113
"gemspec": attr.label(
97114
allow_single_file = True,
98115
),
99116
"excludes": attr.string_list_dict(
100117
doc = "List of glob patterns per gem to be excluded from the library",
101118
),
119+
"_install_bundler": attr.label(
120+
default = "%s//ruby/private/bundle:install_bundler.rb" % (
121+
RULES_RUBY_WORKSPACE_NAME
122+
),
123+
allow_single_file = True,
124+
),
102125
"_create_bundle_build_file": attr.label(
103126
default = "%s//ruby/private/bundle:create_bundle_build_file.rb" % (
104127
RULES_RUBY_WORKSPACE_NAME

ruby/private/bundler.bzl

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)