Skip to content

Commit

Permalink
Generate javadocs for uber jars
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 24, 2020
1 parent 0e15b7f commit 1b28709
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 116 deletions.
12 changes: 12 additions & 0 deletions java/client/src/org/openqa/selenium/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ java_dist_zip(
],
)

javadoc(
name = "client-javadoc",
deps = [
":client-combined",
],
transitive = True,
third_party_prefixes = [
"@maven//",
"//third_party",
],
)

java_import(
name = "manifest",
jars = [
Expand Down
110 changes: 110 additions & 0 deletions java/private/dist_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
load("//java/private:common.bzl", "MavenInfo", "has_maven_deps", "read_coordinates", "explode_coordinates")
load("//java/private:module.bzl", "JavaModuleInfo")

DistInfo = provider(
fields = {
"target": "Label that this info was derived from",
"name": "The name by which this target is known, which may be derived from maven coordinates",
"binary_jars": "The binary jars associated with this target",
"source_jars": "The source jars associated with this target",
},
)

DistZipInfo = provider(
fields = {
"dist_infos": "Depset of transitive DistInfos",
},
)

_ATTR_ASPECTS = [
"deps",
"exports",
"runtime_deps",
]

def _name(coordinates, default):
if not coordinates:
return default
exploded = explode_coordinates(coordinates)
return exploded[1] + "-" + exploded[2]

def _dist_aspect_impl(target, ctx):
deps = getattr(ctx.rule.attr, "deps", [])
exports = getattr(ctx.rule.attr, "exports", [])
rt_deps = getattr(ctx.rule.attr, "runtime_deps", [])

all_deps = deps + exports + rt_deps
transitive_infos = [d[DistZipInfo].dist_infos for d in all_deps]

name = None
binary_jars = []
source_jars = []

if MavenInfo in target and target[MavenInfo].coordinates:
name = _name(target[MavenInfo].coordinates, None)
binary_jars = target[MavenInfo].artifact_jars
source_jars = target[MavenInfo].source_jars
elif JavaModuleInfo in target and target[JavaModuleInfo].name:
coordinates = read_coordinates(ctx.rule.attr.tags)
name = _name(coordinates, target[JavaModuleInfo].name)
binary_jars = target[JavaInfo].runtime_output_jars
source_jars = target[JavaInfo].source_jars
elif JavaInfo in target:
coordinates = read_coordinates(ctx.rule.attr.tags)
if coordinates:
name = _name(coordinates, None)
binary_jars = target[JavaInfo].runtime_output_jars
source_jars = target[JavaInfo].source_jars

if len(binary_jars) > 1:
fail("Unsure how to handle expanding binary jars for " + target)
if len(source_jars) > 1:
fail("Unsure how to handle expanding source jars for " + target)

current = DistInfo(
target = str(target.label),
name = name,
binary_jars = binary_jars,
source_jars = source_jars,
)

return [
DistZipInfo(
dist_infos = depset([current], transitive = transitive_infos),
),
]

dist_aspect = aspect(
_dist_aspect_impl,
attr_aspects = _ATTR_ASPECTS,
provides = [
DistZipInfo,
],
required_aspect_providers = [
[JavaInfo],
[JavaInfo, JavaModuleInfo],
[MavenInfo],
],
)

def _is_third_party(prefixes, target):
for prefix in prefixes:
if target.startswith(prefix):
return True
return False

def separate_first_and_third_party(third_party_prefixes, dist_zip_infos):
combined = depset(transitive = [i.dist_infos for i in dist_zip_infos])

first_party = []
third_party = []

for dist_zip_info in combined.to_list():
if not dist_zip_info.name:
continue
if _is_third_party(third_party_prefixes, dist_zip_info.target):
third_party.append(dist_zip_info)
else:
first_party.append(dist_zip_info)

return (first_party, third_party)
123 changes: 15 additions & 108 deletions java/private/dist_zip.bzl
Original file line number Diff line number Diff line change
@@ -1,95 +1,7 @@
load("//java/private:common.bzl", "MavenInfo", "has_maven_deps", "read_coordinates", "explode_coordinates")
load("//java/private:module.bzl", "JavaModuleInfo")

DistZipInfo = provider(
fields = {
"dist_infos": "Transitive collection of structs containing base_name, binary_jar, and source_jar",
},
)

_ATTR_ASPECTS = [
"deps",
"exports",
"runtime_deps",
]

def _name(coordinates, default):
if not coordinates:
return default
exploded = explode_coordinates(coordinates)
return exploded[1] + "-" + exploded[2]

def _dist_aspect_impl(target, ctx):
deps = getattr(ctx.rule.attr, "deps", [])
exports = getattr(ctx.rule.attr, "exports", [])
rt_deps = getattr(ctx.rule.attr, "runtime_deps", [])

all_deps = deps + exports + rt_deps
transitive_infos = [d[DistZipInfo].dist_infos for d in all_deps]

name = None
binary_jars = []
source_jars = []

if MavenInfo in target and target[MavenInfo].coordinates:
name = _name(target[MavenInfo].coordinates, None)
binary_jars = target[MavenInfo].artifact_jars
source_jars = target[MavenInfo].source_jars
elif JavaModuleInfo in target and target[JavaModuleInfo].name:
coordinates = read_coordinates(ctx.rule.attr.tags)
name = _name(coordinates, target[JavaModuleInfo].name)
binary_jars = target[JavaInfo].runtime_output_jars
source_jars = target[JavaInfo].source_jars
elif JavaInfo in target:
coordinates = read_coordinates(ctx.rule.attr.tags)
if coordinates:
name = _name(coordinates, None)
binary_jars = target[JavaInfo].runtime_output_jars
source_jars = target[JavaInfo].source_jars

if len(binary_jars) > 1:
fail("Unsure how to handle expanding binary jars for " + target)
if len(source_jars) > 1:
fail("Unsure how to handle expanding source jars for " + target)

current = struct(
target = str(target.label),
name = name,
binary_jar = binary_jars[0] if len(binary_jars) else None,
source_jar = source_jars[0] if len(source_jars) else None,
)

return [
DistZipInfo(
dist_infos = depset([current], transitive = transitive_infos),
),
]

_dist_aspect = aspect(
_dist_aspect_impl,
attr_aspects = _ATTR_ASPECTS,
provides = [
DistZipInfo,
],
required_aspect_providers = [
[JavaInfo],
[JavaInfo, JavaModuleInfo],
[MavenInfo],
],
)

def is_third_party(prefixes, target):
for prefix in prefixes:
if target.startswith(prefix):
return True
return False
load("//java/private:common.bzl", "has_maven_deps")
load("//java/private:dist_info.bzl", "DistZipInfo", "dist_aspect", "separate_first_and_third_party")

def _java_dist_zip_impl(ctx):
# out = ctx.actions.declare_file("%s-dist.zip" % ctx.attr.name)

# args = ctx.actions.args()
# args.add_all(["c", out.path])

inputs = []
files = []
for file in ctx.files.files:
Expand All @@ -98,23 +10,21 @@ def _java_dist_zip_impl(ctx):

infos = depset([d[DistZipInfo] for d in ctx.attr.deps]).to_list()

(first, third) = separate_first_and_third_party(ctx.attr.third_party_prefixes, [dep[DistZipInfo] for dep in ctx.attr.deps])

first_party = []
third_party = []

for info in infos:
for dist_info in info.dist_infos.to_list():
if not dist_info.name:
continue
for info in first:
inputs.extend(info.binary_jars)
inputs.extend(info.source_jars)
[first_party.append("%s.jar=%s" % (info.name, fp.path)) for fp in info.binary_jars]
[first_party.append("%s-sources.jar=%s" % (info.name, fp.path)) for fp in info.source_jars]

inputs.append(dist_info.binary_jar)
if is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
third_party.append("lib/%s.jar=%s" % (dist_info.name, dist_info.binary_jar.path))
else:
first_party.append("%s.jar=%s" % (dist_info.name, dist_info.binary_jar.path))

if dist_info.source_jar and not is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
inputs.append(dist_info.source_jar)
first_party.append("%s-sources.jar=%s" % (dist_info.name, dist_info.source_jar.path))
for info in third:
inputs.extend(info.binary_jars)
inputs.extend(info.source_jars)
[third_party.append("lib/%s.jar=%s" % (info.name, tp.path)) for tp in info.binary_jars]

out = ctx.actions.declare_file("%s.zip" % ctx.attr.name)
args = ctx.actions.args()
Expand Down Expand Up @@ -146,13 +56,10 @@ java_dist_zip = rule(
[DistZipInfo],
],
aspects = [
_dist_aspect, has_maven_deps,
dist_aspect, has_maven_deps,
],
),
"third_party_prefixes": attr.string_list(
default = [],
allow_empty = True,
),
"third_party_prefixes": attr.string_list(),
"_zip": attr.label(
default = "@bazel_tools//tools/zip:zipper",
executable = True,
Expand Down
18 changes: 11 additions & 7 deletions java/private/javadoc.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//java/private:dist_info.bzl", "DistZipInfo", "dist_aspect", "separate_first_and_third_party")
load("//java/private:module.bzl", "JavaModuleInfo")

def generate_javadoc(ctx, javadoc, source_jars, classpath, output):
Expand All @@ -14,12 +15,11 @@ def generate_javadoc(ctx, javadoc, source_jars, classpath, output):
)

def _javadoc_impl(ctx):
(first, ignored) = separate_first_and_third_party(
ctx.attr.third_party_prefixes, [dep[DistZipInfo] for dep in ctx.attr.deps])

sources = []
for dep in ctx.attr.deps:
if JavaModuleInfo in dep:
sources.extend(dep[JavaInfo].source_jars)
else:
sources.extend(dep[JavaInfo].source_jars)
[sources.extend(dep.source_jars) for dep in first]

jar_file = ctx.actions.declare_file("%s.jar" % ctx.attr.name)

Expand All @@ -37,10 +37,14 @@ javadoc = rule(
"deps": attr.label_list(
mandatory = True,
providers = [
[JavaInfo],
[DistZipInfo],
],
aspects = [
dist_aspect,
],
),
"hide": attr.string_list(),
"transitive": attr.bool(),
"third_party_prefixes": attr.string_list(),
"_javadoc": attr.label(
default = "//java/client/src/org/openqa/selenium/tools/javadoc",
cfg = "host",
Expand Down
14 changes: 13 additions & 1 deletion java/server/src/org/openqa/selenium/grid/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//:copy_file.bzl", "copy_file")
load("//java:defs.bzl", "java_dist_zip", "java_export")
load("//java:defs.bzl", "java_dist_zip", "java_export", "javadoc")
load("//java:version.bzl", "SE_VERSION")

java_export(
Expand Down Expand Up @@ -66,3 +66,15 @@ java_dist_zip(
":grid",
],
)

javadoc(
name = "all-javadocs",
deps = [
":grid",
],
transitive = True,
third_party_prefixes = [
"@maven//",
"//third_party",
],
)

0 comments on commit 1b28709

Please sign in to comment.