Skip to content

Commit 1929556

Browse files
authored
Custom phases expose custom providers (#946)
* rules_scala custom phases should expose providers- failing test * rules_scala custom phases should expose providers- passing test while doing it I found and fixed a bug in phases adjustments of first/last (they were duplicated) Additionally this mandates bazel 2.0.0 as they have some kind of bug with providers before that (don't have an issue to ref but couldn't get the build to pass with lower bazel version) * update readme to note when we stopped fully supporting 1.1.0 * remove unneeded 1.2.1 shas * drop 1.1.0 travis builds * remove 1.1.0 from bazel wrapper * break after adding positional phase * rule_providers to external_providers
1 parent d681a95 commit 1929556

File tree

11 files changed

+86
-34
lines changed

11 files changed

+86
-34
lines changed

.travis.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,12 @@ jobs:
4848
<<: *linux
4949
env: TEST_SCRIPT=test_lint
5050
# Test
51-
- <<: *linux
52-
env: TEST_SCRIPT=test_rules_scala BAZEL_VERSION=1.1.0
5351
- <<: *linux
5452
env: TEST_SCRIPT=test_rules_scala BAZEL_VERSION=2.0.0
55-
- <<: *linux
56-
env: TEST_SCRIPT=test_reproducibility BAZEL_VERSION=1.1.0
5753
- <<: *linux
5854
env: TEST_SCRIPT=test_reproducibility BAZEL_VERSION=2.0.0
59-
- <<: *osx
60-
env: TEST_SCRIPT=test_rules_scala BAZEL_VERSION=1.1.0
6155
- <<: *osx
6256
env: TEST_SCRIPT=test_rules_scala BAZEL_VERSION=2.0.0
63-
- <<: *osx
64-
env: TEST_SCRIPT=test_reproducibility BAZEL_VERSION=1.1.0
6557
- <<: *osx
6658
env: TEST_SCRIPT=test_reproducibility BAZEL_VERSION=2.0.0
6759

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ for an example workspace using another scala version.
112112
| bazel | rules_scala gitsha |
113113
|--------|--------------------|
114114
| 2.0.0 | HEAD |
115-
| 1.1.0 | HEAD |
115+
| 1.1.0 | d681a952da74fc61a49fc3167b03548f42fc5dde |
116116
| 0.28.1 | bd0c388125e12f4f173648fc4474f73160a5c628 |
117117
| 0.23.x | ca655e5a330cbf1d66ce1d9baa63522752ec6011 |
118118
| 0.22.x | f3113fb6e9e35cb8f441d2305542026d98afc0a2 |

scala/private/phases/api.bzl

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ def _adjust_phases(phases, adjustments):
2222
# phase_name: the name of the new phase, also used to access phase information
2323
# phase_function: the function of the new phase
2424
for (relation, peer_name, phase_name, phase_function) in adjustments:
25-
for idx, (needle, _) in enumerate(phases):
26-
if relation in ["^", "first"]:
27-
phases.insert(0, (phase_name, phase_function))
28-
elif relation in ["$", "last"]:
29-
phases.append((phase_name, phase_function))
30-
elif needle == peer_name:
31-
if relation in ["-", "before"]:
32-
phases.insert(idx, (phase_name, phase_function))
33-
elif relation in ["+", "after"]:
34-
phases.insert(idx + 1, (phase_name, phase_function))
35-
elif relation in ["=", "replace"]:
36-
phases[idx] = (phase_name, phase_function)
25+
if relation in ["^", "first"]:
26+
phases.insert(0, (phase_name, phase_function))
27+
elif relation in ["$", "last"]:
28+
phases.append((phase_name, phase_function))
29+
else:
30+
for idx, (needle, _) in enumerate(phases):
31+
if needle == peer_name:
32+
if relation in ["-", "before"]:
33+
phases.insert(idx, (phase_name, phase_function))
34+
break
35+
elif relation in ["+", "after"]:
36+
phases.insert(idx + 1, (phase_name, phase_function))
37+
break
38+
elif relation in ["=", "replace"]:
39+
phases[idx] = (phase_name, phase_function)
40+
break
3741
return phases
3842

3943
# Execute phases
@@ -59,18 +63,21 @@ def run_phases(ctx, builtin_customizable_phases, fixed_phase):
5963
# A placeholder for data shared with later phases
6064
global_provider = {}
6165
current_provider = struct(**global_provider)
66+
acculmulated_external_providers = []
6267
for (name, function) in adjusted_phases + [fixed_phase]:
6368
# Run a phase
6469
new_provider = function(ctx, current_provider)
6570

6671
# If a phase returns data, append it to global_provider
6772
# for later phases to access
6873
if new_provider != None:
74+
if (hasattr(new_provider, "external_providers")):
75+
acculmulated_external_providers.extend(new_provider.external_providers)
6976
global_provider[name] = new_provider
7077
current_provider = struct(**global_provider)
7178

7279
# The final return of rules implementation
73-
return current_provider
80+
return acculmulated_external_providers + current_provider.final
7481

7582
# A method to pass in phase provider
7683
def extras_phases(extras):

scala/private/rules/scala_binary.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _scala_binary_impl(ctx):
4545
],
4646
# fixed phase
4747
("final", phase_binary_final),
48-
).final
48+
)
4949

5050
_scala_binary_attrs = {
5151
"main_class": attr.string(mandatory = True),

scala/private/rules/scala_junit_test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _scala_junit_test_impl(ctx):
5050
],
5151
# fixed phase
5252
("final", phase_binary_final),
53-
).final
53+
)
5454

5555
_scala_junit_test_attrs = {
5656
"prefixes": attr.string_list(default = []),

scala/private/rules/scala_library.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _scala_library_impl(ctx):
6969
],
7070
# fixed phase
7171
("final", phase_library_final),
72-
).final
72+
)
7373

7474
_scala_library_attrs = {}
7575

@@ -146,7 +146,7 @@ def _scala_library_for_plugin_bootstrapping_impl(ctx):
146146
],
147147
# fixed phase
148148
("final", phase_library_final),
149-
).final
149+
)
150150

151151
# the scala compiler plugin used for dependency analysis is compiled using `scala_library`.
152152
# in order to avoid cyclic dependencies `scala_library_for_plugin_bootstrapping` was created for this purpose,
@@ -202,7 +202,7 @@ def _scala_macro_library_impl(ctx):
202202
],
203203
# fixed phase
204204
("final", phase_library_final),
205-
).final
205+
)
206206

207207
_scala_macro_library_attrs = {
208208
"main_class": attr.string(),

scala/private/rules/scala_repl.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _scala_repl_impl(ctx):
4646
],
4747
# fixed phase
4848
("final", phase_binary_final),
49-
).final
49+
)
5050

5151
_scala_repl_attrs = {
5252
"jvm_flags": attr.string_list(),

scala/private/rules/scala_test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _scala_test_impl(ctx):
4747
],
4848
# fixed phase
4949
("final", phase_scalatest_final),
50-
).final
50+
)
5151

5252
_scala_test_attrs = {
5353
"main_class": attr.string(

test/phase/providers/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load(":phase_providers_expose.bzl", "phase_expose_provider_singleton", "rule_that_needs_custom_provider", "scala_library_that_exposes_custom_provider")
2+
3+
scala_library_that_exposes_custom_provider(
4+
name = "scala_library_that_exposes_custom_provider",
5+
)
6+
7+
rule_that_needs_custom_provider(
8+
name = "rule_that_needs_custom_provider",
9+
dep = ":scala_library_that_exposes_custom_provider",
10+
)
11+
12+
phase_expose_provider_singleton(
13+
name = "phase_expose_provider_singleton_target",
14+
visibility = ["//visibility:public"],
15+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
load("@io_bazel_rules_scala//scala:advanced_usage/providers.bzl", "ScalaRulePhase")
2+
load("@io_bazel_rules_scala//scala:advanced_usage/scala.bzl", "make_scala_library")
3+
4+
ext_phase_expose_provider = {
5+
"phase_providers": [
6+
"//test/phase/providers:phase_expose_provider_singleton_target",
7+
],
8+
}
9+
10+
scala_library_that_exposes_custom_provider = make_scala_library(ext_phase_expose_provider)
11+
12+
_some_position = "last" #last position is just because a location is mandatory, not important
13+
14+
def _phase_expose_provider_singleton_implementation(ctx):
15+
return [
16+
ScalaRulePhase(
17+
custom_phases = [
18+
(_some_position, "", "phase_expose_provider", _phase_expose_provider),
19+
],
20+
),
21+
]
22+
23+
phase_expose_provider_singleton = rule(
24+
implementation = _phase_expose_provider_singleton_implementation,
25+
)
26+
27+
CustomProviderExposedByPhase = provider()
28+
29+
def _phase_expose_provider(ctx, p):
30+
return struct(
31+
external_providers = [CustomProviderExposedByPhase()],
32+
)
33+
34+
def _rule_that_needs_custom_provider_impl(ctx):
35+
return []
36+
37+
rule_that_needs_custom_provider = rule(
38+
implementation = _rule_that_needs_custom_provider_impl,
39+
attrs = {
40+
"dep": attr.label(providers = [CustomProviderExposedByPhase]),
41+
},
42+
)

0 commit comments

Comments
 (0)