Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 1 | # Copyright 2015 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 15 | JAVA_DOCS = "-link https://docs.oracle.com/javase/11/docs/api/" |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 16 | |
| 17 | def dump(obj): |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 18 | print(dir(obj)) |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 19 | for attr in dir(obj): |
| 20 | print("obj.%s = %r" % (attr, getattr(obj, attr))) |
| 21 | |
| 22 | def _impl(ctx): |
| 23 | dir = ctx.label.name |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 24 | outjar = ctx.outputs.jar |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 25 | |
| 26 | classpath = "" |
| 27 | for dep in ctx.files.deps: |
| 28 | classpath += ":" + dep.path |
| 29 | |
| 30 | src_list = "" |
| 31 | for src in ctx.files.srcs: |
| 32 | if src.path.endswith(".jar"): |
| 33 | src_list += " " + src.path |
| 34 | |
| 35 | group_list = "" |
| 36 | for group in ctx.attr.groups: |
| 37 | packages = "" |
| 38 | for p in ctx.attr.groups[group]: |
| 39 | packages += ":" + p |
| 40 | group_list += " -group \"%s\" %s" % (group, packages.replace(":", "", 1)) |
| 41 | |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 42 | java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo] |
| 43 | jar_exe_path = "%s/bin/jar" % java_runtime.java_home |
| 44 | javadoc_exe_path = "%s/bin/javadoc" % java_runtime.java_home |
| 45 | |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 46 | cmd = [ |
| 47 | "mkdir src; cd src", |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 48 | "for s in %s; do ../%s xf ../$s; done" % (src_list, jar_exe_path), |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 49 | "rm -f META-INF/MANIFEST.MF", |
| 50 | "cd ..", |
| 51 | "cp -r docs/src/main/javadoc/* .", |
| 52 | "ls -lR doc-files overview.html", |
| 53 | ] |
| 54 | |
| 55 | if ctx.attr.internal: |
| 56 | cmd += ["find src -type f | egrep -v 'src/(OSGI|WEB)-INF' >> FILES"] |
| 57 | else: |
| 58 | cmd += ["find src -type f | egrep -v 'src/(OSGI|WEB)-INF' | egrep -v '/(impl|internal)/' >> FILES"] |
| 59 | |
| 60 | cmd += [ |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 61 | "%s -encoding UTF-8 -overview overview.html -doctitle '%s' -windowtitle '%s' %s -d apidocs -classpath %s -sourcepath src %s @FILES" % |
| 62 | (javadoc_exe_path, ctx.attr.title, ctx.attr.title, group_list, classpath.replace(":", "", 1), JAVA_DOCS), |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 63 | "cp -r doc-files apidocs/doc-files", |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 64 | "%s cf %s apidocs" % (jar_exe_path, outjar.path), |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 65 | ] |
| 66 | |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 67 | ctx.actions.run_shell( |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 68 | inputs = ctx.files.srcs + ctx.files.deps, |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 69 | outputs = [outjar], |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 70 | progress_message = "Generating javadocs jar for %s" % ctx.attr.name, |
| 71 | command = ";\n".join(cmd), |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 72 | tools = java_runtime.files, |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 73 | ) |
| 74 | |
| 75 | project_javadoc = rule( |
| 76 | attrs = { |
| 77 | "title": attr.string(), |
| 78 | "overview": attr.string(default = "src/main/javadoc/overview.html"), |
| 79 | "groups": attr.string_list_dict(), |
| 80 | "deps": attr.label_list(allow_files = True), |
| 81 | "srcs": attr.label_list(allow_files = True), |
| 82 | "internal": attr.bool(default = False), |
Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 83 | "_jdk": attr.label( |
| 84 | default = Label("@bazel_tools//tools/jdk:current_java_runtime"), |
| 85 | providers = [java_common.JavaRuntimeInfo], |
| 86 | ), |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 87 | }, |
| 88 | implementation = _impl, |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 89 | outputs = {"jar": "%{name}.jar"}, |
Thomas Vachuska | c3226e9 | 2018-07-25 12:00:57 -0700 | [diff] [blame] | 90 | ) |