diff --git a/.gitignore b/.gitignore index 48f8535aa..052792737 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /src/ /include/ /lib/enums.js +/lib/nodegit.js /vendor/Release diff --git a/lib/nodegit.js b/generate/combyne/templates/nodegit.js similarity index 54% rename from lib/nodegit.js rename to generate/combyne/templates/nodegit.js index 11461a265..5ea95e89e 100644 --- a/lib/nodegit.js +++ b/generate/combyne/templates/nodegit.js @@ -1,6 +1,5 @@ var Promise = require("nodegit-promise"); var promisify = require("promisify-node"); -var descriptors = require("../generate/output/idefs.json"); var rawApi; // Attempt to load the production release first, if it fails fall back to the @@ -13,31 +12,40 @@ catch (e) { rawApi = require("../build/Debug/nodegit"); } -// Native methods do not return an identifiable function, so this function will -// filter down the function identity to match the libgit2 descriptor. -descriptors.forEach(function(descriptor) { - if (descriptor.type == "enum") { - return; - } - var Ctor = rawApi[descriptor.jsClassName]; - - // Iterate over each function in the file. - descriptor.functions.filter(function(func) { - return func.isAsync; - }).forEach(function(asyncFunc) { - var original = null; - - // Special case when you have a prototype method. - if (asyncFunc.isPrototypeMethod && Ctor.prototype) { - original = Ctor.prototype[asyncFunc.jsFunctionName]; - Ctor.prototype[asyncFunc.jsFunctionName] = promisify(original); - } - else { - original = Ctor[asyncFunc.jsFunctionName]; - Ctor[asyncFunc.jsFunctionName] = promisify(original); - } - }); -}); +// Native methods do not return an identifiable function, so we +// have to override them here +/* jshint ignore:start */ +{% each . as idef %} + {% if idef.type != "enum" %} + + var _{{ idef.jsClassName }} + = rawApi.{{idef.jsClassName}}; + + {% each idef.functions as fn %} + {% if fn.isAsync %} + + {% if fn.isPrototypeMethod %} + + var _{{ idef.jsClassName }}_{{ fn.jsFunctionName}} + = _{{ idef.jsClassName }}.prototype.{{ fn.jsFunctionName }}; + _{{ idef.jsClassName }}.prototype.{{ fn.jsFunctionName }} + = promisify(_{{ idef.jsClassName }}_{{ fn.jsFunctionName}}); + + {% else %} + + var _{{ idef.jsClassName }}_{{ fn.jsFunctionName}} + = _{{ idef.jsClassName }}.{{ fn.jsFunctionName }}; + _{{ idef.jsClassName }}.{{ fn.jsFunctionName }} + = promisify(_{{ idef.jsClassName }}_{{ fn.jsFunctionName}}); + + {% endif %} + + {% endif %} + {% endeach %} + + {% endif %} +{% endeach %} +/* jshint ignore:end */ // Set the exports prototype to the raw API. exports.__proto__ = rawApi; diff --git a/generate/scripts/generateNativeCode.js b/generate/scripts/generateNativeCode.js index e4e08c1cc..8ee98f74c 100644 --- a/generate/scripts/generateNativeCode.js +++ b/generate/scripts/generateNativeCode.js @@ -44,7 +44,8 @@ module.exports = function() { class_header: utils.readFile("combyne/templates/class_header.h"), struct_header: utils.readFile("combyne/templates/struct_header.h"), binding: utils.readFile("combyne/templates/binding.gyp"), - nodegit: utils.readFile("combyne/templates/nodegit.cc"), + nodegitCC: utils.readFile("combyne/templates/nodegit.cc"), + nodegitJS: utils.readFile("combyne/templates/nodegit.js"), enums: utils.readFile("combyne/templates/enums.js") }; @@ -101,19 +102,18 @@ module.exports = function() { }).then(function() { // Write out single purpose templates. utils.writeFile("../binding.gyp", beautify(templates.binding.render(enabled))); - utils.writeFile("../src/nodegit.cc", templates.nodegit.render(enabled)); - - + utils.writeFile("../src/nodegit.cc", templates.nodegitCC.render(enabled)); + utils.writeFile("../lib/nodegit.js", beautify(templates.nodegitJS.render(enabled))); // Write out all the classes. enabled.forEach(function(idef) { try { - if (idef.type == "struct") { - utils.writeFile("../src/" + idef.filename + ".cc", templates.struct_content.render(idef)); - utils.writeFile("../include/" + idef.filename + ".h", templates.struct_header.render(idef)); - } - else if (idef.type == "class") { - utils.writeFile("../src/" + idef.filename + ".cc", templates.class_content.render(idef)); - utils.writeFile("../include/" + idef.filename + ".h", templates.class_header.render(idef)); + if (idef.type && idef.type != "enum") { + utils.writeFile( + "../src/" + idef.filename + ".cc", templates[idef.type + "_content"].render(idef) + ); + utils.writeFile( + "../include/" + idef.filename + ".h", templates[idef.type + "_header"].render(idef) + ); } } catch (e) { @@ -123,7 +123,6 @@ module.exports = function() { } }); - utils.writeFile("../lib/enums.js", beautify(templates.enums.render(enabled))); }).then(function() { return exec("command -v astyle").then(function(astyle) {