Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
60d79c7
Initial work on combyne templates
tbranyen Jul 19, 2014
391b3ed
Started working on replacing the header template
tbranyen Jul 22, 2014
f522bd6
Upgraded all dependencies
tbranyen Sep 24, 2014
5953faa
Converting more templates to Combyne
Sep 25, 2014
6a92dd2
Missed an els tag
Sep 26, 2014
6cffb84
Starting to fix template compile time errors
Sep 26, 2014
ed07f2a
Fixed more compile time errors
Sep 26, 2014
82167de
Stopped using a reserved word in the template.
Sep 26, 2014
16f9358
Bump Combyne version to fix partials issue
tbranyen Sep 26, 2014
38d7fc7
Remove functionInfo references
tbranyen Sep 26, 2014
b7b5c11
Updated gitignore to include log files
Sep 26, 2014
8d440d9
Added more combyne template files
Sep 26, 2014
9e929cf
Starting to work on sync_functions.cc
Sep 27, 2014
e52b9c0
Fixed an issue with the returns in the async function template
Sep 27, 2014
3c992e5
Fixing more bugs with the template build
Sep 29, 2014
cac4c8c
Templates build now
Sep 29, 2014
a288308
Fixed bug in header.h template
Sep 29, 2014
89cec60
More work on getting partials to have the right data
Sep 29, 2014
9c37236
Starting to make combyne templates 1:1 with ejs
Sep 29, 2014
10c2132
Getting close to template 1:1
Sep 29, 2014
e57b390
Combyne templates are almost finished
Sep 30, 2014
415e22a
Fixed sync_function.cc calling convertFromV8 for returns and self vars
Sep 30, 2014
ff17f1c
Removed ejs template files
Sep 30, 2014
6b30987
More fixes to generated files
Oct 2, 2014
70aefe5
Starting to put the filters for templates back in
Oct 3, 2014
f34ccc2
Added hasReturnType and isDoublePointer filter back in
Oct 3, 2014
79fe10b
Added more filters for combine templates back in
Oct 3, 2014
cac31a5
Some cleanup in templates
Oct 3, 2014
89a94ca
Fixed some more issues with the template conversion
Oct 3, 2014
8f16afa
Combyne templates are good
Oct 4, 2014
6865040
Moved comments into partial, fixed alignment
tbranyen Oct 4, 2014
db670ba
Generated templates are looking better
Oct 6, 2014
9a29299
Updated all NPM dependencies
tbranyen Oct 7, 2014
bc0e87e
Updated the README examples to use promises
tbranyen Oct 7, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
/include/*
!/include/functions/copy.h
!/include/wrapper.h

*.log
44 changes: 10 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ git clone git://github.com/tbranyen/nodegit.git
# Enter the repository.
cd nodegit

# Install the template engine, run the code generation script, and install.
npm install ejs && npm run codegen && npm install
# Installs the template engine, run the code generation script, and build.
npm install
```

If you encounter errors, you most likely have not configured the dependencies
Expand Down Expand Up @@ -93,35 +93,19 @@ npm install nodegit --msvs_version=2013
### Cloning a repository and reading a file: ###

``` javascript
var clone = require("nodegit").Repo.clone;
var clone = require("nodegit").Repository.clone;

// Clone a given repository into a specific folder.
clone("https://github.com/nodegit/nodegit", "tmp", null, function(err, repo) {
if (err) {
throw err;
}

clone("https://github.com/nodegit/nodegit", "tmp", null).then(function(repo) {
// Use a known commit sha from this repository.
var sha = "59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5";

// Look up this known commit.
repo.getCommit(sha, function(err, commit) {
if (err) {
throw err;
}

repo.getCommit(sha).then(function(commit) {
// Look up a specific file within that commit.
commit.getEntry("README.md", function(err, entry) {
if (err) {
throw err;
}

commit.getEntry("README.md").then(function(entry) {
// Get the blob contents from the file.
entry.getBlob(function(err, blob) {
if (err) {
throw err;
}

entry.getBlob().then(function(blob) {
// Show the name, sha, and filesize in byes.
console.log(entry.name() + entry.sha() + blob.size() + "b");

Expand All @@ -139,20 +123,12 @@ clone("https://github.com/nodegit/nodegit", "tmp", null, function(err, repo) {
### Emulating git log: ###

``` javascript
var open = require("nodegit").Repo.open;
var open = require("nodegit").Repository.open;

// Open the repository directory.
open("tmp", function(err, repo) {
if (err) {
throw err;
}

open("tmp").then(function(repo) {
// Open the master branch.
repo.getMaster(function(err, branch) {
if (err) {
throw err;
}

repo.getMaster().then(function(branch) {
// Create a new history event emitter.
var history = branch.history();

Expand Down
3 changes: 3 additions & 0 deletions generate/filters/and.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(value, other) {
return value && other;
};
30 changes: 30 additions & 0 deletions generate/filters/args_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = function(args) {
var result = [],
cArg,
jsArg;

for(cArg = 0, jsArg = 0; cArg < args.length; cArg++) {
var arg = args[cArg];

if (!arg.isReturn && !arg.isSelf && !arg.isPayload) {
arg.isJsArg = true;
arg.jsArg = jsArg;

jsArg++;
}

if (cArg === args.length -1) {
arg.lastArg = true;
}
else {
arg.lastArg = false;
}

arg.cArg = cArg;
arg.isCppClassStringOrArray = ~["String", "Array"].indexOf(arg.cppClassName);

result.push(arg);
}

return result;
};
5 changes: 5 additions & 0 deletions generate/filters/cpp_to_v8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var isV8Value = require("./is_v8_value");

module.exports = function(cppClassName) {
return isV8Value(cppClassName) ? cppClassName : "Object";
};
3 changes: 3 additions & 0 deletions generate/filters/default_value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(cType) {
return cType === "git_otype" ? "GIT_OBJ_ANY" : "0";
};
17 changes: 17 additions & 0 deletions generate/filters/fields_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function(fields) {
var result = [];

fields.forEach(function (field){
var fieldInfo = {};

fieldInfo.__proto__ = field;

fieldInfo.parsedName = field.name || "result";
fieldInfo.isCppClassIntType = ~["Uint32", "Int32"].indexOf(field.cppClassName);
fieldInfo.parsedClassName = (field.cppClassName || '').toLowerCase() + "_t";

result.push(fieldInfo);
});

return result;
};
7 changes: 7 additions & 0 deletions generate/filters/has_return_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(functionInfo) {
if (functionInfo.return) {
return functionInfo.return.cType != "void" || functionInfo.return.isErrorCode;
}

return false;
};
15 changes: 15 additions & 0 deletions generate/filters/has_returns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function(fn) {
var args = fn.args || [];
var result = args.some(function (arg) {
return arg.isReturn;
});

if (!result
&& fn.return
&& !fn.return.isErrorCode
&& fn.return.cType != "void") {
result = true;
}

return result || (fn.return && fn.return.isErrorCode);
};
3 changes: 3 additions & 0 deletions generate/filters/is_double_pointer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(cType) {
return /\s*\*\*\s*/.test(cType);
};
3 changes: 3 additions & 0 deletions generate/filters/is_pointer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(cType) {
return /\s*\*\s*/.test(cType);
};
14 changes: 14 additions & 0 deletions generate/filters/is_v8_value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var v8 = [
"Boolean",
"Number",
"String",
"Integer",
"Int32",
"Uint32",
"Date",
"Function"
];

module.exports = function(cppClassName) {
return v8.indexOf(cppClassName) > -1;
};
18 changes: 18 additions & 0 deletions generate/filters/js_args_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function(args) {
var cArg,
jsArg;

if (!args) {
return 0;
}

for(cArg = 0, jsArg = 0; cArg < args.length; cArg++) {
var arg = args[cArg];

if (!arg.isReturn && !arg.isSelf && !arg.isPayload) {
jsArg++;
}
}

return jsArg;
};
3 changes: 3 additions & 0 deletions generate/filters/or.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(value, other) {
return value || other;
};
3 changes: 3 additions & 0 deletions generate/filters/replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(value, find, replace) {
return value.replace(find, replace);
};
15 changes: 15 additions & 0 deletions generate/filters/returns_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function(fn) {
var args = fn.args || [];
var result = args.reduce(function (currentValue, arg) {
return currentValue + (arg.isReturn ? 1 : 0);
}, 0);

if (!result
&& fn.return
&& !fn.return.isErrorCode
&& fn.return.cType != "void") {
result = 1;
}

return result;
};
40 changes: 40 additions & 0 deletions generate/filters/returns_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = function(fn, argReturnsOnly, isAsync) {
var result = [];
var args = fn.args || [];

args.forEach(function (arg) {
if (!arg.isReturn) return;

var return_info = {};

return_info.__proto__ = arg;

return_info.parsedName = isAsync ? "baton->" + return_info.name : return_info.name;
return_info.isCppClassIntType = ~['Uint32', 'Int32'].indexOf(return_info.cppClassName);
return_info.parsedClassName = (return_info.cppClassName || '').toLowerCase() + "_t";
return_info.jsNameOrName = return_info.jsName | return_info.name;
return_info.jsOrCppClassName = return_info.jsClassName || return_info.cppClassName;

result.push(return_info);
});

if (!result.length
&& !argReturnsOnly
&& fn.return
&& !fn.return.isErrorCode
&& fn.return.cType != "void") {
var return_info = {};

return_info.__proto__ = fn.return;
return_info.parsedName = return_info.name && isAsync ? "baton->" + return_info.name : "result";
return_info.isCppClassIntType = ~['Uint32', 'Int32'].indexOf(return_info.cppClassName);
return_info.parsedClassName = (return_info.cppClassName || '').toLowerCase() + "_t";
return_info.jsNameOrName = return_info.jsName | return_info.name;
return_info.jsOrCppClassName = return_info.jsClassName || return_info.cppClassName;

result.push(return_info);
}


return result;
};
3 changes: 3 additions & 0 deletions generate/filters/un_pointer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(cType) {
return cType.replace(/\s*\*\s*$/, "");
};
3 changes: 3 additions & 0 deletions generate/filters/upper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(value) {
return value.toUpperCase();
};
98 changes: 71 additions & 27 deletions generate/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,88 @@
// https://github.com/nodegit/nodegit/issues/94
const fs = require("fs");
const ejs = require("ejs");
const path = require("path");
const combyne = require("combyne");
const file = require("./util/file");
const idefs = require("./idefs");

var local = path.join.bind(null, __dirname);
// Customize the delimiters so as to not process `{{{` or `}}}`.
combyne.settings.delimiters = {
START_RAW: "{{=",
END_RAW: "=}}"
};

var classTemplate = ejs.compile(
"" + fs.readFileSync(local("templates/class.cc.ejs")), {
filename: "class.cc"
});
var partials = {
asyncFunction: file.read("partials/async_function.cc"),
convertFromV8: file.read("partials/convert_from_v8.cc"),
convertToV8: file.read("partials/convert_to_v8.cc"),
doc: file.read("partials/doc.cc"),
fields: file.read("partials/fields.cc"),
guardArguments: file.read("partials/guard_arguments.cc"),
syncFunction: file.read("partials/sync_function.cc")
};

var templates = {
class: file.read("templates/class.cc"),
header: file.read("templates/header.h"),
binding: file.read("templates/binding.gyp"),
nodegit: file.read("templates/nodegit.cc")
};

var filters = {
upper: require("./filters/upper"),
replace: require("./filters/replace"),
or: require("./filters/or"),
and: require("./filters/and"),
defaultValue: require("./filters/default_value"),
argsInfo: require("./filters/args_info"),
cppToV8: require("./filters/cpp_to_v8"),
jsArgsCount: require("./filters/js_args_count"),
isV8Value: require("./filters/is_v8_value"),
isPointer: require("./filters/is_pointer"),
isDoublePointer: require("./filters/is_double_pointer"),
unPointer: require("./filters/un_pointer"),
hasReturnType: require("./filters/has_return_type"),
hasReturns: require("./filters/has_returns"),
returnsCount: require("./filters/returns_count"),
returnsInfo: require("./filters/returns_info"),
fieldsInfo: require("./filters/fields_info")
};

var headerTemplate = ejs.compile(
"" + fs.readFileSync(local("templates/header.h.ejs")), {
filename: "header.h"
// Convert Buffers to Combyne templates.
Object.keys(templates).forEach(function(template) {
templates[template] = combyne(templates[template]);

// Attach all filters to all templates.
Object.keys(filters).forEach(function(filter) {
templates[template].registerFilter(filter, filters[filter]);
});
});

var bindingTemplate = ejs.compile(
"" + fs.readFileSync(local("templates/binding.gyp.ejs")), {});
// Attach all partials to select templates.
Object.keys(partials).forEach(function(partial) {
templates.class.registerPartial(partial, combyne(partials[partial]));
});

var nodegitSourceTemplate = ejs.compile(
"" + fs.readFileSync(local("templates/nodegit.cc.ejs")), {});

// Determine which definitions to actually include in the source code.
var enabled = idefs.filter(function(idef) {
// Additionally provide a friendly name to the actual filename.
idef.name = path.basename(idef.filename, ".h");

// We need some custom data on each of the functions
idef.functions.forEach(function(fn) {
fn.cppClassName = idef.cppClassName;
});

return !idef.ignore;
});

enabled.forEach(function(idef) {
fs.writeFileSync(local("../include/", idef.filename), headerTemplate(idef));

fs.writeFileSync(
local("../src/", path.basename(idef.filename, ".h")) + ".cc",
classTemplate(idef));
// TODO Wipe out all existing source files.

fs.writeFileSync(local("../binding.gyp"), bindingTemplate({
idefs: idefs
}));
// Write out single purpose templates.
file.write("../binding.gyp", templates.binding.render(enabled));
file.write("../src/nodegit.cc", templates.nodegit.render(enabled));

fs.writeFileSync(local("../src/nodegit.cc"), nodegitSourceTemplate({
idefs: idefs
}));
// Write out all the classes.
enabled.forEach(function(idef) {
file.write("../src/" + idef.name + ".cc", templates.class.render(idef));
file.write("../include/" + idef.name + ".h", templates.header.render(idef));
});
Loading