forked from Univa/rumcake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_api_docs.js
More file actions
212 lines (185 loc) · 5.66 KB
/
gen_api_docs.js
File metadata and controls
212 lines (185 loc) · 5.66 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// API doc generation is inspired by they way embassy-rs generates their docs: https://github.com/embassy-rs/docserver
import process from "child_process";
import path from "path";
import fs from "fs";
import url from "url";
import handlebars from "handlebars";
import toml from "toml";
function main() {
const manifest = toml.parse(
fs
.readFileSync(
path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
path.join("..", "..", "rumcake", "Cargo.toml").toString(),
),
)
.toString(),
);
const redirect_template = handlebars.compile(
fs
.readFileSync(
path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
"redirect.html.hbs",
),
)
.toString(),
);
const nav_template = handlebars.compile(
fs
.readFileSync(
path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
"nav.html.hbs",
),
)
.toString(),
);
const head = handlebars.compile(
fs
.readFileSync(
path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
"head.html.hbs",
),
)
.toString(),
)({});
const end_of_head = /<\/head>/;
const start_of_body = /<body class="([^\"]*)">/;
const end_of_body = /<\/body>/;
const update_html_in_folder = (folder, navbar) => {
for (const file of fs
.readdirSync(folder, { withFileTypes: true })
.filter(
(file) =>
file.isDirectory() || (file.isFile() && file.name.endsWith(".html")),
)) {
// Update html in the folder
if (file.isDirectory()) {
update_html_in_folder(path.join(folder, file.name), navbar);
continue;
}
// Otherwise, we have an HTML file, update the contents
let data = fs.readFileSync(path.join(folder, file.name), "utf8");
data = data.replace(end_of_head, `${head}</head>`);
data = data.replace(start_of_body, `<body>${navbar}<div class="$1">`);
data = data.replace(end_of_body, "</div></body>");
fs.writeFileSync(path.join(folder, file.name), data);
}
};
const delete_lock_file = (folder) => {
for (const file of fs
.readdirSync(folder, { withFileTypes: true })
.filter(
(file) =>
file.isDirectory() || (file.isFile() && file.name === ".lock"),
)) {
// Update html in the folder
if (file.isDirectory()) {
delete_lock_file(path.join(folder, file.name));
continue;
}
// Otherwise, we have a lock file. Delete it.
fs.rmSync(path.join(folder, file.name));
}
};
let error = false;
// generate docs
for (const target of manifest.package.metadata.rumcake_docs.flavours) {
const output_dir = path.join(".", "temp", target.feature);
console.log(`Checking rumcake for ${target.feature}.`);
const rustcheck = process.spawnSync(
"cargo",
[
"check",
"--release",
`--features=${
target.feature
},${manifest.package.metadata.rumcake_docs.features.join(
",",
)},${target.extra_features.join(",")}`,
"--target",
target.triple, // build target triple
"--target-dir",
output_dir.toString(),
"--manifest-path",
path.join("..", "rumcake", "Cargo.toml").toString(),
],
{
stdio: "inherit",
env: {
RUSTFLAGS: "--cfg doc",
},
},
);
if (rustcheck.status) {
error = true;
console.error(`Check failed for ${target.feature}.`);
continue;
}
console.log(`rumcake for ${target.feature} has been checked successfully.`);
console.log(`Building ${target.feature} API docs.`);
const rustdoc = process.spawnSync(
"cargo",
[
"doc",
"--no-deps",
"--release",
`--features=${
target.feature
},${manifest.package.metadata.rumcake_docs.features.join(
",",
)},${target.extra_features.join(",")}`,
"--target",
target.triple, // build target triple
"--target-dir",
output_dir.toString(),
"--manifest-path",
path.join("..", "rumcake", "Cargo.toml").toString(),
],
{
stdio: "inherit",
},
);
if (rustdoc.status) {
error = true;
console.error(`${target.feature} API docs failed to build.`);
continue;
}
console.log(`${target.feature} API docs built.`);
// Generate the navbar
const navbar = nav_template({
current_target: target.feature,
targets: manifest.package.metadata.rumcake_docs.flavours,
});
// Update the HTML
const doc_folder = path.join(output_dir, target.triple, "doc");
update_html_in_folder(doc_folder, navbar);
// Add redirect from root of the target's docs to the rumcake folder containing the docs
fs.writeFileSync(
path.join(doc_folder, "index.html"),
redirect_template({ url: "rumcake" }),
);
// Remove the .lock file in the docs (GitHub doesn't like it because it doesn't have the right permissions)
delete_lock_file(doc_folder);
// Copy the results to ./dist
fs.cpSync(doc_folder, path.join(".", "dist", "api", target.feature), {
recursive: true,
});
}
// Remove temp folder
fs.rmSync(path.join(".", "temp"), { recursive: true });
if (error) {
throw new Error("Some docs failed to build.");
}
// Add an index.html to the /rumcake/api route to redirect to the docs for the first target
fs.writeFileSync(
path.join(".", "dist", "api", "index.html"),
redirect_template({
url: `${manifest.package.metadata.rumcake_docs.flavours[0].feature}/rumcake`,
}),
);
}
main();