Skip to content

Commit 9d0d4f9

Browse files
authored
scala_doc: Add support for building docs without transitive dependencies (#1331)
* scala_doc: Add support for building docs without transitive dependencies * Apply lint fixes * scala-doc: Use aspect instead of a flag to build docs without transitive dependencies * Address nits * scala_doc: Add comment to explain why we are exporting make_scala_doc_rule * scala_doc: Lint fixes * scala_doc: Use exec instead of host
1 parent 1f2c78b commit 9d0d4f9

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

scala/private/rules/scala_doc.bzl

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,26 @@ _ScaladocAspectInfo = provider(fields = [
88
"plugins",
99
])
1010

11-
def _scaladoc_aspect_impl(target, ctx):
11+
def _scaladoc_intransitive_aspect_impl(target, ctx):
12+
"""Build scaladocs only for the provided targets."""
13+
return _scaladoc_aspect_impl(target, ctx, transitive = False)
14+
15+
def _scaladoc_aspect_impl(target, ctx, transitive = True):
1216
"""Collect source files and compile_jars from JavaInfo-returning deps."""
1317

1418
# We really only care about visited targets with srcs, so only look at those.
1519
if hasattr(ctx.rule.attr, "srcs"):
1620
# Collect only Java and Scala sources enumerated in visited targets, including src_files in deps.
17-
src_files = depset(
18-
direct = [file for file in ctx.rule.files.srcs if file.extension.lower() in ["java", "scala"]],
19-
transitive = [dep[_ScaladocAspectInfo].src_files for dep in ctx.rule.attr.deps if _ScaladocAspectInfo in dep],
20-
)
21+
direct_deps = [file for file in ctx.rule.files.srcs if file.extension.lower() in ["java", "scala"]]
22+
23+
# Sometimes we only want to generate scaladocs for a single target and not all of its
24+
# dependencies
25+
if transitive:
26+
transitive_deps = [dep[_ScaladocAspectInfo].src_files for dep in ctx.rule.attr.deps if _ScaladocAspectInfo in dep]
27+
else:
28+
transitive_deps = []
29+
30+
src_files = depset(direct = direct_deps, transitive = transitive_deps)
2131

2232
# Collect compile_jars from visited targets' deps.
2333
compile_jars = depset(
@@ -40,14 +50,22 @@ def _scaladoc_aspect_impl(target, ctx):
4050
else:
4151
return []
4252

43-
_scaladoc_aspect = aspect(
53+
_scaladoc_transitive_aspect = aspect(
4454
implementation = _scaladoc_aspect_impl,
4555
attr_aspects = ["deps"],
4656
required_aspect_providers = [
4757
[JavaInfo],
4858
],
4959
)
5060

61+
scaladoc_intransitive_aspect = aspect(
62+
implementation = _scaladoc_intransitive_aspect_impl,
63+
attr_aspects = ["deps"],
64+
required_aspect_providers = [
65+
[JavaInfo],
66+
],
67+
)
68+
5169
def _scala_doc_impl(ctx):
5270
# scaladoc warns if you don't have the output directory already created, which is annoying.
5371
output_path = ctx.actions.declare_directory("{}.html".format(ctx.attr.name))
@@ -85,19 +103,20 @@ def _scala_doc_impl(ctx):
85103

86104
return [DefaultInfo(files = depset(direct = [output_path]))]
87105

88-
scala_doc = rule(
89-
attrs = {
90-
"deps": attr.label_list(
91-
aspects = [_scaladoc_aspect],
92-
providers = [JavaInfo],
93-
),
94-
"scalacopts": attr.string_list(),
95-
"_scaladoc": attr.label(
96-
cfg = "host",
97-
executable = True,
98-
default = Label("//src/scala/io/bazel/rules_scala/scaladoc_support:scaladoc_generator"),
99-
),
100-
},
101-
doc = "Generate Scaladoc HTML documentation for source files in from the given dependencies.",
102-
implementation = _scala_doc_impl,
103-
)
106+
def make_scala_doc_rule(aspect = _scaladoc_transitive_aspect):
107+
return rule(
108+
attrs = {
109+
"deps": attr.label_list(
110+
aspects = [aspect],
111+
providers = [JavaInfo],
112+
),
113+
"scalacopts": attr.string_list(),
114+
"_scaladoc": attr.label(
115+
cfg = "exec",
116+
executable = True,
117+
default = Label("//src/scala/io/bazel/rules_scala/scaladoc_support:scaladoc_generator"),
118+
),
119+
},
120+
doc = "Generate Scaladoc HTML documentation for source files in from the given dependencies.",
121+
implementation = _scala_doc_impl,
122+
)

scala/scala.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ load(
1212
)
1313
load(
1414
"@io_bazel_rules_scala//scala/private:rules/scala_doc.bzl",
15-
_scala_doc = "scala_doc",
15+
_make_scala_doc_rule = "make_scala_doc_rule",
16+
_scaladoc_intransitive_aspect = "scaladoc_intransitive_aspect",
1617
)
1718
load(
1819
"@io_bazel_rules_scala//scala/private:rules/scala_junit_test.bzl",
@@ -50,7 +51,11 @@ def scala_specs2_junit_test(name, **kwargs):
5051

5152
# Re-export private rules for public consumption
5253
scala_binary = _scala_binary
53-
scala_doc = _scala_doc
54+
55+
# These are exported for enabling users to build scaladocs without transitive dependencies.
56+
make_scala_doc_rule = _make_scala_doc_rule
57+
scaladoc_intransitive_aspect = _scaladoc_intransitive_aspect
58+
scala_doc = _make_scala_doc_rule()
5459
scala_junit_test = _scala_junit_test
5560
scala_library = _scala_library
5661
scala_library_for_plugin_bootstrapping = _scala_library_for_plugin_bootstrapping

0 commit comments

Comments
 (0)