forked from octokit/octokit.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
executable file
·471 lines (415 loc) · 17.9 KB
/
generate.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env node
/** section: github, internal
* class ApiGenerator
*
* Copyright 2012 Cloud9 IDE, Inc.
*
* This product includes software developed by
* Cloud9 IDE, Inc (http://c9.io).
*
* Author: Mike de Boer <[email protected]>
**/
"use strict";
var Fs = require("fs");
var Path = require("path");
var Url = require("url");
var Async = require("async");
var Optimist = require("optimist");
var Util = require("./util");
var IndexTpl = Fs.readFileSync(__dirname + "/templates/index.js.tpl", "utf8");
var SectionTpl = Fs.readFileSync(__dirname + "/templates/section.js.tpl", "utf8");
var HandlerTpl = Fs.readFileSync(__dirname + "/templates/handler.js.tpl", "utf8");
var AfterRequestTpl = Fs.readFileSync(__dirname + "/templates/after_request.js.tpl", "utf8");
var TestSectionTpl = Fs.readFileSync(__dirname + "/templates/test_section.js.tpl", "utf8");
var TestHandlerTpl = Fs.readFileSync(__dirname + "/templates/test_handler.js.tpl", "utf8");
var DocSectionsMap = {
"authorization": "oauth_authorizations",
"gitdata": "git",
"pull-requests": "pulls",
"pullRequests": "pulls",
"releases": "repos/releases",
"user": "users"
};
var main = module.exports = function(versions, tests, restore) {
Util.log("Generating for versions", Object.keys(versions));
Async.each(Object.keys(versions), function(version, versionDone) {
var dir = Path.join(__dirname, "api", version);
// If we're in restore mode, move .bak file back to their original position
// and short-circuit.
if (restore) {
var bakRE = /\.bak$/;
var files = Fs.readdirSync(dir).filter(function(file) {
return bakRE.test(file);
}).forEach(function(file) {
var from = Path.join(dir, file);
var to = Path.join(dir, file.replace(/\.bak$/, ""));
Fs.renameSync(from, to);
Util.log("Restored '" + file + "' (" + version + ")");
});
versionDone();
return;
}
var routes = versions[version];
var defines = routes.defines;
delete routes.defines;
var headers = defines["response-headers"];
// cast header names to lowercase.
if (headers && headers.length)
headers = headers.map(function(header) { return header.toLowerCase(); });
var sections = {};
var testSections = {};
// First fetch and cache the documentation sections, but only if the site
// can be reached at the moment.
var addDocLink = false;
var docsUrl = defines.constants.documentation;
pingDocumentationUrl(docsUrl, function(success) {
if (success) {
Util.log("Using '" + docsUrl + "' to generate links to online documentation.");
addDocLink = true;
Async.each(Object.keys(routes), function(section, sectionDone) {
section = DocSectionsMap[section] || section;
getSectionDocument(docsUrl + "/" + section + "/", function(err) {
// Ignore errors here...
sectionDone();
});
}, generateFiles);
return;
}
Util.log("Could not reach documentation site, continuing without " +
"adding links to online documentation.");
generateFiles();
});
function generateFiles() {
function createComment(block, section, funcName, indent) {
var paramsStruct = block.params;
var params = Object.keys(paramsStruct);
var comment = [
indent + "/** section: github",
indent + " * " + section + "#" + funcName + "(msg, callback) -> null",
indent + " * - msg (Object): Object that contains the parameters and their values to be sent to the server.",
indent + " * - callback (Function): function to call when the request is finished " +
"with an error as first argument and result data as second argument.",
indent + " *",
indent + " * ##### Params on the `msg` object:",
indent + " *"
];
comment.push(indent + " * - headers (Object): Optional. Key/ value pair "
+ "of request headers to pass along with the HTTP request. Valid headers are: "
+ "'" + defines["request-headers"].join("', '") + "'.");
if (!params.length)
comment.push(indent + " * No other params, simply pass an empty Object literal `{}`");
var paramName, def, line;
for (var i = 0, l = params.length; i < l; ++i) {
paramName = params[i];
if (paramName.charAt(0) == "$") {
paramName = paramName.substr(1);
if (!defines.params[paramName]) {
Util.log("Invalid variable parameter name substitution; param '" +
paramName + "' not found in defines block", "fatal");
process.exit(1);
}
else
def = defines.params[paramName];
}
else
def = paramsStruct[paramName];
line = indent + " * - " + paramName + " (" + (def.type || "mixed") + "): " +
(def.required ? "Required. " : "Optional. ");
if (def.description)
line += def.description;
if (def.validation)
line += " Validation rule: ` " + def.validation + " `.";
comment.push(line);
}
if (addDocLink) {
var docSection = DocSectionsMap[section] || section;
var url = searchSectionUrl(docsUrl + "/" + docSection + "/",
block.method + " " + block.url);
if (url) {
comment.push(
indent + " *",
indent + " * " + url);
}
else {
Util.log("No documentation link found for '" + block.url +
"' on '" + docsUrl + "/" + docSection + "/'");
}
}
return comment.join("\n") + "\n" + indent + " **/";
}
function getParams(paramsStruct, indent) {
var params = Object.keys(paramsStruct);
if (!params.length)
return "{}";
var values = [];
var paramName, def;
for (var i = 0, l = params.length; i < l; ++i) {
paramName = params[i];
if (paramName.charAt(0) == "$") {
paramName = paramName.substr(1);
if (!defines.params[paramName]) {
Util.log("Invalid variable parameter name substitution; param '" +
paramName + "' not found in defines block", "fatal");
process.exit(1);
}
else
def = defines.params[paramName];
}
else
def = paramsStruct[paramName];
values.push(indent + " " + paramName + ": \"" + def.type + "\"");
}
return "{\n" + values.join(",\n") + "\n" + indent + "}";
}
function prepareApi(struct, baseType) {
if (!baseType)
baseType = "";
Object.keys(struct).forEach(function(routePart) {
var block = struct[routePart];
if (!block)
return;
var messageType = baseType + "/" + routePart;
if (block.url && block.params) {
// we ended up at an API definition part!
var parts = messageType.split("/");
var section = Util.toCamelCase(parts[1].toLowerCase());
if (!block.method) {
throw new Error("No HTTP method specified for " + messageType +
"in section " + section);
}
parts.splice(0, 2);
var funcName = Util.toCamelCase(parts.join("-"));
var comment = createComment(block, section, funcName, " ");
// add the handler to the sections
if (!sections[section])
sections[section] = [];
var afterRequest = "";
if (headers && headers.length) {
afterRequest = AfterRequestTpl.replace("<%headers%>", "\"" +
headers.join("\", \"") + "\"");
}
sections[section].push(HandlerTpl
.replace("<%funcName%>", funcName)
.replace("<%comment%>", comment)
.replace("<%afterRequest%>", afterRequest)
);
// add test to the testSections
if (!testSections[section])
testSections[section] = [];
testSections[section].push(TestHandlerTpl
.replace("<%name%>", block.method + " " + block.url + " (" + funcName + ")")
.replace("<%funcName%>", section + "." + funcName)
.replace("<%params%>", getParams(block.params, " "))
);
}
else {
// recurse into this block next:
prepareApi(block, messageType);
}
});
}
Util.log("Converting routes to functions");
prepareApi(routes);
Util.log("Writing files to version dir");
var sectionNames = Object.keys(sections);
Util.log("Writing index.js file for version " + version);
Fs.writeFileSync(Path.join(dir, "index.js"),
IndexTpl
.replace("<%name%>", defines.constants.name)
.replace("<%description%>", defines.constants.description)
.replace("<%scripts%>", "\"" + sectionNames.join("\", \"") + "\""),
"utf8");
Object.keys(sections).forEach(function(section) {
var def = sections[section];
Util.log("Writing '" + section + ".js' file for version " + version);
Fs.writeFileSync(Path.join(dir, section + ".js"), SectionTpl
.replace(/<%sectionName%>/g, section)
.replace("<%sectionBody%>", def.join("\n")),
"utf8"
);
// When we don't need to generate tests, bail out here.
if (!tests)
return;
def = testSections[section];
// test if previous tests already contained implementations by checking
// if the difference in character count between the current test file
// and the newly generated one is more than twenty characters.
var body = TestSectionTpl
.replace("<%version%>", version.replace("v", ""))
.replace(/<%sectionName%>/g, section)
.replace("<%testBody%>", def.join("\n\n"));
var path = Path.join(dir, section + "Test.js");
if (Fs.existsSync(path) && Math.abs(Fs.readFileSync(path, "utf8").length - body.length) >= 20) {
Util.log("Moving old test file to '" + path + ".bak' to preserve tests " +
"that were already implemented. \nPlease be sure te check this file " +
"and move all implemented tests back into the newly generated test!", "error");
Fs.renameSync(path, path + ".bak");
}
Util.log("Writing test file for " + section + ", version " + version);
Fs.writeFileSync(path, body, "utf8");
});
versionDone();
}
}, function() {});
};
function getRequestOptions(url, method) {
var parsedUrl = Url.parse(url);
var protocol = parsedUrl.protocol.replace(/[:\/]+/, "");
return {
options: {
hostname: parsedUrl.hostname,
method: method || "GET",
path: parsedUrl.pathname,
port: parsedUrl.port || protocol == "https" ? 443 : 80
},
protocol: protocol
};
}
function pingDocumentationUrl(url, callback) {
if (!url) {
callback(false);
return;
}
var options = getRequestOptions(url, "HEAD");
function callCallback(result) {
if (!callback)
return;
var cb = callback;
callback = undefined;
cb(result);
}
var req = require(options.protocol).request(options.options, function(res) {
if (res.statusCode >= 400 && res.statusCode < 600 || res.statusCode < 10) {
callCallback(false);
} else {
callCallback(true);
}
});
req.on("error", function(err) {
callCallback(false);
});
req.end();
}
function searchSectionUrl(url, needle) {
var doc = sectionDocCache[url];
if (!doc)
return null;
var idx = doc.indexOf(needle);
var userIdx = needle.indexOf(":user");
// GH uses ':owner' to identify the :user param for clearer documentation,
// so we attempt to substitute that here to search for instead.
if (idx == -1 && userIdx != -1) {
needle = needle.substr(0, userIdx) + ":owner" + needle.substr(userIdx + 5);
idx = doc.indexOf(needle);
}
// We tried.
if (idx == -1)
return null;
var subDoc = doc.substr(0, idx);
// Search for the first header with an ID attribute.
var headerIdx = -1;
for (var i = 2; i >= 1 && headerIdx == -1; --i) {
headerIdx = subDoc.lastIndexOf("<h" + i + " id=");
}
// We tried.
if (headerIdx == -1)
return null;
// Fetch the content of the ID attribute.
var id = subDoc.substr(headerIdx).match(/id=["']+([^'">]*)/);
if (!id || !id.length)
return null;
return url + "#" + id[1];
}
var sectionDocCache = {};
function getSectionDocument(url, callback) {
// First try to fetch the section document from cache.
if (sectionDocCache[url]) {
callCallback(null, sectionDocCache[url]);
return;
}
var options = getRequestOptions(url);
function callCallback(err, result) {
if (callback) {
var cb = callback;
callback = undefined;
cb(err, result);
}
}
Util.log("Fetching documentation section '" + url + "'");
var req = require(options.protocol).request(options.options, function(res) {
res.setEncoding("utf8");
var data = "";
res.on("data", function(chunk) {
data += chunk;
});
res.on("error", function(err) {
callCallback(err);
});
res.on("end", function() {
if (res.statusCode >= 400 && res.statusCode < 600 || res.statusCode < 10) {
callCallback(data, res.statusCode);
} else {
sectionDocCache[url] = data;
callCallback(null, data);
}
});
});
req.on("error", function(e) {
callCallback(e.message);
});
req.on("timeout", function() {
callCallback("Gateway timed out for '" + url + "'");
});
req.end();
}
if (!module.parent) {
var argv = Optimist
.wrap(80)
.usage("Generate the implementation of the node-github module, including "
+ "unit-test scaffolds.\nUsage: $0 [-r] [-v VERSION]")
.alias("r", "restore")
.describe("r", "Restore .bak files, generated by a previous run, to the original")
.alias("v", "version")
.describe("v", "Semantic version number of the API to generate. Example: '3.0.0'")
.alias("t", "tests")
.describe("t", "Also generate unit test scaffolds")
.alias("h", "help")
.describe("h", "Display this usage information")
.boolean(["r", "t", "h"])
.argv;
if (argv.help) {
Util.log(Optimist.help());
process.exit();
}
var baseDir = Path.join(__dirname, "api");
var availVersions = {};
Fs.readdirSync(baseDir).forEach(function(version) {
var path = Path.join(baseDir, version, "routes.json");
if (!Fs.existsSync(path))
return;
var routes = JSON.parse(Fs.readFileSync(path, "utf8"));
if (!routes.defines)
return;
availVersions[version] = routes;
});
if (!Object.keys(availVersions).length) {
Util.log("No versions available to generate.", "fatal");
process.exit(1);
}
var versions = {};
if (argv.version) {
if (argv.version.charAt(0) != "v")
argv.version = argv.v = "v" + argv.version;
if (!availVersions[argv.version]) {
Util.log("Version '" + argv.version + "' is not available", "fatal");
process.exit(1);
}
versions[argv.version] = availVersions[argv.version];
}
if (!Object.keys(versions).length) {
Util.log("No versions specified via the command line, generating for all available versions.");
versions = availVersions;
}
Util.log("Starting up...");
main(versions, argv.tests, argv.restore);
}