Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom phases expose custom providers #946

Merged
merged 8 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ for an example workspace using another scala version.
| bazel | rules_scala gitsha |
|--------|--------------------|
| 2.0.0 | HEAD |
| 1.1.0 | HEAD |
| 1.1.0 | d681a952da74fc61a49fc3167b03548f42fc5dde |
| 0.28.1 | bd0c388125e12f4f173648fc4474f73160a5c628 |
| 0.23.x | ca655e5a330cbf1d66ce1d9baa63522752ec6011 |
| 0.22.x | f3113fb6e9e35cb8f441d2305542026d98afc0a2 |
Expand Down
30 changes: 17 additions & 13 deletions scala/private/phases/api.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ def _adjust_phases(phases, adjustments):
# phase_name: the name of the new phase, also used to access phase information
# phase_function: the function of the new phase
for (relation, peer_name, phase_name, phase_function) in adjustments:
for idx, (needle, _) in enumerate(phases):
if relation in ["^", "first"]:
phases.insert(0, (phase_name, phase_function))
elif relation in ["$", "last"]:
phases.append((phase_name, phase_function))
elif needle == peer_name:
if relation in ["-", "before"]:
phases.insert(idx, (phase_name, phase_function))
elif relation in ["+", "after"]:
phases.insert(idx + 1, (phase_name, phase_function))
elif relation in ["=", "replace"]:
phases[idx] = (phase_name, phase_function)
if relation in ["^", "first"]:
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
phases.insert(0, (phase_name, phase_function))
elif relation in ["$", "last"]:
phases.append((phase_name, phase_function))
else:
for idx, (needle, _) in enumerate(phases):
if needle == peer_name:
if relation in ["-", "before"]:
phases.insert(idx, (phase_name, phase_function))
elif relation in ["+", "after"]:
phases.insert(idx + 1, (phase_name, phase_function))
elif relation in ["=", "replace"]:
phases[idx] = (phase_name, phase_function)
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
return phases

# Execute phases
Expand All @@ -59,18 +60,21 @@ def run_phases(ctx, builtin_customizable_phases, fixed_phase):
# A placeholder for data shared with later phases
global_provider = {}
current_provider = struct(**global_provider)
rule_providers = []
for (name, function) in adjusted_phases + [fixed_phase]:
# Run a phase
new_provider = function(ctx, current_provider)

# If a phase returns data, append it to global_provider
# for later phases to access
if new_provider != None:
if (hasattr(new_provider, "rule_providers")):
rule_providers.extend(new_provider.rule_providers)
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
global_provider[name] = new_provider
current_provider = struct(**global_provider)

# The final return of rules implementation
return current_provider
return rule_providers + current_provider.final

# A method to pass in phase provider
def extras_phases(extras):
Expand Down
2 changes: 1 addition & 1 deletion scala/private/rules/scala_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _scala_binary_impl(ctx):
],
# fixed phase
("final", phase_binary_final),
).final
)

_scala_binary_attrs = {
"main_class": attr.string(mandatory = True),
Expand Down
2 changes: 1 addition & 1 deletion scala/private/rules/scala_junit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _scala_junit_test_impl(ctx):
],
# fixed phase
("final", phase_binary_final),
).final
)

_scala_junit_test_attrs = {
"prefixes": attr.string_list(default = []),
Expand Down
6 changes: 3 additions & 3 deletions scala/private/rules/scala_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _scala_library_impl(ctx):
],
# fixed phase
("final", phase_library_final),
).final
)

_scala_library_attrs = {}

Expand Down Expand Up @@ -146,7 +146,7 @@ def _scala_library_for_plugin_bootstrapping_impl(ctx):
],
# fixed phase
("final", phase_library_final),
).final
)

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

_scala_macro_library_attrs = {
"main_class": attr.string(),
Expand Down
2 changes: 1 addition & 1 deletion scala/private/rules/scala_repl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _scala_repl_impl(ctx):
],
# fixed phase
("final", phase_binary_final),
).final
)

_scala_repl_attrs = {
"jvm_flags": attr.string_list(),
Expand Down
2 changes: 1 addition & 1 deletion scala/private/rules/scala_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _scala_test_impl(ctx):
],
# fixed phase
("final", phase_scalatest_final),
).final
)

_scala_test_attrs = {
"main_class": attr.string(
Expand Down
15 changes: 15 additions & 0 deletions test/phase/providers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load(":phase_providers_expose.bzl", "phase_expose_provider_singleton", "rule_that_needs_custom_provider", "scala_library_that_exposes_custom_provider")

scala_library_that_exposes_custom_provider(
name = "scala_library_that_exposes_custom_provider",
)

rule_that_needs_custom_provider(
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
name = "rule_that_needs_custom_provider",
dep = ":scala_library_that_exposes_custom_provider",
)

phase_expose_provider_singleton(
name = "phase_expose_provider_singleton_target",
visibility = ["//visibility:public"],
)
42 changes: 42 additions & 0 deletions test/phase/providers/phase_providers_expose.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@io_bazel_rules_scala//scala:advanced_usage/providers.bzl", "ScalaRulePhase")
load("@io_bazel_rules_scala//scala:advanced_usage/scala.bzl", "make_scala_library")

ext_phase_expose_provider = {
"phase_providers": [
"//test/phase/providers:phase_expose_provider_singleton_target",
],
}

scala_library_that_exposes_custom_provider = make_scala_library(ext_phase_expose_provider)

_some_position = "last" #last position is just because a location is mandatory, not important

def _phase_expose_provider_singleton_implementation(ctx):
return [
ScalaRulePhase(
custom_phases = [
(_some_position, "", "phase_expose_provider", _phase_expose_provider),
],
),
]

phase_expose_provider_singleton = rule(
implementation = _phase_expose_provider_singleton_implementation,
)

CustomProviderExposedByPhase = provider()

def _phase_expose_provider(ctx, p):
return struct(
rule_providers = [CustomProviderExposedByPhase()],
)

def _rule_that_needs_custom_provider_impl(ctx):
return []

rule_that_needs_custom_provider = rule(
implementation = _rule_that_needs_custom_provider_impl,
attrs = {
"dep": attr.label(providers = [CustomProviderExposedByPhase]),
},
)
2 changes: 1 addition & 1 deletion tools/bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -e

default_bazel_version='1.1.0'
default_bazel_version='2.0.0'

if [ "$BUILDKITE" = true ]; then
bazel_version='host'
Expand Down