Skip to content

Commit a4ea22b

Browse files
yuguikigster
authored andcommitted
Expose rake, erb and other commands as sh_binary (#28)
* Add binaries from default gems into the host_runtime * Add tests for run bundled commands * Ran buildifier before buildifier-check 2 fix tests
1 parent bf352ee commit a4ea22b

File tree

12 files changed

+196
-3
lines changed

12 files changed

+196
-3
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ DEPENDENCIES
2626
rubocop (~> 0.76.0)
2727

2828
BUNDLED WITH
29-
1.17.3
29+
2.0.2

ruby/private/BUILD.host_runtime.tpl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ sh_binary(
2727
data = [":runtime"],
2828
)
2929

30+
sh_binary(
31+
name = "irb",
32+
srcs = ["irb_bin"],
33+
)
34+
35+
sh_binary(
36+
name = "erb",
37+
srcs = ["erb_bin"],
38+
)
39+
40+
sh_binary(
41+
name = "rake",
42+
srcs = ["rake_bin"],
43+
)
44+
45+
sh_binary(
46+
name = "rdoc",
47+
srcs = ["rdoc_bin"],
48+
)
49+
50+
sh_binary(
51+
name = "ri",
52+
srcs = ["ri_bin"],
53+
)
54+
55+
sh_binary(
56+
name = "gem",
57+
srcs = ["gem_bin"],
58+
)
59+
3060
cc_import(
3161
name = "libruby",
3262
hdrs = glob({hdrs}),

ruby/private/host_runtime.bzl

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,41 @@ def _install_dirs(ctx, ruby, *names):
3737
ctx.symlink(path, rel_path)
3838
return rel_paths
3939

40+
def _bin_install_path(ctx, ruby, bin):
41+
"""Transform the given command name "bin" to actual file name.
42+
43+
Uses the same logic as "script_installer" in tools/rbinstall.rb in Ruby.
44+
But it does not currently support RbConfig::CONFIG['program_transform_name']
45+
"""
46+
install_name = ruby.expand_rbconfig(ruby, "${bindir}/${ruby_install_name}")
47+
path = install_name.replace("ruby", bin, 1)
48+
if ctx.path(path).exists:
49+
return path
50+
51+
path = ruby.expand_rbconfig(ruby, "${bindir}/%s" % bin)
52+
if ctx.path(path).exists:
53+
return path
54+
else:
55+
return ctx.which(bin)
56+
57+
# Commands installed together with ruby command.
58+
_DEFAULT_SCRIPTS = [
59+
"irb",
60+
"rdoc",
61+
"ri",
62+
"erb",
63+
"rake",
64+
"gem",
65+
]
66+
4067
def _install_host_ruby(ctx, ruby):
4168
# Places SDK
4269
ctx.symlink(ruby.interpreter_realpath, ruby.rel_interpreter_path)
70+
for bin_name in _DEFAULT_SCRIPTS:
71+
script_path = _bin_install_path(ctx, ruby, bin_name)
72+
if not script_path:
73+
fail("Failed to locate %s" % bin_name)
74+
ctx.symlink(script_path, "%s_bin" % bin_name)
4375

4476
# Places the interpreter at a predictable place regardless of the actual binary name
4577
# so that bundle_install can depend on it.
@@ -67,8 +99,8 @@ def _install_host_ruby(ctx, ruby):
6799

68100
return struct(
69101
includedirs = _install_dirs(ctx, ruby, "rubyarchhdrdir", "rubyhdrdir"),
70-
static_library = _relativate(static_library),
71102
shared_library = _relativate(shared_library),
103+
static_library = _relativate(static_library),
72104
)
73105

74106
def _ruby_host_runtime_impl(ctx):
@@ -107,7 +139,6 @@ def _ruby_host_runtime_impl(ctx):
107139
)
108140

109141
ruby_host_runtime = repository_rule(
110-
implementation = _ruby_host_runtime_impl,
111142
attrs = {
112143
"interpreter_path": attr.string(),
113144
"_install_bundler": attr.label(
@@ -129,4 +160,5 @@ ruby_host_runtime = repository_rule(
129160
allow_single_file = True,
130161
),
131162
},
163+
implementation = _ruby_host_runtime_impl,
132164
)

ruby/tests/BUILD.bazel

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,70 @@ sh_test(
264264
data = [":load_path_in_runfiles_container_image"],
265265
tags = ["docker"],
266266
)
267+
268+
## Tests for bundled commands in the runtime
269+
270+
ruby_binary(
271+
name = "diff_test",
272+
srcs = ["diff_test.rb"],
273+
)
274+
275+
sh_test(
276+
name = "irb_test",
277+
srcs = [":diff_test"],
278+
args = [
279+
"--stdin",
280+
"$(location //ruby/tests/testdata:bundled_commands/irb_input)",
281+
"--golden",
282+
"$(location //ruby/tests/testdata:bundled_commands/irb_golden)",
283+
"--",
284+
"$(location @org_ruby_lang_ruby_host//:irb)",
285+
"--echo",
286+
"--noverbose",
287+
"--prompt=simple",
288+
"--noreadline",
289+
],
290+
data = [
291+
"//ruby/tests/testdata:bundled_commands/irb_golden",
292+
"//ruby/tests/testdata:bundled_commands/irb_input",
293+
"@org_ruby_lang_ruby_host//:irb",
294+
],
295+
)
296+
297+
sh_test(
298+
name = "erb_test",
299+
srcs = [":diff_test"],
300+
args = [
301+
"--stdin",
302+
"$(location //ruby/tests/testdata:bundled_commands/erb_input)",
303+
"--golden",
304+
"$(location //ruby/tests/testdata:bundled_commands/erb_golden)",
305+
"--",
306+
"$(location @org_ruby_lang_ruby_host//:erb)",
307+
"values=foo,bar,baz",
308+
],
309+
data = [
310+
"//ruby/tests/testdata:bundled_commands/erb_golden",
311+
"//ruby/tests/testdata:bundled_commands/erb_input",
312+
"@org_ruby_lang_ruby_host//:erb",
313+
],
314+
)
315+
316+
sh_test(
317+
name = "rake_test",
318+
srcs = [":diff_test"],
319+
args = [
320+
"--golden",
321+
"$(location //ruby/tests/testdata:bundled_commands/rake_golden)",
322+
"--",
323+
"$(location @org_ruby_lang_ruby_host//:rake)",
324+
"-f",
325+
"$(location //ruby/tests/testdata:bundled_commands/rake_input)",
326+
"foo",
327+
],
328+
data = [
329+
"//ruby/tests/testdata:bundled_commands/rake_golden",
330+
"//ruby/tests/testdata:bundled_commands/rake_input",
331+
"@org_ruby_lang_ruby_host//:rake",
332+
],
333+
)

ruby/tests/diff_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/ruby -w
2+
# frozen_string_literal: true
3+
4+
require 'optparse'
5+
6+
def parse_flags(args)
7+
stdin = IO::NULL
8+
golden = nil
9+
parser = OptionParser.new do |opts|
10+
opts.on('--stdin=PATH') do |v|
11+
stdin = v
12+
end
13+
14+
opts.on('--golden=PATH') do |v|
15+
golden = v
16+
end
17+
end
18+
args = parser.parse(args)
19+
20+
[args, stdin, golden]
21+
end
22+
23+
def main(args)
24+
args, stdin, golden = parse_flags(args)
25+
args << { in: stdin }
26+
stdout = IO.popen(args, &:read)
27+
28+
IO.popen(['diff', '-B', '-c', golden, '-'], 'w') { |io| io << stdout }
29+
exit $?.exitstatus
30+
end
31+
32+
if $0 == __FILE__
33+
main(ARGV)
34+
end

ruby/tests/testdata/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ load(
55

66
package(default_visibility = ["//ruby/tests:__pkg__"])
77

8+
exports_files([
9+
"bundled_commands/erb_golden",
10+
"bundled_commands/erb_input",
11+
"bundled_commands/irb_golden",
12+
"bundled_commands/irb_input",
13+
"bundled_commands/rake_golden",
14+
"bundled_commands/rake_input",
15+
])
16+
817
#
918
# Dependency Graph::
1019
#
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo
2+
bar
3+
baz
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
% values.split(',').each do |value|
2+
<%= value %>
3+
% end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
=> T
2+
=> #<struct T a=1, b=2, c=3>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
T = Struct.new(:a, :b, :c)
2+
T.new(1, 2, 3)

0 commit comments

Comments
 (0)