Generate symbols before exports

This commit is contained in:
Tim Schaub
2014-04-11 12:12:35 -06:00
parent fe5d8712f2
commit 88e41f25ea
3 changed files with 117 additions and 82 deletions
+1 -5
View File
@@ -218,12 +218,8 @@ def build_ol_all_js(t):
t.output('%(JAVA)s', '-server', '-XX:+TieredCompilation', '-jar', t.output('%(JAVA)s', '-server', '-XX:+TieredCompilation', '-jar',
PLOVR_JAR, 'build', 'buildcfg/ol-all.json') PLOVR_JAR, 'build', 'buildcfg/ol-all.json')
@target('build/symbols.json', 'tasks/generate-symbols.js', SRC)
def build_symbols_js(t):
t.run('node', 'tasks/generate-symbols.js')
@target('build/exports.js', SRC)
@target('build/exports.js', 'tasks/generate-exports.js', 'build/symbols.json')
def build_exports_js(t): def build_exports_js(t):
t.run('node', 'tasks/generate-exports.js') t.run('node', 'tasks/generate-exports.js')
+103 -74
View File
@@ -3,13 +3,65 @@ var path = require('path');
var async = require('async'); var async = require('async');
var generateSymbols = require('./generate-symbols');
var build = path.join(__dirname, '..', 'build'); var build = path.join(__dirname, '..', 'build');
var symbols = require('../build/symbols.json').symbols;
var lookup = {}; /**
symbols.forEach(function(symbol) { * Get a list of patterns from the config file. If configPath is provided
lookup[symbol.name] = symbol; * it is assumed to be a JSON file with an 'exports' member that is a list
}); * of symbol names or patterns.
*
* @param {string} configPath Path to config file.
* @param {function(Error, Array.<string>)} callback Called with the list of
* patterns.
*/
function getPatterns(configPath, callback) {
if (configPath) {
fs.readFile(configPath, function(err, data) {
if (err) {
callback(err);
return;
}
var obj;
try {
obj = JSON.parse(String(data));
} catch (err) {
callback(new Error('Trouble parsing file as JSON: ' + options.config));
return;
}
var patterns = obj.exports;
if (patterns && !Array.isArray(patterns)) {
callback(new Error('Expected an exports array, got: ' + patterns));
return;
}
callback(null, patterns);
});
} else {
process.nextTick(function() {
callback(null, ['*']);
});
}
}
/**
* Read the symbols file.
* @param {Array.<string>} patterns List of patterns to pass along.
* @param {funciton(Error, Array.<string>, Array.<Object>)} callback Called
* with the patterns and symbols (or any error).
*/
function getSymbols(patterns, callback) {
generateSymbols(function(err) {
if (err) {
callback(new Error('Trouble generating symbols: ' + err.message));
return;
}
var symbols = require('../build/symbols.json').symbols;
callback(null, patterns, symbols);
});
}
/** /**
@@ -20,12 +72,18 @@ symbols.forEach(function(symbol) {
* *
* @param {Array.<string>} patterns A list of symbol names to match. Wildcards * @param {Array.<string>} patterns A list of symbol names to match. Wildcards
* at the end of a string will match multiple names. * at the end of a string will match multiple names.
* @param {Array.<Object>} symbols List of symbols.
* @param {function(Error, Array.<string>)} callback Called with the filtered * @param {function(Error, Array.<string>)} callback Called with the filtered
* list of symbol names (or any error). * list of symbol names (or any error).
*/ */
function filterSymbols(patterns, callback) { function filterSymbols(patterns, symbols, callback) {
var matches = []; var matches = [];
var lookup = {};
symbols.forEach(function(symbol) {
lookup[symbol.name] = symbol;
});
patterns.forEach(function(name) { patterns.forEach(function(name) {
var match = false; var match = false;
var pattern = (name.substr(-1) === '*'); var pattern = (name.substr(-1) === '*');
@@ -54,43 +112,6 @@ function filterSymbols(patterns, callback) {
} }
/**
* Get a list of patterns from the options arg. If options.config is provided
* it is assumed to be a JSON file with an 'exports' member that is a list
* of symbol names or patterns.
*
* @param {Object} options Options.
* @param {function(Error, Array.<string>)} callback Callback.
*/
function getPatterns(options, callback) {
if (options.config) {
fs.readFile(options.config, function(err, data) {
if (err) {
callback(err);
return;
}
var obj;
try {
obj = JSON.parse(String(data));
} catch (err) {
callback(new Error('Trouble parsing file as JSON: ' + options.config));
return;
}
var patterns = obj.exports;
if (patterns && !Array.isArray(patterns)) {
callback(new Error('Expected an exports array, got: ' + patterns));
return;
}
callback(null, patterns);
});
} else {
process.nextTick(function() {
callback(null, ['*']);
});
}
}
/** /**
* Generate goog code to export a named symbol. * Generate goog code to export a named symbol.
* @param {string} name Symbol name. * @param {string} name Symbol name.
@@ -120,35 +141,30 @@ function formatPropertyExport(name) {
/** /**
* Generate export code given a list of patterns that match symbol names. * Generate export code given a list symbol names.
* @param {Array.<string>} patterns List of patterns. * @param {Array.<string>} names List of symbol names.
* @param {function(Error, string)} callback Callback called with export code * @return {string} Export code.
* (or any error).
*/ */
function generateExports(patterns, callback) { function generateExports(names) {
filterSymbols(patterns, function(err, symbols) { var blocks = [];
if (err) { names.forEach(function(name) {
return callback(err); if (name.indexOf('#') > 0) {
blocks.push(formatPropertyExport(name));
} else {
blocks.push(formatSymbolExport(name));
} }
var blocks = [];
symbols.forEach(function(name) {
if (name.indexOf('#') > 0) {
blocks.push(formatPropertyExport(name));
} else {
blocks.push(formatSymbolExport(name));
}
});
callback(null, blocks.join('\n'));
}); });
return blocks.join('\n');
} }
/** /**
* Write the build/exports.js file. * Write the build/exports.js file.
* @param {string} code Exports code. * @param {Array.<string>} names List of symbol names.
* @param {function(Error)} callback Callback. * @param {function(Error)} callback Callback.
*/ */
function writeExports(code, callback) { function writeExports(names, callback) {
var code = generateExports(names);
fs.writeFile(path.join(build, 'exports.js'), code, callback); fs.writeFile(path.join(build, 'exports.js'), code, callback);
} }
@@ -158,29 +174,42 @@ function writeExports(code, callback) {
* it is assumed to be a path to a JSON file with an 'exports' member whose * it is assumed to be a path to a JSON file with an 'exports' member whose
* value is an array of symbol names or patterns. * value is an array of symbol names or patterns.
* *
* @param {Object} options Options. * @param {Array.<string>} patterns List of symbol names or patterns.
* @param {function(Error)} callback Callback. * @param {function(Error)} callback Callback.
*/ */
exports.main = function(options, callback) { function main(patterns, callback) {
async.waterfall([ async.waterfall([
getPatterns.bind(null, options), getSymbols.bind(null, patterns),
generateExports, filterSymbols,
writeExports writeExports
], callback); ], callback);
}; }
/**
* If running this module directly, read the config file and call the main
* function.
*/
if (require.main === module) { if (require.main === module) {
var options = { var configPath = process.argv[2];
config: process.argv[2] getPatterns(configPath, function(err, patterns) {
};
exports.main(options, function(err) {
if (err) { if (err) {
console.error(err.message); console.error(err.message);
process.exit(1); process.exit(1);
} else {
process.exit(0);
} }
main(patterns, function(err) {
if (err) {
console.error(err.message);
process.exit(1);
} else {
process.exit(0);
}
});
}); });
} }
/**
* Export main function.
*/
module.exports = main;
+13 -3
View File
@@ -197,18 +197,22 @@ function writeSymbols(symbols, output, callback) {
* @param {function(Error)} callback Called when the symbols file has been * @param {function(Error)} callback Called when the symbols file has been
* written (or if an error occurs). * written (or if an error occurs).
*/ */
exports.main = function(callback) { function main(callback) {
async.waterfall([ async.waterfall([
readSymbols, readSymbols,
getNewer, getNewer,
spawnJSDoc, spawnJSDoc,
writeSymbols writeSymbols
], callback); ], callback);
}; }
/**
* If running this module directly, read the config file and call the main
* function.
*/
if (require.main === module) { if (require.main === module) {
exports.main(function(err) { main(function(err) {
if (err) { if (err) {
console.error(err.message); console.error(err.message);
process.exit(1); process.exit(1);
@@ -217,3 +221,9 @@ if (require.main === module) {
} }
}); });
} }
/**
* Export main function.
*/
module.exports = main;