1- load (":constants.bzl" , "TOOLCHAIN_TYPE_NAME" )
2- load (":providers.bzl" , "RubyLibrary" )
1+ load (":constants.bzl" , "RUBY_ATTRS" , "TOOLCHAIN_TYPE_NAME" )
32load (
43 "//ruby/private/tools:deps.bzl" ,
54 _transitive_deps = "transitive_deps" ,
@@ -11,14 +10,19 @@ def _to_manifest_path(ctx, file):
1110 else :
1211 return ("%s/%s" % (ctx .workspace_name , file .short_path ))
1312
14- def _ruby_binary_impl (ctx ):
13+ # Having this function allows us to override otherwise frozen attributes
14+ # such as main, srcs and deps. We use this in ruby_rspec_test rule by
15+ # adding rspec as a main, and sources, and rspec gem as a dependency.
16+ #
17+ # There could be similar situations in the future where we might want
18+ # to create a rule (eg, rubocop) that does exactly the same.
19+ def ruby_binary_macro (ctx , main , srcs , deps , args ):
1520 sdk = ctx .toolchains [TOOLCHAIN_TYPE_NAME ].ruby_runtime
1621 interpreter = sdk .interpreter [DefaultInfo ].files_to_run .executable
1722
18- main = ctx .file .main
1923 if not main :
2024 expected_name = "%s.rb" % ctx .attr .name
21- for f in ctx . attr . srcs :
25+ for f in srcs :
2226 if f .label .name == expected_name :
2327 main = f .files .to_list ()[0 ]
2428 break
@@ -30,6 +34,7 @@ def _ruby_binary_impl(ctx):
3034 )
3135
3236 executable = ctx .actions .declare_file (ctx .attr .name )
37+
3338 deps = _transitive_deps (
3439 ctx ,
3540 extra_files = [executable ],
@@ -55,41 +60,25 @@ def _ruby_binary_impl(ctx):
5560 data_runfiles = deps .data_files ,
5661 )]
5762
58- _ATTRS = {
59- "srcs" : attr .label_list (
60- allow_files = True ,
61- ),
62- "deps" : attr .label_list (
63- providers = [RubyLibrary ],
64- ),
65- "includes" : attr .string_list (),
66- "rubyopt" : attr .string_list (),
67- "data" : attr .label_list (
68- allow_files = True ,
69- ),
70- "main" : attr .label (
71- allow_single_file = True ,
72- ),
73- "_wrapper_template" : attr .label (
74- allow_single_file = True ,
75- default = "binary_wrapper.tpl" ,
76- ),
77- "_misc_deps" : attr .label_list (
78- allow_files = True ,
79- default = ["@bazel_tools//tools/bash/runfiles" ],
80- ),
81- }
63+ def ruby_binary_impl (ctx ):
64+ return ruby_binary_macro (
65+ ctx ,
66+ ctx .file .main ,
67+ ctx .attr .srcs ,
68+ ctx .attr .deps ,
69+ ctx .attr .args ,
70+ )
8271
8372ruby_binary = rule (
84- implementation = _ruby_binary_impl ,
85- attrs = _ATTRS ,
73+ implementation = ruby_binary_impl ,
74+ attrs = RUBY_ATTRS ,
8675 executable = True ,
8776 toolchains = [TOOLCHAIN_TYPE_NAME ],
8877)
8978
9079ruby_test = rule (
91- implementation = _ruby_binary_impl ,
92- attrs = _ATTRS ,
80+ implementation = ruby_binary_impl ,
81+ attrs = RUBY_ATTRS ,
9382 test = True ,
9483 toolchains = [TOOLCHAIN_TYPE_NAME ],
9584)
0 commit comments