blob: d9296494698bc974bfab32d7d04fe8f7dcf318eb [file] [log] [blame]
Lorenz Brundcfc6782021-11-30 05:27:48 +01001load("@rules_proto//proto:defs.bzl", "ProtoInfo")
2
3def _proto_docs(ctx):
4 protos = [proto[ProtoInfo] for proto in ctx.attr.protos]
5 transitive_sources = depset(transitive = [proto.transitive_sources for proto in protos])
6 transitive_proto_path = depset(transitive = [proto.transitive_proto_path for proto in protos])
7
8 out = ctx.actions.declare_file(ctx.label.name + ".html")
9
10 args = []
11 args.append("--plugin")
12 args.append(ctx.executable._protoc_gen_doc.path)
13 args.append("--doc_out")
14 args.append(out.dirname)
15 args.append("--doc_opt=html," + out.basename)
16
17 for include_path in transitive_proto_path.to_list():
18 args.append("-I")
19 args.append(include_path)
20
21 for src in transitive_sources.to_list():
22 # Due to the built-in import path for well-known types (see AddDefaultProtoPaths
23 # in @com_google_protobuf//src/google/protobuf/compiler:command_line_interface.cc)
24 # in protoc the Bazel-generated well-known protos are considered to contain
25 # "duplicate" types.
26 # Since generating documentation for well-known types is not that useful just
27 # skip them.
Leopoldbc93c2b2023-01-14 13:12:23 +010028 if src.path.find("/bin/external/com_github_protocolbuffers_protobuf/_virtual_imports/") != -1:
Lorenz Brundcfc6782021-11-30 05:27:48 +010029 continue
30 args.append(src.path)
31
32 ctx.actions.run(
33 tools = [ctx.executable._protoc_gen_doc],
34 inputs = transitive_sources,
35 outputs = [out],
36 executable = ctx.executable._protoc,
37 arguments = args,
38 )
39 return [DefaultInfo(files = depset([out]))]
40
41proto_docs = rule(
42 implementation = _proto_docs,
43 doc = """
44 Generate a single HTML documentation file documenting all types and services from the transitive set of
45 Protobuf files referenced by all proto_libraries passed into `protos`.
46 """,
47 attrs = {
48 "protos": attr.label_list(
49 doc = "A list of protobuf libraries for which (and their dependencies) documentation should be generated for",
50 providers = [ProtoInfo],
51 default = [],
52 ),
53 "_protoc": attr.label(
54 default = Label("@com_google_protobuf//:protoc"),
55 cfg = "exec",
56 executable = True,
57 allow_files = True,
58 ),
59 "_protoc_gen_doc": attr.label(
60 default = Label("@com_github_pseudomuto_protoc_gen_doc//cmd/protoc-gen-doc"),
61 cfg = "exec",
62 executable = True,
63 allow_files = True,
64 ),
65 },
66)