Skip to content

Commit

Permalink
Customizable phases
Browse files Browse the repository at this point in the history
  • Loading branch information
borkaehw committed Oct 24, 2019
1 parent 8b7c669 commit 1d6e868
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 65 deletions.
23 changes: 12 additions & 11 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ workspace(name = "io_bazel_rules_scala")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "cdaac537b56375f658179ee2f27813cac19542443f4722b6730d84e4125355e6",
strip_prefix = "buildtools-f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db",
url = "https://github.com/bazelbuild/buildtools/archive/f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db.zip",
)

load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")

buildifier_dependencies()

load("//scala:scala.bzl", "scala_repositories")

scala_repositories()
Expand Down Expand Up @@ -151,13 +163,6 @@ http_archive(
url = "https://github.com/bazelbuild/rules_go/releases/download/0.18.7/rules_go-0.18.7.tar.gz",
)

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "cdaac537b56375f658179ee2f27813cac19542443f4722b6730d84e4125355e6",
strip_prefix = "buildtools-f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db",
url = "https://github.com/bazelbuild/buildtools/archive/f27d1753c8b3210d9e87cdc9c45bc2739ae2c2db.zip",
)

load(
"@io_bazel_rules_go//go:deps.bzl",
"go_register_toolchains",
Expand All @@ -168,10 +173,6 @@ go_rules_dependencies()

go_register_toolchains()

load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")

buildifier_dependencies()

http_archive(
name = "bazel_toolchains",
sha256 = "5962fe677a43226c409316fcb321d668fc4b7fa97cb1f9ef45e7dc2676097b26",
Expand Down
37 changes: 37 additions & 0 deletions scala/private/phases/api.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
load(
"@io_bazel_rules_scala//scala:providers.bzl",
_ScalaRulePhase = "ScalaRulePhase",
)

def _adjust_phases(phases, adjustments):
if len(adjustments) == 0:
return phases
phases = phases[:]
for (relation, peer_name, name, function) in adjustments:
for idx, (needle, _) in enumerate(phases):
if needle == peer_name:
if relation in ["-", "before"]:
phases.insert(idx, (name, function))
elif relation in ["+", "after"]:
phases.insert(idx + 1, (name, function))
elif relation in ["=", "replace"]:
phases[idx] = (name, function)
return phases

def run_phases(ctx, phases):
phase_providers = [
p[_ScalaRulePhase]
for p in ctx.attr._phase_providers
if _ScalaRulePhase in p
]

if phase_providers != []:
phases = _adjust_phases(phases, [p for pp in phase_providers for p in pp.phases])

global_provider = {}
current_provider = struct(**global_provider)
for (name, function) in phases:
Expand All @@ -8,3 +37,11 @@ def run_phases(ctx, phases):
current_provider = struct(**global_provider)

return current_provider

def extras_phases(extras):
return {
"_phase_providers": attr.label_list(
default = [pp for extra in extras for pp in extra["phase_providers"]],
providers = [_ScalaRulePhase],
),
}
2 changes: 2 additions & 0 deletions scala/private/phases/phases.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load(
"@io_bazel_rules_scala//scala/private:phases/api.bzl",
_run_phases = "run_phases",
_extras_phases = "extras_phases",
)
load(
"@io_bazel_rules_scala//scala/private:phases/phase_write_executable.bzl",
Expand Down Expand Up @@ -64,6 +65,7 @@ load("@io_bazel_rules_scala//scala/private:phases/phase_coverage_runfiles.bzl",

# API
run_phases = _run_phases
extras_phases = _extras_phases
# init
phase_common_init = _phase_common_init
phase_library_init = _phase_library_init
Expand Down
24 changes: 16 additions & 8 deletions scala/private/rules/scala_binary.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Builds Scala binaries"""

load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts")
load(
"@io_bazel_rules_scala//scala/private:common_attributes.bzl",
"common_attrs",
Expand All @@ -10,6 +11,7 @@ load(
load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs")
load(
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
"extras_phases",
"phase_binary_coda",
"phase_binary_compile",
"phase_common_collect_jars",
Expand Down Expand Up @@ -54,11 +56,17 @@ _scala_binary_attrs.update(common_attrs)

_scala_binary_attrs.update(resolve_deps)

scala_binary = rule(
attrs = _scala_binary_attrs,
executable = True,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_binary_impl,
)
def make_scala_binary(*extras):
return rule(
attrs = _dicts.add(
_scala_binary_attrs,
extras_phases(extras),
),
executable = True,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_binary_impl,
)

scala_binary = make_scala_binary()
24 changes: 16 additions & 8 deletions scala/private/rules/scala_junit_test.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Rules for writing tests with JUnit"""

load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts")
load(
"@io_bazel_rules_scala//scala/private:common_attributes.bzl",
"common_attrs",
Expand All @@ -9,6 +10,7 @@ load(
load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs")
load(
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
"extras_phases",
"phase_binary_coda",
"phase_common_init",
"phase_common_java_wrapper",
Expand Down Expand Up @@ -106,11 +108,17 @@ _scala_junit_test_attrs.update({
"tests_from": attr.label_list(providers = [[JavaInfo]]),
})

scala_junit_test = rule(
attrs = _scala_junit_test_attrs,
fragments = ["java"],
outputs = common_outputs,
test = True,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_junit_test_impl,
)
def make_scala_junit_test(*extras):
return rule(
attrs = _dicts.add(
_scala_junit_test_attrs,
extras_phases(extras),
),
fragments = ["java"],
outputs = common_outputs,
test = True,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_junit_test_impl,
)

scala_junit_test = make_scala_junit_test()
62 changes: 41 additions & 21 deletions scala/private/rules/scala_library.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts")
load(
"@io_bazel_rules_scala//scala/private:common.bzl",
"sanitize_string_for_usage",
Expand All @@ -16,6 +17,7 @@ load(
)
load(
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
"extras_phases",
"phase_common_collect_jars",
"phase_library_coda",
"phase_library_compile",
Expand Down Expand Up @@ -70,13 +72,19 @@ _scala_library_attrs.update(_library_attrs)

_scala_library_attrs.update(resolve_deps)

scala_library = rule(
attrs = _scala_library_attrs,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_library_impl,
)
def make_scala_library(*extras):
return rule(
attrs = _dicts.add(
_scala_library_attrs,
extras_phases(extras),
),
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_library_impl,
)

scala_library = make_scala_library()

# Scala library suite generates a series of scala libraries
# then it depends on them with a meta one which exports all the sub targets
Expand Down Expand Up @@ -135,13 +143,19 @@ _scala_library_for_plugin_bootstrapping_attrs.update(
common_attrs_for_plugin_bootstrapping,
)

scala_library_for_plugin_bootstrapping = rule(
attrs = _scala_library_for_plugin_bootstrapping_attrs,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_library_for_plugin_bootstrapping_impl,
)
def make_scala_library_for_plugin_bootstrapping(*extras):
return rule(
attrs = _dicts.add(
_scala_library_for_plugin_bootstrapping_attrs,
extras_phases(extras),
),
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_library_for_plugin_bootstrapping_impl,
)

scala_library_for_plugin_bootstrapping = make_scala_library_for_plugin_bootstrapping()

##
# scala_macro_library
Expand Down Expand Up @@ -184,10 +198,16 @@ _scala_macro_library_attrs["unused_dependency_checker_mode"] = attr.string(
mandatory = False,
)

scala_macro_library = rule(
attrs = _scala_macro_library_attrs,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_macro_library_impl,
)
def make_scala_macro_library(*extras):
return rule(
attrs = _dicts.add(
_scala_macro_library_attrs,
extras_phases(extras),
),
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_macro_library_impl,
)

scala_macro_library = make_scala_macro_library()
24 changes: 16 additions & 8 deletions scala/private/rules/scala_repl.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Rule for launching a Scala REPL with dependencies"""

load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts")
load(
"@io_bazel_rules_scala//scala/private:common_attributes.bzl",
"common_attrs",
Expand All @@ -10,6 +11,7 @@ load(
load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs")
load(
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
"extras_phases",
"phase_binary_coda",
"phase_common_init",
"phase_common_runfiles",
Expand Down Expand Up @@ -53,11 +55,17 @@ _scala_repl_attrs.update(common_attrs)

_scala_repl_attrs.update(resolve_deps)

scala_repl = rule(
attrs = _scala_repl_attrs,
executable = True,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_repl_impl,
)
def make_scala_repl(*extras):
return rule(
attrs = _dicts.add(
_scala_repl_attrs,
extras_phases(extras),
),
executable = True,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_repl_impl,
)

scala_repl = make_scala_repl()
26 changes: 17 additions & 9 deletions scala/private/rules/scala_test.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Rules for writing tests with ScalaTest"""

load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts")
load(
"@io_bazel_rules_scala//scala/private:common_attributes.bzl",
"common_attrs",
Expand All @@ -10,6 +11,7 @@ load("@io_bazel_rules_scala//scala/private:common.bzl", "sanitize_string_for_usa
load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs")
load(
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
"extras_phases",
"phase_common_init",
"phase_common_java_wrapper",
"phase_common_scala_provider",
Expand Down Expand Up @@ -91,15 +93,21 @@ _scala_test_attrs.update(common_attrs)

_scala_test_attrs.update(_test_resolve_deps)

scala_test = rule(
attrs = _scala_test_attrs,
executable = True,
fragments = ["java"],
outputs = common_outputs,
test = True,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_test_impl,
)
def make_scala_test(*extras):
return rule(
attrs = _dicts.add(
_scala_test_attrs,
extras_phases(extras),
),
executable = True,
fragments = ["java"],
outputs = common_outputs,
test = True,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_test_impl,
)

scala_test = make_scala_test()

# This auto-generates a test suite based on the passed set of targets
# we will add a root test_suite with the name of the passed name
Expand Down
7 changes: 7 additions & 0 deletions scala/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ declare_scalac_provider = rule(
"default_macro_classpath": attr.label_list(allow_files = True),
},
)

ScalaRulePhase = provider(
doc = "A Scala compiler plugin",
fields = {
"phases": "the phases to add",
},
)
Loading

0 comments on commit 1d6e868

Please sign in to comment.