Skip to content

Commit e556a08

Browse files
authored
Merge pull request #60 from kigster/kig/download-gem-refactor
Adds ruby_rspec and refactors the ruby_bundle rule, plus some more
2 parents e275d94 + a596328 commit e556a08

29 files changed

+1059
-374
lines changed

.circleci/config.yml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,9 @@ jobs:
6161
/usr/bin/env bash bin/setup
6262
6363
- run:
64-
name: "Show Bazel Info"
64+
name: "Bazel Build & Test Example"
6565
command: |
66-
/usr/bin/env bash bin/test-suite bazel-info
67-
68-
- run:
69-
name: "Bazel Simple Script Example Build"
70-
command: |
71-
cd examples/simple_script && bazel ${BAZEL_OPTS} build ${BAZEL_BUILD_OPTS} -- //...
72-
73-
- run:
74-
name: "Bazel Simple Script Example Test"
75-
command: |
76-
cd examples/simple_script && bazel ${BAZEL_OPTS} test ${BAZEL_BUILD_OPTS} ${BAZEL_TEST_OPTS} -- //...
77-
78-
- run:
79-
name: "Bazel Simple Script Example Rubocop Check"
80-
command: |
81-
cd examples/simple_script && bazel ${BAZEL_OPTS} run ${BAZEL_BUILD_OPTS} -- :rubocop
66+
/usr/bin/env bash bin/test-suite simple-script
8267
8368
buildifier:
8469
<<: *bazel_defaults

.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: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<!-- TOC depthFrom:1 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->
22

3-
* [Status:](#status)
43
* [Usage](#usage)
4+
* [WORKSPACE File](#workspace-file)
5+
* [BUILD.bazel files](#buildbazel-files)
56
* [Rules](#rules)
67
* [ruby_library](#ruby_library)
78
* [ruby_binary](#ruby_binary)
89
* [ruby_test](#ruby_test)
9-
* [bundle_install](#bundle_install)
10+
* [ruby_bundle](#ruby_bundle)
1011
* [What's coming next](#whats-coming-next)
1112
* [Contributing](#contributing)
1213
* [Setup](#setup)
@@ -35,14 +36,14 @@
3536

3637
Ruby rules for [Bazel](https://bazel.build).
3738

38-
## Status:
39+
** Current Status:** *Work in progress.*
3940

40-
**Work in progress.**
41-
42-
We have a guide on [Building your first Ruby Project](https://github.com/bazelruby/rules_ruby/wiki/Build-your-ruby-project) on the Wiki. We encourage you to check it out.
41+
Note: we have a short guide on [Building your first Ruby Project](https://github.com/bazelruby/rules_ruby/wiki/Build-your-ruby-project) on the Wiki. We encourage you to check it out.
4342

4443
## Usage
4544

45+
### `WORKSPACE` File
46+
4647
Add `ruby_rules_dependencies` and `ruby_register_toolchains` into your `WORKSPACE` file.
4748

4849
```python
@@ -65,6 +66,33 @@ ruby_rules_dependencies()
6566
ruby_register_toolchains()
6667
```
6768

69+
Next, add any external Gem dependencies you may have via `ruby_bundle` command.
70+
The name of the bundle becomes a reference to this particular Gemfile.lock.
71+
72+
Install external gems that can be later referenced as `@<bundle-name>//:<gem-name>`,
73+
and the executables from each gem can be accessed as `@<bundle-name//:bin/<gem-binary-name>`
74+
for instance, `@bundle//:bin/rubocop`.
75+
76+
You can install more than one bundle per WORKSPACE, but that's not recommended.
77+
78+
```python
79+
ruby_bundle(
80+
name = "bundle",
81+
gemfile = ":Gemfile",
82+
gemfile_lock = ":Gemfile.lock",
83+
bundler_version = "2.1.2",
84+
)
85+
86+
ruby_bundle(
87+
name = "bundle_app_shopping",
88+
gemfile = "//apps/shopping:Gemfile",
89+
gemfile_lock = "//apps/shopping:Gemfile.lock",
90+
bundler_version = "2.1.2",
91+
)
92+
```
93+
94+
### `BUILD.bazel` files
95+
6896
Add `ruby_library`, `ruby_binary` or `ruby_test` into your `BUILD.bazel` files.
6997

7098
```python
@@ -73,12 +101,18 @@ load(
73101
"ruby_binary",
74102
"ruby_library",
75103
"ruby_test",
104+
"ruby_rspec",
76105
)
77106

78107
ruby_library(
79108
name = "foo",
80-
srcs = ["lib/foo.rb"],
109+
srcs = glob(["lib/**/*.rb"]),
81110
includes = ["lib"],
111+
deps = [
112+
"@bundle//:activesupport",
113+
"@bundle//:awesome_print",
114+
"@bundle//:rubocop",
115+
]
82116
)
83117

84118
ruby_binary(
@@ -88,10 +122,18 @@ ruby_binary(
88122
)
89123

90124
ruby_test(
91-
name = "foo_test",
125+
name = "foo-test",
92126
srcs = ["test/foo_test.rb"],
93127
deps = [":foo"],
94128
)
129+
130+
ruby_rspec(
131+
name = "foo-spec",
132+
specs = glob(["spec/**/*.rb"]),
133+
rspec_args = { "--format": "progress" },
134+
deps = [":foo"]
135+
}
136+
95137
```
96138

97139
## Rules
@@ -338,7 +380,7 @@ ruby_test(name, deps, srcs, data, main, compatible_with, deprecation, distribs,
338380
</tbody>
339381
</table>
340382

341-
### `bundle_install`
383+
### `ruby_bundle`
342384

343385
Installs gems with Bundler, and make them available as a `ruby_library`.
344386

@@ -361,9 +403,9 @@ ruby_rules_dependencies()
361403

362404
ruby_register_toolchains()
363405

364-
load("@bazelruby_ruby_rules//ruby:defs.bzl", "bundle_install")
406+
load("@bazelruby_ruby_rules//ruby:defs.bzl", "ruby_bundle")
365407

366-
bundle_install(
408+
ruby_bundle(
367409
name = "gems",
368410
gemfile = "//:Gemfile",
369411
gemfile_lock = "//:Gemfile.lock",
@@ -376,12 +418,24 @@ Example: `lib/BUILD.bazel`:
376418
ruby_library(
377419
name = "foo",
378420
srcs = ["foo.rb"],
379-
deps = ["@gems//:libs"],
421+
deps = ["@gems//:all"],
422+
)
423+
```
424+
425+
Or declare many gems in your `Gemfile`, and only use some of them in each ruby library:
426+
427+
```python
428+
ruby_binary(
429+
name = "rubocop",
430+
srcs = [":foo", ".rubocop.yml"],
431+
args = ["-P", "-D", "-c" ".rubocop.yml"],
432+
main = "@gems//:bin/rubocop",
433+
deps = ["@gems//:rubocop"],
380434
)
381435
```
382436

383437
<pre>
384-
bundle_install(name, gemfile, gemfile_lock)
438+
ruby_bundle(name, gemfile, gemfile_lock, bundler_version = "2.1.2")
385439
</pre>
386440
<table class="table table-condensed table-bordered table-params">
387441
<colgroup>
@@ -418,6 +472,14 @@ bundle_install(name, gemfile, gemfile_lock)
418472
<p>NOTE: This rule never updates the <code>Gemfile.lock</code>. It is your responsibility to generate/update <code>Gemfile.lock</code></p>
419473
</td>
420474
</tr>
475+
<tr>
476+
<td><code>bundler_version</code></td>
477+
<td>
478+
<code>String, optional</code>
479+
<p>The Version of Bundler to use. Defaults to 2.1.2.</p>
480+
<p>NOTE: This rule never updates the <code>Gemfile.lock</code>. It is your responsibility to generate/update <code>Gemfile.lock</code></p>
481+
</td>
482+
</tr>
421483
</tbody>
422484
</table>
423485

WORKSPACE

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

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

55
ruby_rules_dependencies()
66

7-
ruby_register_toolchains()
8-
97
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
108

119
bazel_skylib_workspace()
@@ -14,6 +12,10 @@ load("@bazel_skylib//lib:versions.bzl", "versions")
1412

1513
versions.check("1.2.1")
1614

15+
load("@//ruby:deps.bzl", "ruby_register_toolchains")
16+
17+
ruby_register_toolchains()
18+
1719
local_repository(
1820
name = "bazelruby_ruby_rules_ruby_tests_testdata_another_workspace",
1921
path = "ruby/tests/testdata/another_workspace",
@@ -95,10 +97,10 @@ load("@bazelruby_ruby_rules//ruby:defs.bzl", "bundle_install")
9597

9698
bundle_install(
9799
name = "bundle",
100+
bundler_version = "2.1.2",
98101
excludes = {
99102
"mini_portile": ["test/**/*"],
100103
},
101104
gemfile = "//:Gemfile",
102105
gemfile_lock = "//:Gemfile.lock",
103-
version = "2.0.2",
104106
)

bin/test-suite

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ test::simple-script() {
148148
__test::exec simple-script "
149149
cd examples/simple_script
150150
${Bazel__BuildSteps}
151-
bazel ${BAZEL_OPTS} run ${BAZEL_BUILD_OPTS} -- :rubocop ;
151+
echo run :bin; bazel ${BAZEL_OPTS} run ${BAZEL_BUILD_OPTS} :bin
152+
echo run :rubocop; bazel ${BAZEL_OPTS} run ${BAZEL_BUILD_OPTS} :rubocop
153+
152154
"
153155
}
154156

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +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-
- bazel-*/**/*
8-
- external/**/*
10+
- "external*/**/*"
11+
- "bazel-*/**/*"
12+
- "**/examples/**/*"
13+
- "**/BUILD"
14+
- "**/*.bazel"
15+
- "**/*.bzl"
16+
- "**/rubocop"
917
- "**/vendor/bundle/**/*"
18+
Include:
19+
- '**/*.rb'
20+
- '**/*.gemfile'
21+
- '**/*.gemspec'
22+
- '**/*.rake'
23+
- '**/*.ru'
24+
- '**/Gemfile'
25+
- '**/Rakefile'
1026

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

34+
# In Bazel we want to use __FILE__ because __dir__points to the actual sources
35+
Style/ExpandPathArguments:
36+
Enabled: false

examples/simple_script/BUILD

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)