Skip to content

Commit 74ccba4

Browse files
committed
Toolchainize //scala/scalafmt:scalafmt_toolchain
This is a pretty straightforward and easy update on top of the previous `setup_toolchains` and `scala_toolchains_repo` changes. Part of bazel-contrib#1482.
1 parent 3d2c94d commit 74ccba4

File tree

7 files changed

+45
-26
lines changed

7 files changed

+45
-26
lines changed

WORKSPACE

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ load("//scala:toolchains.bzl", "scala_toolchains")
4343

4444
scala_toolchains(
4545
fetch_sources = True,
46+
scalafmt = True,
4647
testing = True,
4748
)
4849

@@ -81,12 +82,6 @@ load("//scala_proto:scala_proto.bzl", "scala_proto_repositories")
8182

8283
scala_proto_repositories()
8384

84-
load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_default_config", "scalafmt_repositories")
85-
86-
scalafmt_default_config()
87-
88-
scalafmt_repositories()
89-
9085
MAVEN_SERVER_URLS = default_maven_server_urls()
9186

9287
# needed for the cross repo proto test

scala/private/macros/toolchains_repo.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def _scala_toolchains_repo_impl(repository_ctx):
5050
format_args.update(testing_build_args)
5151
toolchains["testing"] = _TESTING_TOOLCHAIN_BUILD
5252

53+
if repo_attr.scalafmt:
54+
toolchains["scalafmt"] = _SCALAFMT_TOOLCHAIN_BUILD
55+
5356
if len(toolchains) == 0:
5457
fail("no toolchains specified")
5558

@@ -69,6 +72,7 @@ _scala_toolchains_repo = repository_rule(
6972
"junit": attr.bool(),
7073
"specs2": attr.bool(),
7174
"testing": attr.bool(),
75+
"scalafmt": attr.bool(),
7276
},
7377
)
7478

@@ -143,3 +147,12 @@ load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
143147
for scala_version in SCALA_VERSIONS
144148
]
145149
"""
150+
151+
_SCALAFMT_TOOLCHAIN_BUILD = """
152+
load(
153+
"@@{rules_scala_repo}//scala/scalafmt/toolchain:setup_scalafmt_toolchain.bzl",
154+
"setup_scalafmt_toolchains",
155+
)
156+
157+
setup_scalafmt_toolchains()
158+
"""

scala/scalafmt/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
load("//scala/scalafmt/toolchain:setup_scalafmt_toolchain.bzl", "setup_scalafmt_toolchains")
21
load("//scala/scalafmt/toolchain:toolchain.bzl", "export_scalafmt_deps")
32
load("//scala/scalafmt:phase_scalafmt_ext.bzl", "scalafmt_singleton")
43
load("//scala:scala.bzl", "scala_binary")
@@ -37,12 +36,13 @@ scalafmt_singleton(
3736
visibility = ["//visibility:public"],
3837
)
3938

40-
setup_scalafmt_toolchains()
41-
4239
# Alias for backward compatibility:
4340
alias(
4441
name = "scalafmt_toolchain",
45-
actual = "scalafmt_toolchain" + version_suffix(SCALA_VERSION),
42+
actual = (
43+
"@io_bazel_rules_scala_toolchains//scalafmt:scalafmt_toolchain" +
44+
version_suffix(SCALA_VERSION)
45+
),
4646
)
4747

4848
export_scalafmt_deps(

scala/scalafmt/phase_scalafmt_ext.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@ ext_scalafmt = {
2020
),
2121
"_fmt": attr.label(
2222
cfg = "exec",
23-
default = "//scala/scalafmt",
23+
default = Label("//scala/scalafmt"),
2424
executable = True,
2525
),
2626
"_java_host_runtime": attr.label(
2727
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
2828
),
2929
"_runner": attr.label(
3030
allow_single_file = True,
31-
default = "//scala/scalafmt:runner",
31+
default = Label("//scala/scalafmt:runner"),
3232
),
3333
"_testrunner": attr.label(
3434
allow_single_file = True,
35-
default = "//scala/scalafmt:testrunner",
35+
default = Label("//scala/scalafmt:testrunner"),
3636
),
3737
},
3838
"outputs": {
3939
"scalafmt_runner": "%{name}.format",
4040
"scalafmt_testrunner": "%{name}.format-test",
4141
},
4242
"phase_providers": [
43-
"//scala/scalafmt:phase_scalafmt",
43+
Label("//scala/scalafmt:phase_scalafmt"),
4444
],
4545
}
4646

scala/scalafmt/scalafmt_repositories.bzl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def scalafmt_artifact_ids(scala_version):
7979

8080
def scalafmt_repositories(
8181
maven_servers = _default_maven_server_urls(),
82-
overriden_artifacts = {},
83-
bzlmod_enabled = False):
82+
overriden_artifacts = {}):
8483
for scala_version in SCALA_VERSIONS:
8584
repositories(
8685
scala_version = scala_version,
@@ -89,11 +88,6 @@ def scalafmt_repositories(
8988
overriden_artifacts = overriden_artifacts,
9089
)
9190

92-
if not bzlmod_enabled:
93-
_register_scalafmt_toolchains()
94-
95-
def _register_scalafmt_toolchains():
96-
for scala_version in SCALA_VERSIONS:
9791
native.register_toolchains(str(Label(
9892
"//scala/scalafmt:scalafmt_toolchain" +
9993
version_suffix(scala_version),

scala/toolchains.bzl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
load("//junit:junit.bzl", "junit_artifact_ids")
44
load("//scala/private:macros/scala_repositories.bzl", "scala_repositories")
55
load("//scala/private:macros/toolchains_repo.bzl", "scala_toolchains_repo")
6+
load(
7+
"//scala/scalafmt:scalafmt_repositories.bzl",
8+
"scalafmt_artifact_ids",
9+
"scalafmt_default_config",
10+
)
611
load("//scala:scala_cross_version.bzl", "default_maven_server_urls")
712
load("//scalatest:scalatest.bzl", "scalatest_artifact_ids")
813
load("//specs2:specs2.bzl", "specs2_artifact_ids")
@@ -21,7 +26,9 @@ def scala_toolchains(
2126
scalatest = False,
2227
junit = False,
2328
specs2 = False,
24-
testing = False):
29+
testing = False,
30+
scalafmt = False,
31+
scalafmt_default_config_path = ".scalafmt.conf"):
2532
"""Instantiates @io_bazel_rules_scala_toolchains and all its dependencies.
2633
2734
Provides a unified interface to configuring rules_scala both directly in a
@@ -66,6 +73,9 @@ def scala_toolchains(
6673
specs2: whether to instantiate the Specs2 JUnit toolchain
6774
testing: whether to instantiate the Scalatest, JUnit, and Specs2 JUnit
6875
toolchains combined
76+
scalafmt: whether to instantiate the Scalafmt toolchain
77+
scalafmt_default_config_path: the relative path to the default Scalafmt
78+
config file within the repository
6979
"""
7080
scala_repositories(
7181
maven_servers = maven_servers,
@@ -78,6 +88,9 @@ def scala_toolchains(
7888
scala_compiler_srcjars = scala_compiler_srcjars,
7989
)
8090

91+
if scalafmt:
92+
scalafmt_default_config(scalafmt_default_config_path)
93+
8194
if testing:
8295
scalatest = True
8396
junit = True
@@ -106,6 +119,12 @@ def scala_toolchains(
106119
for scala_version in SCALA_VERSIONS:
107120
version_specific_artifact_ids = {}
108121

122+
if scalafmt:
123+
version_specific_artifact_ids.update({
124+
id: fetch_sources
125+
for id in scalafmt_artifact_ids(scala_version)
126+
})
127+
109128
all_artifacts = (
110129
artifact_ids_to_fetch_sources | version_specific_artifact_ids
111130
)
@@ -125,6 +144,7 @@ def scala_toolchains(
125144
junit = junit,
126145
specs2 = specs2,
127146
testing = testing,
147+
scalafmt = scalafmt,
128148
)
129149

130150
def scala_register_toolchains():

test_cross_build/WORKSPACE

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@ scala_config(
7575
load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains")
7676

7777
scala_toolchains(
78+
scalafmt = True,
7879
scalatest = True,
7980
)
8081

8182
register_toolchains("@io_bazel_rules_scala_toolchains//...:all")
82-
83-
load("@io_bazel_rules_scala//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_repositories")
84-
85-
scalafmt_repositories()

0 commit comments

Comments
 (0)