Skip to content

Commit 9531628

Browse files
committed
Merge pull request #303 from nodegit/remove-idefs-dependencies
move nodegit.js to a template to remove idefs dependency
2 parents 9ae685d + 661c21c commit 9531628

File tree

3 files changed

+46
-38
lines changed

3 files changed

+46
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/src/
1010
/include/
1111
/lib/enums.js
12+
/lib/nodegit.js
1213

1314

1415
/vendor/Release
Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var Promise = require("nodegit-promise");
22
var promisify = require("promisify-node");
3-
var descriptors = require("../generate/output/idefs.json");
43
var rawApi;
54

65
// Attempt to load the production release first, if it fails fall back to the
@@ -13,31 +12,40 @@ catch (e) {
1312
rawApi = require("../build/Debug/nodegit");
1413
}
1514

16-
// Native methods do not return an identifiable function, so this function will
17-
// filter down the function identity to match the libgit2 descriptor.
18-
descriptors.forEach(function(descriptor) {
19-
if (descriptor.type == "enum") {
20-
return;
21-
}
22-
var Ctor = rawApi[descriptor.jsClassName];
23-
24-
// Iterate over each function in the file.
25-
descriptor.functions.filter(function(func) {
26-
return func.isAsync;
27-
}).forEach(function(asyncFunc) {
28-
var original = null;
29-
30-
// Special case when you have a prototype method.
31-
if (asyncFunc.isPrototypeMethod && Ctor.prototype) {
32-
original = Ctor.prototype[asyncFunc.jsFunctionName];
33-
Ctor.prototype[asyncFunc.jsFunctionName] = promisify(original);
34-
}
35-
else {
36-
original = Ctor[asyncFunc.jsFunctionName];
37-
Ctor[asyncFunc.jsFunctionName] = promisify(original);
38-
}
39-
});
40-
});
15+
// Native methods do not return an identifiable function, so we
16+
// have to override them here
17+
/* jshint ignore:start */
18+
{% each . as idef %}
19+
{% if idef.type != "enum" %}
20+
21+
var _{{ idef.jsClassName }}
22+
= rawApi.{{idef.jsClassName}};
23+
24+
{% each idef.functions as fn %}
25+
{% if fn.isAsync %}
26+
27+
{% if fn.isPrototypeMethod %}
28+
29+
var _{{ idef.jsClassName }}_{{ fn.jsFunctionName}}
30+
= _{{ idef.jsClassName }}.prototype.{{ fn.jsFunctionName }};
31+
_{{ idef.jsClassName }}.prototype.{{ fn.jsFunctionName }}
32+
= promisify(_{{ idef.jsClassName }}_{{ fn.jsFunctionName}});
33+
34+
{% else %}
35+
36+
var _{{ idef.jsClassName }}_{{ fn.jsFunctionName}}
37+
= _{{ idef.jsClassName }}.{{ fn.jsFunctionName }};
38+
_{{ idef.jsClassName }}.{{ fn.jsFunctionName }}
39+
= promisify(_{{ idef.jsClassName }}_{{ fn.jsFunctionName}});
40+
41+
{% endif %}
42+
43+
{% endif %}
44+
{% endeach %}
45+
46+
{% endif %}
47+
{% endeach %}
48+
/* jshint ignore:end */
4149

4250
// Set the exports prototype to the raw API.
4351
exports.__proto__ = rawApi;

generate/scripts/generateNativeCode.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ module.exports = function() {
4444
class_header: utils.readFile("combyne/templates/class_header.h"),
4545
struct_header: utils.readFile("combyne/templates/struct_header.h"),
4646
binding: utils.readFile("combyne/templates/binding.gyp"),
47-
nodegit: utils.readFile("combyne/templates/nodegit.cc"),
47+
nodegitCC: utils.readFile("combyne/templates/nodegit.cc"),
48+
nodegitJS: utils.readFile("combyne/templates/nodegit.js"),
4849
enums: utils.readFile("combyne/templates/enums.js")
4950
};
5051

@@ -101,19 +102,18 @@ module.exports = function() {
101102
}).then(function() {
102103
// Write out single purpose templates.
103104
utils.writeFile("../binding.gyp", beautify(templates.binding.render(enabled)));
104-
utils.writeFile("../src/nodegit.cc", templates.nodegit.render(enabled));
105-
106-
105+
utils.writeFile("../src/nodegit.cc", templates.nodegitCC.render(enabled));
106+
utils.writeFile("../lib/nodegit.js", beautify(templates.nodegitJS.render(enabled)));
107107
// Write out all the classes.
108108
enabled.forEach(function(idef) {
109109
try {
110-
if (idef.type == "struct") {
111-
utils.writeFile("../src/" + idef.filename + ".cc", templates.struct_content.render(idef));
112-
utils.writeFile("../include/" + idef.filename + ".h", templates.struct_header.render(idef));
113-
}
114-
else if (idef.type == "class") {
115-
utils.writeFile("../src/" + idef.filename + ".cc", templates.class_content.render(idef));
116-
utils.writeFile("../include/" + idef.filename + ".h", templates.class_header.render(idef));
110+
if (idef.type && idef.type != "enum") {
111+
utils.writeFile(
112+
"../src/" + idef.filename + ".cc", templates[idef.type + "_content"].render(idef)
113+
);
114+
utils.writeFile(
115+
"../include/" + idef.filename + ".h", templates[idef.type + "_header"].render(idef)
116+
);
117117
}
118118
}
119119
catch (e) {
@@ -123,7 +123,6 @@ module.exports = function() {
123123
}
124124
});
125125

126-
127126
utils.writeFile("../lib/enums.js", beautify(templates.enums.render(enabled)));
128127
}).then(function() {
129128
return exec("command -v astyle").then(function(astyle) {

0 commit comments

Comments
 (0)