Skip to content

Commit a596328

Browse files
author
Konstantin Gredeskoul
committed
Refactor download_gem.rb and build file generator
1 parent 3242b0a commit a596328

File tree

13 files changed

+351
-174
lines changed

13 files changed

+351
-174
lines changed

.rubocop.yml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
inherit_from: .relaxed-rubocop-2.4.yml
22

33
AllCops:
4-
TargetRubyVersion: 2.6.5
4+
TargetRubyVersion: 2.6
5+
UseCache: true
6+
DefaultFormatter: progress
7+
DisplayStyleGuide: true
8+
DisplayCopNames: true
59
Exclude:
6-
- "**/rubocop"
7-
- "examples/**/*"
10+
- "external*/**/*"
811
- "bazel-*/**/*"
9-
- "external/**/*"
12+
- "**/examples/**/*"
13+
- "**/BUILD"
14+
- "**/*.bazel"
15+
- "**/*.bzl"
16+
- "**/rubocop"
1017
- "**/vendor/bundle/**/*"
18+
Include:
19+
- '**/*.rb'
20+
- '**/*.gemfile'
21+
- '**/*.gemspec'
22+
- '**/*.rake'
23+
- '**/*.ru'
24+
- '**/Gemfile'
25+
- '**/Rakefile'
1126

27+
Layout/HashAlignment:
28+
Enabled: true
29+
EnforcedColonStyle: table
30+
31+
Style/Dir:
32+
Enabled: false
1233

34+
Layout/MultilineMethodCallIndentation:
35+
Enabled: true
36+
EnforcedStyle: indented_relative_to_receiver

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ ruby_binary(
122122
)
123123

124124
ruby_test(
125-
name = "foo_test",
125+
name = "foo-test",
126126
srcs = ["test/foo_test.rb"],
127127
deps = [":foo"],
128128
)
129129

130130
ruby_rspec(
131-
name = "foo_spec",
132-
specs = glob(["spec/**/*.rb]),
131+
name = "foo-spec",
132+
specs = glob(["spec/**/*.rb"]),
133133
rspec_args = { "--format": "progress" },
134134
deps = [":foo"]
135135
}

WORKSPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspace(name = "bazelruby_ruby_rules")
22

3-
load("@//ruby/private:dependencies.bzl", "ruby_rules_dependencies")
3+
load("@//ruby:deps.bzl", "ruby_rules_dependencies")
44

55
ruby_rules_dependencies()
66

@@ -97,10 +97,10 @@ load("@bazelruby_ruby_rules//ruby:defs.bzl", "bundle_install")
9797

9898
bundle_install(
9999
name = "bundle",
100+
bundler_version = "2.1.2",
100101
excludes = {
101102
"mini_portile": ["test/**/*"],
102103
},
103104
gemfile = "//:Gemfile",
104105
gemfile_lock = "//:Gemfile.lock",
105-
version = "2.0.2",
106106
)

examples/simple_script/BUILD.bazel

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ ruby_binary(
4747
],
4848
)
4949

50+
ruby_binary(
51+
name = "bin-all",
52+
srcs = ["script.rb"],
53+
main = "script.rb",
54+
deps = [
55+
":lib",
56+
"//lib:foo",
57+
"@bundle//:gems",
58+
],
59+
)
60+
5061
# This is an example of the RSpec definition that uses autorun
5162
# and points to spec_helper as the main spec file. It specifies
5263
# which specs to run using the args.
@@ -87,9 +98,8 @@ ruby_test(
8798
args = [
8899
"--format documentation",
89100
"--force-color",
90-
] + glob([
91-
"spec/**/*.rb",
92-
]),
101+
"spec",
102+
],
93103
main = "@bundle//:bin/rspec",
94104
deps = [
95105
"@bundle//:awesome_print",
@@ -110,6 +120,10 @@ ruby_rspec(
110120
":sources",
111121
":spec_sources",
112122
],
123+
rspec_args = {
124+
# NOTE: the output is only visible with --test_output=streamed flag
125+
"--format": "progress", # this is how we can override rspec output format
126+
},
113127
specs = glob([
114128
"spec/**/*.rb",
115129
]),

examples/simple_script/WORKSPACE

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,18 @@ local_repository(
88
)
99

1010
load(
11-
"@bazelruby_ruby_rules//ruby/private:dependencies.bzl",
11+
"@bazelruby_ruby_rules//ruby:deps.bzl",
12+
"ruby_register_toolchains",
1213
"ruby_rules_dependencies",
1314
)
1415

1516
ruby_rules_dependencies()
1617

17-
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
18-
19-
bazel_skylib_workspace()
20-
21-
load(
22-
"@bazelruby_ruby_rules//ruby:deps.bzl",
23-
"ruby_register_toolchains",
24-
)
25-
2618
ruby_register_toolchains(version = "2.6.5")
2719

28-
load("@bazelruby_ruby_rules//ruby:defs.bzl", "bundle_install")
20+
load("@bazelruby_ruby_rules//ruby:defs.bzl", "ruby_bundle")
2921

30-
bundle_install(
22+
ruby_bundle(
3123
name = "bundle",
3224
excludes = {
3325
"mini_portile": ["test/**/*"],

examples/simple_script/spec/spec_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
require 'awesome_print'
1919
require 'colored2'
2020

21-
# frozen_string_literal: true
2221
RSpec.configure do |config|
2322
config.expect_with :rspec do |expectations|
2423
expectations.include_chain_clauses_in_custom_matcher_descriptions = true

ruby/deps.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Repository rules
22
load(
33
"@bazelruby_ruby_rules//ruby/private:dependencies.bzl",
4-
_load_ruby_rules_dependencies = "ruby_rules_dependencies",
4+
_ruby_rules_dependencies = "ruby_rules_dependencies",
55
)
66
load(
77
"@bazelruby_ruby_rules//ruby/private:sdk.bzl",
88
_register_toolchains = "ruby_register_toolchains",
99
)
1010

11-
load_ruby_rules_dependencies = _load_ruby_rules_dependencies
11+
ruby_rules_dependencies = _ruby_rules_dependencies
1212

1313
ruby_register_toolchains = _register_toolchains

ruby/private/bundle/bundle.bzl

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
load(
22
"//ruby/private:constants.bzl",
33
"BUNDLE_ATTRS",
4+
"BUNDLE_BINARY",
5+
"BUNDLE_BIN_PATH",
6+
"BUNDLE_PATH",
47
"RULES_RUBY_WORKSPACE_NAME",
8+
"SCRIPT_BUILD_FILE_GENERATOR",
9+
"SCRIPT_INSTALL_GEM",
510
)
611
load("//ruby/private:providers.bzl", "RubyRuntimeContext")
712
load("//ruby/private/tools:deprecations.bzl", "deprecated_attribute")
813

9-
BUNDLE_BIN_PATH = "bin"
10-
BUNDLE_PATH = "lib"
11-
BUNDLE_BINARY = "bundler/exe/bundler"
12-
SCRIPT_INSTALL_GEM = "install_bundler.rb"
13-
SCRIPT_BUILD_FILE_GENERATOR = "create_bundle_build_file.rb"
14-
1514
# Runs bundler with arbitrary arguments
16-
#
1715
# eg: run_bundler(runtime_ctx, [ "lock", " --gemfile", "Gemfile.rails5" ])
18-
#
1916
def run_bundler(runtime_ctx, bundler_arguments):
17+
#print("BUNDLE RUN", bundler_arguments)
18+
2019
# Now we are running bundle install
2120
args = [
2221
runtime_ctx.interpreter, # ruby
@@ -39,7 +38,7 @@ def run_bundler(runtime_ctx, bundler_arguments):
3938
#
4039
# Sets local bundler config values by calling
4140
#
42-
# $ bundler config --local | --global config-option config-value
41+
# $ bundle config --local | --global config-option config-value
4342
#
4443
# @config_category can be either 'local' or 'global'
4544
def set_bundler_config(runtime_ctx, config_category = "local"):
@@ -51,16 +50,15 @@ def set_bundler_config(runtime_ctx, config_category = "local"):
5150
#
5251
# Set local configuration options for bundler
5352
bundler_config = {
54-
"binstubs": BUNDLE_BIN_PATH,
55-
"deployment": "'true'",
56-
"standalone": "'true'",
57-
"frozen": "'true'",
53+
"deployment": "true",
54+
"standalone": "true",
55+
"frozen": "true",
5856
"without": "development,test",
5957
"path": BUNDLE_PATH,
6058
"jobs": "20",
6159
}
6260

63-
for option, value in [(option, value) for option, value in bundler_config.items()]:
61+
for option, value in bundler_config.items():
6462
args = ["config", "--%s" % (config_category), option, value]
6563

6664
result = run_bundler(runtime_ctx, args)
@@ -72,6 +70,13 @@ def set_bundler_config(runtime_ctx, config_category = "local"):
7270
)
7371
fail(message)
7472

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])
75+
76+
# This function is called "pure_ruby" because it downloads and unpacks the gem
77+
# file into a given folder, which for gems without C-extensions is the same
78+
# as install. To support gems that have C-extensions, the Ruby file install_gem.rb
79+
# will need to be modified to use Gem::Installer.at(path).install(gem) API.
7580
def install_pure_ruby_gem(runtime_ctx, gem_name, gem_version, folder):
7681
# USAGE: ./install_bundler.rb gem-name gem-version destination-folder
7782
args = [
@@ -104,7 +109,7 @@ def bundle_install(runtime_ctx):
104109
result = run_bundler(
105110
runtime_ctx,
106111
[
107-
"install", # > bundle install
112+
"install", # bundle install
108113
"--standalone", # Makes a bundle that can work without depending on Rubygems or Bundler at runtime.
109114
"--binstubs={}".format(BUNDLE_BIN_PATH), # Creates a directory and place any executables from the gem there.
110115
"--path={}".format(BUNDLE_PATH), # The location to install the specified gems to.
@@ -120,7 +125,7 @@ def generate_bundle_build_file(runtime_ctx):
120125
args = [
121126
runtime_ctx.interpreter, # ruby interpreter
122127
"--enable=gems", # prevent the addition of gem installation directories to the default load path
123-
"-I", # -I lib (adds this folder to $LOAD_PATH where ruby searchesf for things)
128+
"-I", # -I lib (adds this folder to $LOAD_PATH where ruby searches for things)
124129
"bundler/lib",
125130
SCRIPT_BUILD_FILE_GENERATOR, # The template used to created bundle file
126131
"BUILD.bazel", # Bazel build file (can be empty)
@@ -134,14 +139,18 @@ def generate_bundle_build_file(runtime_ctx):
134139
if result.return_code:
135140
fail("build file generation failed: %s%s" % (result.stdout, result.stderr))
136141

137-
def bundle_install_impl(ctx):
142+
def _ruby_bundle_impl(ctx):
138143
ctx.symlink(ctx.attr.gemfile, "Gemfile")
139144
ctx.symlink(ctx.attr.gemfile_lock, "Gemfile.lock")
140145
ctx.symlink(ctx.attr._create_bundle_build_file, SCRIPT_BUILD_FILE_GENERATOR)
141146
ctx.symlink(ctx.attr._install_bundler, SCRIPT_INSTALL_GEM)
142147

143148
# version is too generic for this operation
144149
deprecated_attribute(ctx, "version", "bundler_version")
150+
if ctx.attr.bundler_version:
151+
bundler_version = ctx.attr.bundler_version
152+
else:
153+
bundler_version = ctx.attr.version
145154

146155
# Setup this provider that we pass around between functions for convenience
147156
runtime_ctx = RubyRuntimeContext(
@@ -151,7 +160,7 @@ def bundle_install_impl(ctx):
151160
)
152161

153162
# 1. Install the right version of the Bundler Gem
154-
install_bundler(runtime_ctx, ctx.attr.bundler_version)
163+
install_bundler(runtime_ctx, bundler_version)
155164

156165
# Create label for the Bundler executable
157166
bundler = Label("//:" + BUNDLE_BINARY)
@@ -166,6 +175,6 @@ def bundle_install_impl(ctx):
166175
generate_bundle_build_file(runtime_ctx)
167176

168177
ruby_bundle = repository_rule(
169-
implementation = bundle_install_impl,
178+
implementation = _ruby_bundle_impl,
170179
attrs = BUNDLE_ATTRS,
171180
)

0 commit comments

Comments
 (0)