Skip to content

Commit fafab7e

Browse files
authored
Merge pull request #61 from kigster/kig/refactor-bundle-binaries
This PR refactors bundle steps and :bin generation
2 parents e556a08 + ecf0fc5 commit fafab7e

File tree

9 files changed

+126
-84
lines changed

9 files changed

+126
-84
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.6.5
1+
2.6.5

examples/simple_script/BUILD.bazel

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ ruby_binary(
5454
deps = [
5555
":lib",
5656
"//lib:foo",
57-
"@bundle//:gems",
57+
"@bundle//:bin",
5858
],
5959
)
6060

@@ -77,10 +77,7 @@ ruby_test(
7777
main = "spec/spec_helper.rb",
7878
rubyopt = ["-rrspec/autorun"],
7979
deps = [
80-
"@bundle//:awesome_print",
81-
"@bundle//:colored2",
82-
"@bundle//:rspec",
83-
"@bundle//:rspec-its",
80+
"@bundle//:gems",
8481
],
8582
)
8683

@@ -103,6 +100,7 @@ ruby_test(
103100
main = "@bundle//:bin/rspec",
104101
deps = [
105102
"@bundle//:awesome_print",
103+
"@bundle//:bin",
106104
"@bundle//:colored2",
107105
"@bundle//:rspec",
108106
"@bundle//:rspec-its",
@@ -138,7 +136,6 @@ ruby_binary(
138136
srcs = [
139137
".relaxed-rubocop-2.4.yml",
140138
".rubocop.yml",
141-
"@bundle//:bin/rubocop",
142139
],
143140
args = [
144141
"-c",
@@ -150,6 +147,6 @@ ruby_binary(
150147
deps = [
151148
":lib",
152149
"//lib:foo",
153-
"@bundle//:rubocop",
150+
"@bundle//:bin",
154151
],
155152
)

examples/simple_script/WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ load("@bazelruby_ruby_rules//ruby:defs.bzl", "ruby_bundle")
2121

2222
ruby_bundle(
2323
name = "bundle",
24+
bundler_version = "2.1.2",
2425
excludes = {
2526
"mini_portile": ["test/**/*"],
2627
},
2728
gemfile = "//:Gemfile",
2829
gemfile_lock = "//:Gemfile.lock",
29-
version = "2.0.2",
3030
)

ruby/private/binary_wrapper.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def main(args)
9898
loadpaths = create_loadpath_entries(custom_loadpaths, runfiles)
9999
loadpaths += get_repository_imports(runfiles)
100100
loadpaths += ENV['RUBYLIB'].split(':') if ENV.key?('RUBYLIB')
101-
ENV['RUBYLIB'] = loadpaths.join(':')
101+
ENV['RUBYLIB'] = loadpaths.sort.uniq.join(':')
102102

103103
runfiles_envkey, runfiles_envvalue = runfiles_envvar(runfiles)
104104
ENV[runfiles_envkey] = runfiles_envvalue if runfiles_envkey

ruby/private/bundle/bundle.bzl

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ load("//ruby/private/tools:deprecations.bzl", "deprecated_attribute")
1313

1414
# Runs bundler with arbitrary arguments
1515
# eg: run_bundler(runtime_ctx, [ "lock", " --gemfile", "Gemfile.rails5" ])
16-
def run_bundler(runtime_ctx, bundler_arguments):
17-
#print("BUNDLE RUN", bundler_arguments)
18-
16+
def run_bundler(runtime_ctx, bundler_arguments, previous_result):
1917
# Now we are running bundle install
18+
bundler_command = bundler_arguments[0]
19+
bundler_args = []
20+
21+
# add --verbose to all commands except install
22+
if bundler_command != "install":
23+
bundler_args += ["--verbose"]
24+
25+
bundler_args += bundler_arguments[1:]
26+
2027
args = [
2128
runtime_ctx.interpreter, # ruby
22-
"--enable=gems", # bundler must run with rubygems enabled
23-
"-I",
24-
".",
2529
"-I", # Used to tell Ruby where to load the library scripts
2630
BUNDLE_PATH, # Add vendor/bundle to the list of resolvers
2731
BUNDLE_BINARY, # our binary
28-
] + bundler_arguments
32+
] + [bundler_command] + bundler_args
2933

3034
# print("Bundler Command:\n\n", args)
3135

@@ -41,7 +45,7 @@ def run_bundler(runtime_ctx, bundler_arguments):
4145
# $ bundle config --local | --global config-option config-value
4246
#
4347
# @config_category can be either 'local' or 'global'
44-
def set_bundler_config(runtime_ctx, config_category = "local"):
48+
def set_bundler_config(runtime_ctx, previous_result, config_category = "local"):
4549
# Bundler is deprecating various flags in favor of the configuration.
4650
# HOWEVER — for reasons I can't explain, Bazel runs "bundle install" *prior*
4751
# to setting these flags. So the flags are then useless until we can force the
@@ -52,16 +56,20 @@ def set_bundler_config(runtime_ctx, config_category = "local"):
5256
bundler_config = {
5357
"deployment": "true",
5458
"standalone": "true",
59+
"force": "false",
60+
"redownload": "false",
5561
"frozen": "true",
56-
"without": "development,test",
5762
"path": BUNDLE_PATH,
5863
"jobs": "20",
64+
"shebang": runtime_ctx.interpreter,
5965
}
6066

61-
for option, value in bundler_config.items():
62-
args = ["config", "--%s" % (config_category), option, value]
67+
last_result = previous_result
6368

64-
result = run_bundler(runtime_ctx, args)
69+
for option, value in bundler_config.items():
70+
args = ["config", "set", "--%s" % (config_category), option, value]
71+
result = run_bundler(runtime_ctx, args, last_result)
72+
last_result = result
6573
if result.return_code:
6674
message = "Failed to set bundle config {} to {}: {}".format(
6775
option,
@@ -70,8 +78,7 @@ def set_bundler_config(runtime_ctx, config_category = "local"):
7078
)
7179
fail(message)
7280

73-
# The new way to generate binstubs is via the binstubs command, not config option.
74-
return run_bundler(runtime_ctx, ["binstubs", "--path", BUNDLE_BIN_PATH])
81+
return last_result
7582

7683
# This function is called "pure_ruby" because it downloads and unpacks the gem
7784
# file into a given folder, which for gems without C-extensions is the same
@@ -96,6 +103,8 @@ def install_pure_ruby_gem(runtime_ctx, gem_name, gem_version, folder):
96103
result.stderr,
97104
)
98105
fail(message)
106+
else:
107+
return result
99108

100109
def install_bundler(runtime_ctx, bundler_version):
101110
return install_pure_ruby_gem(
@@ -105,21 +114,26 @@ def install_bundler(runtime_ctx, bundler_version):
105114
"bundler",
106115
)
107116

108-
def bundle_install(runtime_ctx):
117+
def bundle_install(runtime_ctx, previous_result):
109118
result = run_bundler(
110119
runtime_ctx,
111120
[
112-
"install", # bundle install
113-
"--standalone", # Makes a bundle that can work without depending on Rubygems or Bundler at runtime.
114-
"--binstubs={}".format(BUNDLE_BIN_PATH), # Creates a directory and place any executables from the gem there.
115-
"--path={}".format(BUNDLE_PATH), # The location to install the specified gems to.
121+
"install",
122+
"--binstubs={}".format(BUNDLE_BIN_PATH),
123+
"--path={}".format(BUNDLE_PATH),
124+
"--deployment",
125+
"--standalone",
126+
"--frozen",
116127
],
128+
previous_result,
117129
)
118130

119131
if result.return_code:
120132
fail("bundle install failed: %s%s" % (result.stdout, result.stderr))
133+
else:
134+
return result
121135

122-
def generate_bundle_build_file(runtime_ctx):
136+
def generate_bundle_build_file(runtime_ctx, previous_result):
123137
# Create the BUILD file to expose the gems to the WORKSPACE
124138
# USAGE: ./create_bundle_build_file.rb BUILD.bazel Gemfile.lock repo-name [excludes-json] workspace-name
125139
args = [
@@ -160,19 +174,16 @@ def _ruby_bundle_impl(ctx):
160174
)
161175

162176
# 1. Install the right version of the Bundler Gem
163-
install_bundler(runtime_ctx, bundler_version)
164-
165-
# Create label for the Bundler executable
166-
bundler = Label("//:" + BUNDLE_BINARY)
177+
result = install_bundler(runtime_ctx, bundler_version)
167178

168179
# 2. Set Bundler config in the .bundle/config file
169-
set_bundler_config(runtime_ctx)
180+
result = set_bundler_config(runtime_ctx, result)
170181

171182
# 3. Run bundle install
172-
bundle_install(runtime_ctx)
183+
result = bundle_install(runtime_ctx, result)
173184

174185
# 4. Generate the BUILD file for the bundle
175-
generate_bundle_build_file(runtime_ctx)
186+
generate_bundle_build_file(runtime_ctx, result)
176187

177188
ruby_bundle = repository_rule(
178189
implementation = _ruby_bundle_impl,

0 commit comments

Comments
 (0)