Skip to content

Commit 5a4ce48

Browse files
committed
Implement Ruby gem building with Bazel
This adds 2 new targets to Ruby Bazel build. Each target copies all necessary files for gem it needs to build and then packages into a gem file that is stored in Bazel output directory. For now there is no way to publish gem to RubyGems, but this should be easy to implement. Please see the following table to understand what commands to use and what gem files each command generates. | command | output | | bazel build //rb:selenium-devtools | bazel-bin/rb/selenium-devtools.gem | | bazel build //rb:selenium-webdriver | bazel-bin/rb/selenium-webdriver.gem |
1 parent d64bc88 commit 5a4ce48

File tree

6 files changed

+134
-16
lines changed

6 files changed

+134
-16
lines changed

rb/BUILD.bazel

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//common:defs.bzl", "copy_file")
2+
load("//rb:defs.bzl", "rb_gem")
23
load(
34
"@coinbase_rules_ruby//ruby:defs.bzl",
45
"rb_binary",
@@ -46,6 +47,36 @@ copy_file(
4647
out = "selenium_server_deploy.jar",
4748
)
4849

50+
genrule(
51+
name = "license",
52+
srcs = ["//:license"],
53+
outs = [
54+
"LICENSE",
55+
"NOTICE",
56+
],
57+
cmd = "cp $(locations //:license) $(@D)",
58+
)
59+
60+
rb_gem(
61+
name = "selenium-webdriver",
62+
gemspec = "selenium-webdriver.gemspec",
63+
deps = [
64+
":bidi",
65+
":chrome",
66+
":common",
67+
":edge",
68+
":firefox",
69+
":ie",
70+
":safari",
71+
],
72+
)
73+
74+
rb_gem(
75+
name = "selenium-devtools",
76+
gemspec = "selenium-devtools.gemspec",
77+
deps = [":devtools"],
78+
)
79+
4980
rb_library(
5081
name = "bidi",
5182
srcs = glob([
@@ -88,7 +119,7 @@ rb_library(
88119
":find-elements",
89120
":get-attribute",
90121
":is-displayed",
91-
"//:license",
122+
":license",
92123
],
93124
deps = [
94125
"@bundle//:childprocess",
@@ -146,17 +177,13 @@ rb_library(
146177
rb_library(
147178
name = "devtools",
148179
srcs = glob([
149-
"selenium-devtools.gemspec",
180+
"Gemfile",
150181
"lib/selenium/devtools.rb",
151182
"lib/selenium/devtools/version.rb",
152-
"Gemfile",
153-
"CHANGES",
154-
"README.md",
183+
"selenium-devtools.gemspec",
155184
]),
156-
data = ["//:license"] + [":cdp-" + n for n in CDP_VERSIONS],
157-
deps = [
158-
"@bundle//:websocket",
159-
],
185+
data = [":license"] + [":cdp-" + n for n in CDP_VERSIONS],
186+
deps = ["@bundle//:websocket"],
160187
)
161188

162189
[genrule(
@@ -170,9 +197,7 @@ rb_library(
170197
"lib/selenium/devtools/" + n + ".rb",
171198
],
172199
cmd = "ruby $(location :cdp-generate) $(location //common/devtools/chromium/" + n + ":browser_protocol) $(location //common/devtools/chromium/" + n + ":js_protocol) $(OUTS) " + n,
173-
tools = [
174-
":cdp-generate",
175-
],
200+
tools = [":cdp-generate"],
176201
) for n in CDP_VERSIONS]
177202

178203
rb_binary(
@@ -542,9 +567,7 @@ rb_binary(
542567
"rb/README.md",
543568
],
544569
main = "@bundle//:bin/yard",
545-
deps = [
546-
"@bundle//:yard",
547-
],
570+
deps = ["@bundle//:yard"],
548571
)
549572

550573
rb_binary(

rb/NOTICE

Lines changed: 0 additions & 1 deletion
This file was deleted.

rb/defs.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//rb/private:rb_gem.bzl", _rb_gem = "rb_gem")
2+
3+
rb_gem = _rb_gem

rb/private/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package(default_visibility = ["//rb/private:__pkg__"])
2+
3+
exports_files(
4+
["gem_builder.tpl"],
5+
visibility = ["//visibility:public"],
6+
)

rb/private/gem_builder.tpl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'fileutils'
4+
require 'rubygems/package'
5+
require 'tmpdir'
6+
7+
gemspec = File.read('{gemspec}')
8+
gem_file = File.expand_path('{gem_filename}', '{bazel_out_dir}')
9+
10+
def copy_srcs(from:, to:)
11+
Dir.chdir(from) do
12+
Dir.glob('**/*').each do |src|
13+
dst = File.join(to, src)
14+
if File.directory?(src)
15+
FileUtils.mkdir_p(dst)
16+
else
17+
FileUtils.cp(src, dst)
18+
end
19+
end
20+
end
21+
end
22+
23+
Dir.mktmpdir do |tmpdir|
24+
# First copy file that live in gem directory.
25+
copy_srcs(from: File.dirname('{gemspec}'), to: tmpdir)
26+
# Then copy all the files that generated by target dependencies.
27+
copy_srcs(from: '{bazel_out_dir}', to: tmpdir)
28+
29+
Dir.chdir(tmpdir) do
30+
spec = eval(gemspec)
31+
file = Gem::Package.build(spec)
32+
FileUtils.mv(file, gem_file)
33+
end
34+
end

rb/private/rb_gem.bzl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
def _rb_gem_impl(ctx):
2+
gem_builder = ctx.actions.declare_file("gem_builder.rb")
3+
4+
inputs = []
5+
for dep in ctx.attr.deps:
6+
inputs.extend(dep.files.to_list())
7+
inputs.extend(dep[DefaultInfo].data_runfiles.files.to_list())
8+
9+
ctx.actions.expand_template(
10+
template = ctx.file._gem_builder_template,
11+
output = gem_builder,
12+
substitutions = {
13+
"{bazel_out_dir}": ctx.outputs.gem.dirname,
14+
"{gem_filename}": ctx.label.name + ".gem",
15+
"{gemspec}": ctx.file.gemspec.path,
16+
},
17+
)
18+
19+
ctx.actions.run(
20+
inputs = inputs,
21+
executable = gem_builder,
22+
outputs = [ctx.outputs.gem],
23+
execution_requirements = {
24+
"no-sandbox": "1", # allow to traverse directory symlinks
25+
},
26+
)
27+
28+
rb_gem = rule(
29+
_rb_gem_impl,
30+
attrs = {
31+
"srcs": attr.label_list(
32+
allow_files = True,
33+
),
34+
"gemspec": attr.label(
35+
allow_single_file = True,
36+
mandatory = True,
37+
),
38+
"deps": attr.label_list(
39+
allow_files = True,
40+
),
41+
"data": attr.label_list(
42+
allow_empty = True,
43+
allow_files = True,
44+
),
45+
"_gem_builder_template": attr.label(
46+
allow_single_file = True,
47+
default = Label("//rb/private:gem_builder.tpl"),
48+
),
49+
},
50+
outputs = {
51+
"gem": "%{name}.gem",
52+
},
53+
)

0 commit comments

Comments
 (0)