|
| 1 | +require('../shelljs/make'); |
| 2 | +var fs = require('fs'), |
| 3 | + path = require('path'), |
| 4 | + vm = require('vm'); |
| 5 | + |
| 6 | +/** |
| 7 | + * A simple preprocessor that is based on the firefox preprocessor |
| 8 | + * see (https://developer.mozilla.org/en/Build/Text_Preprocessor). The main |
| 9 | + * difference is that this supports a subset of the commands and it supports |
| 10 | + * preproccesor commands in html style comments. |
| 11 | + * Currently Supported commands: |
| 12 | + * - if |
| 13 | + * - else |
| 14 | + * - endif |
| 15 | + * - include |
| 16 | + * - expand |
| 17 | + */ |
| 18 | +function preprocess(inFilename, outFilename, defines) { |
| 19 | + // TODO make this really read line by line. |
| 20 | + var lines = fs.readFileSync(inFilename).toString().split('\n'); |
| 21 | + var totalLines = lines.length; |
| 22 | + var out = ''; |
| 23 | + var i = 0; |
| 24 | + function readLine() { |
| 25 | + if (i < totalLines) { |
| 26 | + return lines[i++]; |
| 27 | + } |
| 28 | + return null; |
| 29 | + } |
| 30 | + var writeLine = typeof outFilename === 'function' ? outFilename : function(line) { |
| 31 | + out += line + '\n'; |
| 32 | + } |
| 33 | + function include(file) { |
| 34 | + var realPath = fs.realpathSync(inFilename); |
| 35 | + var dir = path.dirname(realPath); |
| 36 | + preprocess(path.join(dir, file), writeLine, defines); |
| 37 | + } |
| 38 | + function expand(line) { |
| 39 | + line = line.replace(/__[\w]+__/g, function(variable) { |
| 40 | + variable = variable.substring(2, variable.length - 2); |
| 41 | + if (variable in defines) { |
| 42 | + return defines[variable]; |
| 43 | + } |
| 44 | + return ''; |
| 45 | + }); |
| 46 | + writeLine(line); |
| 47 | + } |
| 48 | + |
| 49 | + var s, state = 0, stack = []; |
| 50 | + var control = /^(?:\/\/|<!--)\s*#(if|else|endif|expand|include)(?:\s+(.*?)(?:-->)?$)?/; |
| 51 | + var lineNumber = 0; |
| 52 | + while ((s = readLine()) !== null) { |
| 53 | + ++lineNumber; |
| 54 | + var m = control.exec(s); |
| 55 | + if (m) { |
| 56 | + switch (m[1]) { |
| 57 | + case 'if': |
| 58 | + stack.push(state); |
| 59 | + try { |
| 60 | + state = vm.runInNewContext(m[2], defines) ? 3 : 1; |
| 61 | + } catch (e) { |
| 62 | + console.error('Could not evalute line \'' + m[2] + '\' at ' + |
| 63 | + fs.realpathSync(inFilename) + ':' + lineNumber); |
| 64 | + throw e; |
| 65 | + } |
| 66 | + break; |
| 67 | + case 'else': |
| 68 | + state = state === 1 ? 3 : 2; |
| 69 | + break; |
| 70 | + case 'endif': |
| 71 | + state = stack.pop(); |
| 72 | + break; |
| 73 | + case 'expand': |
| 74 | + if (state === 0 || state === 3) |
| 75 | + expand(m[2]); |
| 76 | + break; |
| 77 | + case 'include': |
| 78 | + if (state === 0 || state === 3) |
| 79 | + include(m[2]); |
| 80 | + break; |
| 81 | + } |
| 82 | + } else { |
| 83 | + if (state === 0) { |
| 84 | + writeLine(s); |
| 85 | + } else if(state === 3) { |
| 86 | + writeLine(s.replace(/^\/\/|^<!--|-->/g, " ")); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + if (state !== 0 || stack.length !== 0) |
| 91 | + throw new Error('Missing endif in preprocessor.'); |
| 92 | + if (typeof outFilename !== 'function') |
| 93 | + fs.writeFileSync(outFilename, out); |
| 94 | +} |
| 95 | +exports.preprocess = preprocess; |
| 96 | + |
| 97 | +/** |
| 98 | + * Simplifies common build steps. |
| 99 | + * @param setup |
| 100 | + * .defines defines for preprocessors |
| 101 | + * .copy array of arrays of source and destination pairs of files to copy |
| 102 | + * .preprocess array of arrays of source and destination pairs of files |
| 103 | + * run through preprocessor |
| 104 | + */ |
| 105 | +function build(setup) { |
| 106 | + var defines = setup.defines; |
| 107 | + |
| 108 | + setup.copy.forEach(function(option) { |
| 109 | + var source = option[0]; |
| 110 | + var destination = option[1]; |
| 111 | + cp('-R', source, destination); |
| 112 | + }); |
| 113 | + |
| 114 | + setup.preprocess.forEach(function(option) { |
| 115 | + var sources = option[0]; |
| 116 | + var destination = option[1]; |
| 117 | + |
| 118 | + sources = ls('-R', sources); |
| 119 | + sources.forEach(function(source) { |
| 120 | + // ??? Warn if the source is wildcard and dest is file? |
| 121 | + var destWithFolder = destination; |
| 122 | + if (test('-d', destination)) |
| 123 | + destWithFolder += '/' + path.basename(source); |
| 124 | + preprocess(source, destWithFolder, defines); |
| 125 | + }); |
| 126 | + }); |
| 127 | +} |
| 128 | +exports.build = build; |
| 129 | + |
| 130 | +/** |
| 131 | + * Merge two defines arrays. Values in the second param will override values in |
| 132 | + * the first. |
| 133 | + */ |
| 134 | +function merge(defaults, defines) { |
| 135 | + var ret = {}; |
| 136 | + for (var key in defaults) |
| 137 | + ret[key] = defaults[key]; |
| 138 | + for (key in defines) |
| 139 | + ret[key] = defines[key]; |
| 140 | + return ret; |
| 141 | +} |
| 142 | +exports.merge = merge; |
0 commit comments