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+ _DEFAULT_VERSION = "2.0.2"
64
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 )
5+ def install_bundler ( ctx , interpreter , install_bundler , dest , version = _DEFAULT_VERSION ):
6+ args = [ "env" , "-i" , interpreter , install_bundler , version , dest ]
7+ environment = { "RUBYOPT" : "--disable-gems" }
108
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 )
9+ result = ctx .execute (args , environment = environment )
10+ if result .return_code :
11+ message = "Failed to evaluate ruby snippet with {}: {}" .format (
12+ interpreter ,
13+ result .stderr ,
14+ )
15+ fail (message )
1416
1517def bundle_install_impl (ctx ):
1618 ctx .symlink (ctx .attr .gemfile , "Gemfile" )
1719 ctx .symlink (ctx .attr .gemfile_lock , "Gemfile.lock" )
1820 ctx .symlink (ctx .attr ._create_bundle_build_file , "create_bundle_build_file.rb" )
21+ ctx .symlink (ctx .attr ._install_bundler , "install_bundler.rb" )
1922
2023 # TODO(kig) Make Gemspec reference from Gemfile actually work
2124 if ctx .attr .gemspec :
2225 ctx .symlink (ctx .attr .gemspec , ctx .path (ctx .attr .gemspec ).basename )
2326
24- ruby = _get_interpreter_label (ctx , ctx .attr .ruby_sdk )
25- bundler = _get_bundler_label (ctx , ctx .attr .ruby_sdk )
27+ ruby = ctx .attr .ruby_interpreter
28+ interpreter_path = ctx .path (ruby )
29+
30+ install_bundler (
31+ ctx ,
32+ interpreter_path ,
33+ "install_bundler.rb" ,
34+ "bundler" ,
35+ )
36+
37+ bundler = Label ("//:bundler/exe/bundler" )
2638
2739 # Install the Gems into the workspace
2840 args = [
@@ -31,8 +43,8 @@ def bundle_install_impl(ctx):
3143 ctx .path (ruby ), # ruby
3244 "--disable-gems" , # prevent the addition of gem installation directories to the default load path
3345 "-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
46+ "bundler/ lib" ,
47+ " bundler/exe/bundler" , # run
3648 "install" , # > bundle install
3749 "--deployment" , # In the deployment mode, gems are dumped to --path and frozen; also .bundle/config file is created
3850 "--standalone" , # Makes a bundle that can work without depending on Rubygems or Bundler at runtime.
@@ -58,7 +70,7 @@ def bundle_install_impl(ctx):
5870 ctx .path (ruby ), # ruby interpreter
5971 "--disable-gems" , # prevent the addition of gem installation directories to the default load path
6072 "-I" , # -I lib (adds this folder to $LOAD_PATH where ruby searchesf for things)
61- ctx . path ( bundler ). dirname . dirname . get_child ( " lib") ,
73+ "bundler/ lib" ,
6274 "create_bundle_build_file.rb" , # The template used to created bundle file
6375 "BUILD.bazel" , # Bazel build file (can be empty)
6476 "Gemfile.lock" , # Gemfile.lock where we list all direct and transitive dependencies
@@ -85,7 +97,10 @@ bundle_install = repository_rule(
8597 implementation = bundle_install_impl ,
8698 attrs = {
8799 "ruby_sdk" : attr .string (
88- default = "@org_ruby_lang_ruby_host" ,
100+ default = "@org_ruby_lang_ruby_toolchain" ,
101+ ),
102+ "ruby_interpreter" : attr .label (
103+ default = "@org_ruby_lang_ruby_toolchain//:ruby" ,
89104 ),
90105 "gemfile" : attr .label (
91106 allow_single_file = True ,
@@ -99,6 +114,12 @@ bundle_install = repository_rule(
99114 "excludes" : attr .string_list_dict (
100115 doc = "List of glob patterns per gem to be excluded from the library" ,
101116 ),
117+ "_install_bundler" : attr .label (
118+ default = "%s//ruby/private/bundle:install_bundler.rb" % (
119+ RULES_RUBY_WORKSPACE_NAME
120+ ),
121+ allow_single_file = True ,
122+ ),
102123 "_create_bundle_build_file" : attr .label (
103124 default = "%s//ruby/private/bundle:create_bundle_build_file.rb" % (
104125 RULES_RUBY_WORKSPACE_NAME
0 commit comments