11load ("//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
1515def 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
0 commit comments