blob: b65f11b77176bc3c0e75f5a3d36b600a7a15f4f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.docgen;
import com.google.devtools.build.docgen.builtin.BuiltinProtos.Builtins;
import com.google.devtools.build.docgen.builtin.BuiltinProtos.Callable;
import com.google.devtools.build.docgen.builtin.BuiltinProtos.Param;
import com.google.devtools.build.docgen.builtin.BuiltinProtos.Type;
import com.google.devtools.build.docgen.builtin.BuiltinProtos.Value;
import com.google.devtools.build.docgen.skylark.SkylarkBuiltinMethodDoc;
import com.google.devtools.build.docgen.skylark.SkylarkMethodDoc;
import com.google.devtools.build.docgen.skylark.SkylarkModuleDoc;
import com.google.devtools.build.docgen.skylark.SkylarkParamDoc;
import com.google.devtools.build.lib.util.Classpath.ClassPathException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
/** The main class for the Skylark documentation generator. */
public class ApiExporter {
private static void appendBuiltins(String builtinsFile) {
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(builtinsFile))) {
Builtins.Builder builtins = Builtins.newBuilder();
Map<String, SkylarkModuleDoc> allTypes = SkylarkDocumentationCollector.collectModules();
for (Map.Entry<String, SkylarkModuleDoc> modEntry : allTypes.entrySet()) {
SkylarkModuleDoc mod = modEntry.getValue();
// Include SkylarkModuleDoc in Builtins as a Type.
Type.Builder type = Type.newBuilder();
type.setName(mod.getName());
type.setDoc(mod.getDocumentation());
for (SkylarkMethodDoc meth : mod.getJavaMethods()) {
type.addField(collectFieldInfo(meth));
}
builtins.addType(type);
// Include SkylarkModuleDoc in Builtins as a Value.
Value.Builder value = Value.newBuilder();
value.setName(mod.getName());
value.setType(mod.getName());
value.setDoc(mod.getDocumentation());
builtins.addGlobal(value);
// Add all global variables and functions in Builtins as Values.
for (Map.Entry<String, SkylarkBuiltinMethodDoc> methEntry :
mod.getBuiltinMethods().entrySet()) {
SkylarkBuiltinMethodDoc meth = methEntry.getValue();
builtins.addGlobal(collectFieldInfo(meth));
}
}
Builtins build = builtins.build();
build.writeTo(out);
} catch (IOException | ClassPathException e) {
System.err.println(e);
}
}
private static Value.Builder collectFieldInfo(SkylarkMethodDoc meth) {
Value.Builder field = Value.newBuilder();
field.setName(meth.getName());
field.setDoc(meth.getDocumentation());
if (meth.isCallable()) {
Callable.Builder callable = Callable.newBuilder();
for (SkylarkParamDoc par : meth.getParams()) {
Param.Builder param = Param.newBuilder();
param.setName(par.getName());
param.setType(par.getType());
param.setDoc(par.getDocumentation());
callable.addParam(param);
}
callable.setReturnType(meth.getReturnType());
field.setCallable(callable);
}
return field;
}
public static void main(String[] args) {
if (args.length != 1) {
throw new IllegalArgumentException(
"Expected one argument. Usage:\n" + "{api_exporter_bin} {builtin_output_file}");
}
String builtinsProtoFile = args[0];
appendBuiltins(builtinsProtoFile);
}
}
|