11load ("//ruby/private:constants.bzl" , "RULES_RUBY_WORKSPACE_NAME" )
22
3- def install_bundler (ctx , interpreter , install_bundler , dest , version ):
3+ BUNDLE_INSTALL_PATH = "lib"
4+ BUNDLE_BINARY = "bundler/exe/bundler"
5+
6+ # TODO: do not hard-code this
7+ GEM_HOME = "lib/ruby/2.5.0/"
8+
9+ def run_bundler (ctx , interpreter , environment , extra_args ):
10+ # Now we are running bundle install
11+ args = [
12+ interpreter , # ruby
13+ "--enable=gems" , # bundler must run with rubygems enabled
14+ "-I" ,
15+ "." ,
16+ "-I" , # Used to tell Ruby where to load the library scripts
17+ "lib" , # Add vendor/bundle to the list of resolvers
18+ BUNDLE_BINARY , # our binary
19+ ] + extra_args
20+
21+ # print("running bundler with args\n", args)
22+
23+ result = ctx .execute (
24+ args ,
25+ quiet = False ,
26+ )
27+
28+ return result
29+
30+ # Sets local bundler config values
31+ def set_bundler_config (ctx , interpreter , environment ):
32+ # Bundler is deprecating various flags in favor of the configuration.
33+ # HOWEVER — for reasons I can't explain, Bazel runs "bundle install" *prior*
34+ # to setting these flags. So the flags are then useless until we can force the
35+ # order and ensure that Bazel first downloads Bundler, then sets config, then
36+ # runs bundle install. Until then, it's a wild west out here.
37+ #
38+ # Set local configuration options for bundler
39+ bundler_config = {
40+ "binstubs" : "bin" ,
41+ "deployment" : "'true'" ,
42+ "standalone" : "'true'" ,
43+ "frozen" : "'true'" ,
44+ "without" : "development,test" ,
45+ "path" : "lib" ,
46+ "jobs" : "20" ,
47+ }
48+
49+ for option , value in [(option , value ) for option , value in bundler_config .items ()]:
50+ args = [
51+ "config" ,
52+ "--local" ,
53+ option ,
54+ value ,
55+ ]
56+
57+ result = run_bundler (ctx , interpreter , environment , args )
58+ if result .return_code :
59+ message = "Failed to set bundle config {} to {}: {}" .format (
60+ option ,
61+ value ,
62+ result .stderr ,
63+ )
64+ fail (message )
65+
66+ def install_bundler (ctx , interpreter , install_bundler , dest , version , environment ):
467 args = [interpreter , install_bundler , version , dest ]
5- environment = {"RUBYOPT" : "--disable-gems" }
668
769 result = ctx .execute (args , environment = environment )
870 if result .return_code :
@@ -22,42 +84,47 @@ def bundle_install_impl(ctx):
2284 if ctx .attr .gemspec :
2385 ctx .symlink (ctx .attr .gemspec , ctx .path (ctx .attr .gemspec ).basename )
2486
87+ environment = {"RUBYOPT" : "--enable-gems" , "GEM_HOME" : GEM_HOME }
88+
2589 ruby = ctx .attr .ruby_interpreter
26- interpreter_path = ctx .path (ruby )
90+ interpreter = ctx .path (ruby )
2791
2892 install_bundler (
2993 ctx ,
30- interpreter_path ,
94+ interpreter ,
3195 "install_bundler.rb" ,
3296 "bundler" ,
3397 ctx .attr .version ,
98+ environment ,
3499 )
100+ bundler = Label ("//:" + BUNDLE_BINARY )
35101
36- bundler = Label ("//:bundler/exe/bundler" )
102+ # Set Bundler config in the .bundle/config file
103+ set_bundler_config (
104+ ctx ,
105+ interpreter ,
106+ environment ,
107+ )
37108
38- # Install the Gems into the workspace
39- args = [
40- ctx .path (ruby ), # ruby
41- "--disable-gems" , # prevent the addition of gem installation directories to the default load path
42- "-I" , # Used to tell Ruby where to load the library scripts
43- "bundler/lib" ,
44- "bundler/exe/bundler" , # run
45- "install" , # > bundle install
46- "--deployment" , # In the deployment mode, gems are dumped to --path and frozen; also .bundle/config file is created
47- "--standalone" , # Makes a bundle that can work without depending on Rubygems or Bundler at runtime.
48- "--frozen" , # Do not allow the Gemfile.lock to be updated after this install.
49- "--binstubs=bin" , # Creates a directory and place any executables from the gem there.
50- "--path=lib" , # The location to install the specified gems to.
51- ]
52- result = ctx .execute (args , quiet = False )
109+ result = run_bundler (
110+ ctx ,
111+ interpreter ,
112+ environment ,
113+ [
114+ "install" , # > bundle install
115+ "--standalone" , # Makes a bundle that can work without depending on Rubygems or Bundler at runtime.
116+ "--binstubs=bin" , # Creates a directory and place any executables from the gem there.
117+ "--path={}" .format (BUNDLE_INSTALL_PATH ), # The location to install the specified gems to.
118+ ],
119+ )
53120
54121 if result .return_code :
55122 fail ("Failed to install gems: %s%s" % (result .stdout , result .stderr ))
56123
57124 # Create the BUILD file to expose the gems to the WORKSPACE
58125 args = [
59126 ctx .path (ruby ), # ruby interpreter
60- "--disable- gems" , # prevent the addition of gem installation directories to the default load path
127+ "--enable= gems" , # prevent the addition of gem installation directories to the default load path
61128 "-I" , # -I lib (adds this folder to $LOAD_PATH where ruby searchesf for things)
62129 "bundler/lib" ,
63130 "create_bundle_build_file.rb" , # The template used to created bundle file
@@ -81,7 +148,7 @@ def bundle_install_impl(ctx):
81148 },
82149 )
83150
84- bundle_install = repository_rule (
151+ ruby_bundle = repository_rule (
85152 implementation = bundle_install_impl ,
86153 attrs = {
87154 "ruby_sdk" : attr .string (
@@ -98,7 +165,7 @@ bundle_install = repository_rule(
98165 allow_single_file = True ,
99166 ),
100167 "version" : attr .string (
101- default = "2.0 .2" ,
168+ default = "2.1 .2" ,
102169 ),
103170 "gemspec" : attr .label (
104171 allow_single_file = True ,
0 commit comments