Skip to content

Commit

Permalink
Add rule javadoc
Browse files Browse the repository at this point in the history
Rule javadoc generates the Javadoc for a set of libraries.
  • Loading branch information
joca-bt committed Aug 30, 2023
1 parent 2576f81 commit c84fcdc
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.0.0
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Additional Java rules for Bazel

Rules:

- [javadoc](#javadoc)

## Usage

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

http_archive(
name = "extra_rules_java",
sha256 = <sha>,
strip_prefix = "extra_rules_java-{}".format(<tag>),
url = "https://github.com/joca-bt/extra_rules_java/archive/{}.tar.gz".format(<tag>),
)
```

## Rules

### javadoc

```bazel
javadoc(name, exclude, javacopts, libs, links, packages, title)
```

Generates the Javadoc for a set of [libraries](https://bazel.build/reference/be/java#java_library).

**Arguments**

| Name | Type | Mandatory | Default |
| --- | --- | --- | --- |
| name | Name | Yes | |
| exclude | List of strings | No | [] |
| javacopts | List of strings | No | [] |
| libs | List of labels | Yes | |
| links | List of strings | No | ["https://docs.oracle.com/en/java/javase/17/docs/api/"] |
| packages | List of strings | Yes | |
| title | String | No | "" |
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "extra_rules_java")
Empty file added java/BUILD
Empty file.
3 changes: 3 additions & 0 deletions java/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("//java/private:javadoc.bzl", _javadoc = "javadoc")

javadoc = _javadoc
Empty file added java/private/BUILD
Empty file.
4 changes: 4 additions & 0 deletions java/private/depsets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
visibility("private")

def to_paths(depset):
return [element.path for element in depset.to_list()]
4 changes: 4 additions & 0 deletions java/private/jars.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
visibility("private")

def extract_jars(jars, dir):
return " && ".join(["unzip -q -o {} -d {}".format(jar, dir) for jar in jars])
81 changes: 81 additions & 0 deletions java/private/javadoc.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
load(":depsets.bzl", "to_paths")
load(":jars.bzl", "extract_jars")

def _javadoc(ctx, deps, src_dir, javadoc_dir, java_home):
command = [
"{}/bin/javadoc".format(java_home),
"-quiet",
"-Xdoclint:-missing",
"--class-path {}".format(":".join(deps)),
"--source-path {}".format(src_dir),
"-subpackages {}".format(":".join(ctx.attr.packages)),
"-d {}".format(javadoc_dir),
"-docencoding UTF-8",
"-notimestamp",
]

if ctx.attr.exclude:
command.append("-exclude {}".format(":".join(ctx.attr.exclude)))

command.extend(ctx.attr.javacopts)

for link in ctx.attr.links:
command.append("-link {}".format(link))

if ctx.attr.title:
command.append("-doctitle '{}'".format(ctx.attr.title))
command.append("-windowtitle '{}'".format(ctx.attr.title))

return " ".join(command)

def _javadoc_impl(ctx):
srcs = depset(transitive = [depset(lib[JavaInfo].source_jars) for lib in ctx.attr.libs])
deps = depset(transitive = [lib[JavaInfo].transitive_compile_time_jars for lib in ctx.attr.libs])
output = ctx.actions.declare_file("{}.jar".format(ctx.label.name))

java_home = ctx.attr._jdk[java_common.JavaRuntimeInfo].java_home
tmp_dir = "{}/_{}/".format(output.dirname, output.basename)
src_dir = "{}/src/".format(tmp_dir)
javadoc_dir = "{}/javadoc/".format(tmp_dir)

commands = [
"rm -rf {}".format(tmp_dir),
"mkdir {}".format(tmp_dir),
extract_jars(to_paths(srcs), src_dir),
_javadoc(ctx, to_paths(deps), src_dir, javadoc_dir, java_home),
"{}/bin/jar -c -f {} -C {} .".format(java_home, output.path, javadoc_dir),
]

ctx.actions.run_shell(
command = " && ".join(commands),
inputs = ctx.files._jdk + srcs.to_list() + deps.to_list(),
outputs = [output],
)

return [DefaultInfo(files = depset([output]))]

javadoc = rule(
doc = "Generates the Javadoc for a set of libraries.",
implementation = _javadoc_impl,
attrs = {
"exclude": attr.string_list(),
"javacopts": attr.string_list(),
"libs": attr.label_list(
allow_empty = False,
mandatory = True,
providers = [JavaInfo],
),
"links": attr.string_list(
default = ["https://docs.oracle.com/en/java/javase/17/docs/api/"],
),
"packages": attr.string_list(
allow_empty = False,
mandatory = True,
),
"title": attr.string(),
"_jdk": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
providers = [java_common.JavaRuntimeInfo],
),
},
)

0 comments on commit c84fcdc

Please sign in to comment.