Skip to content

Commit 00f3574

Browse files
committed
Initial static codegen target for reference [ci skip]
1 parent 5785dee commit 00f3574

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

cli/targets/static.js

+59-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,66 @@ module.exports = static_target;
22

33
static_target.private = true;
44

5+
// This file contains the beginnings of static code generation.
6+
// It doesn't generate anything useful, yet, but can be used as a starting point.
7+
8+
// TBD:
9+
// - Generate a single file or scaffold an entire project directory? Both?
10+
// - Targets: ES5, ES6, TypeScript? CommonJS? AMD?
11+
// - Is there a need for a minimal runtime composed only of Reader/Writer/minimal util?
12+
// - What about generating comments and typescript definitions for non-ts targets?
13+
514
var protobuf = require("../..");
615

16+
var Type = protobuf.Type,
17+
Service = protobuf.Service,
18+
Enum = protobuf.Enum,
19+
Namespace = protobuf.Namespace,
20+
codegen = protobuf.util.codegen;
21+
22+
var out = [];
23+
724
function static_target(root, options, callback) {
8-
callback(Error("not implemented"));
25+
tree = {};
26+
try {
27+
out.push("var protobuf = require(\"protobufjs\");");
28+
out.push("var root = exports;");
29+
buildNamespace("root", root);
30+
callback(null, out.join('\n'));
31+
} catch (err) {
32+
callback(err);
33+
} finally {
34+
out = [];
35+
}
36+
}
37+
38+
function buildNamespace(ref, ns) {
39+
if (!ns)
40+
return;
41+
ns.nestedArray.forEach(function(nested) {
42+
if (nested instanceof Type)
43+
buildType(ref, nested);
44+
else if (nested instanceof Service)
45+
buildService(ref, nested);
46+
else if (nested instanceof Enum)
47+
buildEnum(ref, nested);
48+
else if (nested instanceof Namespace)
49+
buildNamespace(ref, nested);
50+
});
51+
}
52+
53+
function buildType(ref, type) {
54+
out.push("");
55+
out.push(ref + "." + type.name + " = function " + type.name + "() {};"); // currently just an empty function
56+
buildNamespace(ref + "." + type.name, type);
57+
}
58+
59+
function buildService(ref, service) {
60+
out.push("");
61+
out.push(ref + "." + service.name + " = {};"); // currently just an empty object
62+
}
63+
64+
function buildEnum(ref, enm) {
65+
out.push("");
66+
out.push(ref + "." + enm.name + " = " + JSON.stringify(enm.values, null, "\t") + ";");
967
}

0 commit comments

Comments
 (0)