1 |
|
2 | (function() {
|
3 | var config, edgeMapping, factories, findCommand, findLocalCommand, findScript, fp, getCoffeelintPaths, getOptionString, getOptions, glob, globule, ld, log, makeAssetEdges, makeCommonRules, makeFingerprintEdge, makeLintEdges, makeNinja, makeSimpleRule, makeSourceEdges, makeSystemEdges, ninjaBuilder, packageJson, path, simpleMapOpt, warnMessage, _ref;
|
4 |
|
5 | packageJson = require('../package.json');
|
6 |
|
7 | ld = require('lodash');
|
8 |
|
9 | path = require('path');
|
10 |
|
11 | glob = require('glob');
|
12 |
|
13 | globule = require('globule');
|
14 |
|
15 | log = require('yadsil')('benbria-configure-ninja');
|
16 |
|
17 | ninjaBuilder = require('ninja-build-gen');
|
18 |
|
19 | factories = require('./ninjaFactories');
|
20 |
|
21 | _ref = require('./ninjaCommands'), findCommand = _ref.findCommand, findLocalCommand = _ref.findLocalCommand, findScript = _ref.findScript;
|
22 |
|
23 | getCoffeelintPaths = require('./index').getCoffeelintPaths;
|
24 |
|
25 | log.warn = log.warning;
|
26 |
|
27 | config = {};
|
28 |
|
29 | config.ninjaFilePath = process.cwd();
|
30 |
|
31 | config.ninjaFile = "build.ninja";
|
32 |
|
33 | config.configureNinjaScript = __filename;
|
34 |
|
35 | config.streamlineVersion = 10;
|
36 |
|
37 | config.streamlineOpts = "";
|
38 |
|
39 | config.stylusOpts = "";
|
40 |
|
41 | fp = {};
|
42 |
|
43 | fp.assets = 'assets';
|
44 |
|
45 | fp.build = 'build';
|
46 |
|
47 | fp.buildAssets = "" + fp.build + "/" + fp.assets;
|
48 |
|
49 | fp.coffeelint = "" + fp.build + "/coffeelint";
|
50 |
|
51 | fp.fingerprintFile = "" + fp.buildAssets + "/release/fingerprints.json";
|
52 |
|
53 | warnMessage = "# Auto-generated by `loop-configure-ninja`.\n";
|
54 |
|
55 | makeSystemEdges = function(ninja, optionString, options) {
|
56 | ninja.assign('buildCoffee', findLocalCommand('coffee', config));
|
57 | ninja.assign('node', 'node');
|
58 | factories.forActiveFactory(config, log, function(factory) {
|
59 | if (factory.assignments) {
|
60 | log.debug("Generating assignments for " + factory.name);
|
61 | return factory.assignments(ninja, config);
|
62 | }
|
63 | });
|
64 | ninja.assign('uglifyjs', findLocalCommand("uglifyjs", config));
|
65 | ninja.rule('configure').run("" + (findCommand('loop-configure-ninja', config)) + optionString).description('CONFIGURE');
|
66 | return ninja.edge(config.ninjaFile).using('configure').need([findCommand('loop-configure-ninja', config)]);
|
67 | };
|
68 |
|
69 | makeSimpleRule = function(ninja, _arg) {
|
70 | var command, desc, display, name;
|
71 | name = _arg.name, command = _arg.command, display = _arg.display;
|
72 | desc = name.toUpperCase() + ' ' + (display != null ? display : '$in');
|
73 | return ninja.rule(name).run(command).description(desc);
|
74 | };
|
75 |
|
76 | makeCommonRules = function(ninja, options) {
|
77 | factories.forActiveFactory(config, log, function(factory) {
|
78 | if (factory.makeRules) {
|
79 | log.debug("Making rules for " + factory.name);
|
80 | return factory.makeRules(ninja, config);
|
81 | }
|
82 | });
|
83 | makeSimpleRule(ninja, {
|
84 | name: 'copy',
|
85 | command: 'cp $in $out'
|
86 | });
|
87 | return makeSimpleRule(ninja, {
|
88 | name: 'fingerprint',
|
89 | command: "$node " + (findScript("fingerprint.js", config)) + " $cliOptions -b $basePath -o $out $in",
|
90 | display: '$basePath'
|
91 | });
|
92 | };
|
93 |
|
94 | edgeMapping = function(ninja, files, mappingOptions, callback) {
|
95 | return ld.map(globule.mapping(files, mappingOptions), function(match) {
|
96 | callback(ninja.edge(match.dest).from(match.src));
|
97 | return match.dest;
|
98 | });
|
99 | };
|
100 |
|
101 | simpleMapOpt = function(srcBase, destBase, ext) {
|
102 | return {
|
103 | srcBase: srcBase,
|
104 | destBase: destBase,
|
105 | ext: ext,
|
106 | extDot: 'last'
|
107 | };
|
108 | };
|
109 |
|
110 | makeLintEdges = function(ninja, coffeeFiles) {
|
111 | var options;
|
112 | options = simpleMapOpt('.', '$builddir/coffeelint', '.coffeelint');
|
113 | return edgeMapping(ninja, coffeeFiles, options, function(edge) {
|
114 | return edge.using('coffeelint');
|
115 | });
|
116 | };
|
117 |
|
118 | makeAssetEdges = function(ninja) {
|
119 | var assetPaths, configName, configNames, fingerprintFile, _i, _len;
|
120 | assetPaths = {};
|
121 | configNames = ['debug', 'release'];
|
122 | for (_i = 0, _len = configNames.length; _i < _len; _i++) {
|
123 | configName = configNames[_i];
|
124 | log.debug("Making " + configName + " asset edges");
|
125 | assetPaths[configName] = [];
|
126 | factories.forActiveFactory(config, log, function(factory) {
|
127 | var edges, mappingOptions, match, sourceFileNames, _j, _len1, _ref1, _results;
|
128 | if (factory.assetFiles && factory.makeAssetEdge) {
|
129 | log.debug(" " + factory.name);
|
130 | mappingOptions = simpleMapOpt(fp.assets, path.join(fp.buildAssets, configName), factory.targetExt || '.js');
|
131 | sourceFileNames = globule.find(factory.assetFiles, mappingOptions);
|
132 | _ref1 = globule.mapping(sourceFileNames, mappingOptions);
|
133 | _results = [];
|
134 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
135 | match = _ref1[_j];
|
136 | edges = factory.makeAssetEdge(ninja, match.src, match.dest, configName);
|
137 | _results.push(assetPaths[configName] = assetPaths[configName].concat(edges));
|
138 | }
|
139 | return _results;
|
140 | }
|
141 | });
|
142 | }
|
143 | ninja.edge('debug-assets').from(assetPaths.debug);
|
144 | if (assetPaths.release.length > 0) {
|
145 | log.debug("Making fingerprint edge for " + assetPaths.release.length + " release assets");
|
146 | fingerprintFile = makeFingerprintEdge(ninja, assetPaths.release);
|
147 | return ninja.edge('release-assets').from(fingerprintFile);
|
148 | } else {
|
149 | return ninja.edge('release-assets');
|
150 | }
|
151 | };
|
152 |
|
153 | makeSourceEdges = function(ninja) {
|
154 | var destFiles;
|
155 | destFiles = [];
|
156 | log.debug("Making src edges");
|
157 | factories.forActiveFactory(config, log, function(factory) {
|
158 | var edges, mappingOptions, match, sourceFileNames, _i, _len, _ref1, _results;
|
159 | if (factory.files && factory.makeSrcEdge) {
|
160 | log.debug(" " + factory.name);
|
161 | mappingOptions = simpleMapOpt('src', 'lib', factory.targetExt || '.js');
|
162 | sourceFileNames = globule.find(factory.files, mappingOptions);
|
163 | _ref1 = globule.mapping(sourceFileNames, mappingOptions);
|
164 | _results = [];
|
165 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
166 | match = _ref1[_i];
|
167 | edges = factory.makeSrcEdge(ninja, match.src, match.dest);
|
168 | _results.push(destFiles = destFiles.concat(edges));
|
169 | }
|
170 | return _results;
|
171 | }
|
172 | });
|
173 | return ninja.edge('lib').from(destFiles);
|
174 | };
|
175 |
|
176 | makeFingerprintEdge = function(ninja, assetsEdges) {
|
177 | ninja.edge(fp.fingerprintFile).using('fingerprint').from(assetsEdges).assign('basePath', "" + fp.buildAssets + "/release");
|
178 | return fp.fingerprintFile;
|
179 | };
|
180 |
|
181 | makeNinja = function(options, done) {
|
182 | var files, ninja;
|
183 | ninja = ninjaBuilder('1.3', 'build');
|
184 | factories.forEachFactory(function(factory) {
|
185 | return typeof factory.initialize === "function" ? factory.initialize(ninja, config, log) : void 0;
|
186 | });
|
187 | ninja.header(warnMessage);
|
188 | if (options.ninjaColor) {
|
189 | ninja.assign('cliOptions', '--color');
|
190 | }
|
191 | makeSystemEdges(ninja, getOptionString(options), options);
|
192 | makeCommonRules(ninja, options);
|
193 | if (!options.noLint) {
|
194 | files = getCoffeelintPaths();
|
195 | ninja.edge('lint').from(makeLintEdges(ninja, files));
|
196 | } else {
|
197 | ninja.edge('lint');
|
198 | }
|
199 | makeAssetEdges(ninja);
|
200 | makeSourceEdges(ninja);
|
201 | ninja.edge('debug').from(['debug-assets', 'lib']);
|
202 | ninja.edge('release').from(['release-assets', 'lib']);
|
203 | ninja.edge('all').from(['debug-assets', 'release-assets', 'lib']);
|
204 | ninja.byDefault('all');
|
205 | return done(null, ninja);
|
206 | };
|
207 |
|
208 | getOptionString = function(options) {
|
209 | var str;
|
210 | str = '';
|
211 | if (options.noLint) {
|
212 | str += ' --no-lint';
|
213 | }
|
214 | if (options.ninjaColor) {
|
215 | str += ' --ninja-color';
|
216 | }
|
217 | if (options.streamline8) {
|
218 | str += ' --streamline8';
|
219 | }
|
220 | if (options.streamlineOpts) {
|
221 | str += " --streamline-opts '" + options.streamlineOpts + "'";
|
222 | }
|
223 | if (options.stylusOpts) {
|
224 | str += " --stylus-opts '" + options.stylusOpts + "'";
|
225 | }
|
226 | if (options.require) {
|
227 | str += " --require '" + options.require + "'";
|
228 | }
|
229 | return str;
|
230 | };
|
231 |
|
232 | getOptions = function() {
|
233 | var ArgumentParser, options, parser;
|
234 | ArgumentParser = require('argparse').ArgumentParser;
|
235 | parser = new ArgumentParser({
|
236 | version: packageJson.version,
|
237 | addHelp: true,
|
238 | description: "Generates a ninja.build file for a coffeescript project."
|
239 | });
|
240 | parser.addArgument(['--require'], {
|
241 | help: "Comma delimited list of modules before building.",
|
242 | nargs: "?"
|
243 | });
|
244 | parser.addArgument(['--verbose'], {
|
245 | help: "Verbose output",
|
246 | nargs: 0
|
247 | });
|
248 | parser.addArgument(['--no-lint'], {
|
249 | help: "Disable coffee linting.",
|
250 | dest: "noLint",
|
251 | nargs: 0
|
252 | });
|
253 | parser.addArgument(['--color'], {
|
254 | help: "Force color display out of a TTY.",
|
255 | nargs: 0
|
256 | });
|
257 | parser.addArgument(['--ninja-color'], {
|
258 | help: "Force color on ninja scripts.",
|
259 | dest: "ninjaColor",
|
260 | nargs: 0
|
261 | });
|
262 | parser.addArgument(['--streamline8'], {
|
263 | help: "Use streamline 0.8.x command line arguments (instead of 0.10.x)",
|
264 | nargs: 0
|
265 | });
|
266 | parser.addArgument(['--streamline-opts'], {
|
267 | help: "Extra options for streamline compilers",
|
268 | metavar: "opts",
|
269 | dest: "streamlineOpts",
|
270 | defaultValue: ''
|
271 | });
|
272 | parser.addArgument(['--stylus-opts'], {
|
273 | help: "Extra options for stylus",
|
274 | metavar: "opts",
|
275 | dest: "stylusOpts",
|
276 | defaultValue: ''
|
277 | });
|
278 | options = parser.parseArgs(process.argv.slice(2));
|
279 | if (options.ninjaColor) {
|
280 | options.color = true;
|
281 | }
|
282 | log.verbose(options.verbose);
|
283 | if (options.color) {
|
284 | log.color(options.color);
|
285 | }
|
286 | if (log.color()) {
|
287 | options.ninjaColor = true;
|
288 | }
|
289 | return options;
|
290 | };
|
291 |
|
292 | module.exports = function(configureNinjaScript) {
|
293 | var mod, options, _i, _len, _ref1;
|
294 | config.configureNinjaScript = configureNinjaScript;
|
295 | options = getOptions();
|
296 | if (options.streamline8) {
|
297 | config.streamlineVersion = 8;
|
298 | }
|
299 | config.streamlineOpts = options.streamlineOpts;
|
300 | config.stylusOpts = options.stylusOpts;
|
301 | if (options.require) {
|
302 | _ref1 = options.require.split(',');
|
303 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
304 | mod = _ref1[_i];
|
305 | if (mod.indexOf('./') === 0) {
|
306 | mod = path.resolve(mod);
|
307 | }
|
308 | require(mod);
|
309 | }
|
310 | }
|
311 | return makeNinja(options, function(err, ninja) {
|
312 | var ninjaFile;
|
313 | if (err) {
|
314 | throw err;
|
315 | }
|
316 | ninjaFile = path.resolve(config.ninjaFilePath, config.ninjaFile);
|
317 | ninja.save(ninjaFile);
|
318 | return log.info("generated \'" + ninjaFile + "\' (" + ninja.ruleCount + " rules, " + ninja.edgeCount + " edges)");
|
319 | });
|
320 | };
|
321 |
|
322 | }).call(this);
|
323 |
|
324 |
|