diff --git a/build.py b/build.py index b43f85b273..a97279c1a0 100755 --- a/build.py +++ b/build.py @@ -218,12 +218,8 @@ def build_ol_all_js(t): t.output('%(JAVA)s', '-server', '-XX:+TieredCompilation', '-jar', 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', 'tasks/generate-exports.js', 'build/symbols.json') +@target('build/exports.js', SRC) def build_exports_js(t): t.run('node', 'tasks/generate-exports.js') diff --git a/tasks/generate-exports.js b/tasks/generate-exports.js index c8de7b8c68..7134f3b1a8 100644 --- a/tasks/generate-exports.js +++ b/tasks/generate-exports.js @@ -3,13 +3,65 @@ var path = require('path'); var async = require('async'); +var generateSymbols = require('./generate-symbols'); + var build = path.join(__dirname, '..', 'build'); -var symbols = require('../build/symbols.json').symbols; -var lookup = {}; -symbols.forEach(function(symbol) { - lookup[symbol.name] = symbol; -}); + +/** + * Get a list of patterns from the config file. If configPath is provided + * 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.)} 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.} patterns List of patterns to pass along. + * @param {funciton(Error, Array., Array.)} 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.} patterns A list of symbol names to match. Wildcards * at the end of a string will match multiple names. + * @param {Array.} symbols List of symbols. * @param {function(Error, Array.)} callback Called with the filtered * list of symbol names (or any error). */ -function filterSymbols(patterns, callback) { +function filterSymbols(patterns, symbols, callback) { var matches = []; + var lookup = {}; + symbols.forEach(function(symbol) { + lookup[symbol.name] = symbol; + }); + patterns.forEach(function(name) { var match = false; 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.)} 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. * @param {string} name Symbol name. @@ -120,35 +141,30 @@ function formatPropertyExport(name) { /** - * Generate export code given a list of patterns that match symbol names. - * @param {Array.} patterns List of patterns. - * @param {function(Error, string)} callback Callback called with export code - * (or any error). + * Generate export code given a list symbol names. + * @param {Array.} names List of symbol names. + * @return {string} Export code. */ -function generateExports(patterns, callback) { - filterSymbols(patterns, function(err, symbols) { - if (err) { - return callback(err); +function generateExports(names) { + var blocks = []; + names.forEach(function(name) { + 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. - * @param {string} code Exports code. + * @param {Array.} names List of symbol names. * @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); } @@ -158,29 +174,42 @@ function writeExports(code, callback) { * 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. * - * @param {Object} options Options. + * @param {Array.} patterns List of symbol names or patterns. * @param {function(Error)} callback Callback. */ -exports.main = function(options, callback) { +function main(patterns, callback) { async.waterfall([ - getPatterns.bind(null, options), - generateExports, + getSymbols.bind(null, patterns), + filterSymbols, writeExports ], callback); -}; +} +/** + * If running this module directly, read the config file and call the main + * function. + */ if (require.main === module) { - var options = { - config: process.argv[2] - }; - - exports.main(options, function(err) { + var configPath = process.argv[2]; + getPatterns(configPath, function(err, patterns) { if (err) { console.error(err.message); 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; diff --git a/tasks/generate-symbols.js b/tasks/generate-symbols.js index 057a80ab91..ef7847187f 100644 --- a/tasks/generate-symbols.js +++ b/tasks/generate-symbols.js @@ -197,18 +197,22 @@ function writeSymbols(symbols, output, callback) { * @param {function(Error)} callback Called when the symbols file has been * written (or if an error occurs). */ -exports.main = function(callback) { +function main(callback) { async.waterfall([ readSymbols, getNewer, spawnJSDoc, writeSymbols ], callback); -}; +} +/** + * If running this module directly, read the config file and call the main + * function. + */ if (require.main === module) { - exports.main(function(err) { + main(function(err) { if (err) { console.error(err.message); process.exit(1); @@ -217,3 +221,9 @@ if (require.main === module) { } }); } + + +/** + * Export main function. + */ +module.exports = main;