1 |
|
2 | (function() {
|
3 | var Config, RuleBook, createLocalMakefileInc, debug, flatten, fs, getFilename, path, writeToFile;
|
4 |
|
5 | path = require('path');
|
6 |
|
7 | fs = require('fs');
|
8 |
|
9 | debug = require('debug')('create-makefile');
|
10 |
|
11 | Config = require('./config');
|
12 |
|
13 | RuleBook = require('./rulebook');
|
14 |
|
15 | module.exports.createMakefiles = function(input, output) {
|
16 | var CustomConfig, customConfig, err, featurePath, lakeConfig, manifest, manifestPath, projectRoot, _i, _len;
|
17 | projectRoot = Config.projectRoot();
|
18 | lakeConfig = Config.config();
|
19 | if (output == null) {
|
20 | output = path.join(lakeConfig.config.lakeOutput);
|
21 | }
|
22 | CustomConfig = function(featurePath) {
|
23 | this.featurePath = featurePath;
|
24 | return this.projectRoot = projectRoot;
|
25 | };
|
26 | CustomConfig.prototype = lakeConfig.config;
|
27 | process.stdout.write("Generating Makefiles");
|
28 | for (_i = 0, _len = input.length; _i < _len; _i++) {
|
29 | featurePath = input[_i];
|
30 | manifest = null;
|
31 | try {
|
32 | manifestPath = path.join(projectRoot, featurePath, 'Manifest');
|
33 | manifest = require(manifestPath);
|
34 | } catch (_error) {
|
35 | err = _error;
|
36 | err.message = "Error in Manifest " + featurePath + ": " + err.message;
|
37 | debug(err.message);
|
38 | return err;
|
39 | }
|
40 | customConfig = new CustomConfig(featurePath);
|
41 | createLocalMakefileInc(lakeConfig.rules, customConfig, manifest, output);
|
42 | process.stdout.write(".");
|
43 | }
|
44 | console.log("");
|
45 | return null;
|
46 | };
|
47 |
|
48 | createLocalMakefileInc = function(ruleFiles, config, manifest, output) {
|
49 | var mkFilePath, ruleBook, ruleFile, ruleFilePath, rules, _i, _len;
|
50 | ruleBook = new RuleBook();
|
51 | for (_i = 0, _len = ruleFiles.length; _i < _len; _i++) {
|
52 | ruleFile = ruleFiles[_i];
|
53 | ruleFilePath = path.join(config.projectRoot, ruleFile);
|
54 | rules = require(ruleFilePath);
|
55 | rules.addRules(config, manifest, ruleBook);
|
56 | }
|
57 | ruleBook.close();
|
58 | mkFilePath = getFilename(config.projectRoot, config.featurePath, output);
|
59 | return writeToFile(mkFilePath, ruleBook);
|
60 | };
|
61 |
|
62 | getFilename = function(projectRoot, featurePath, output) {
|
63 | var featureName, mkFilePath;
|
64 | featureName = path.basename(featurePath);
|
65 | mkFilePath = path.join(path.resolve(projectRoot, output), featureName + '.mk');
|
66 | return mkFilePath;
|
67 | };
|
68 |
|
69 | flatten = function(array, result) {
|
70 | var x, _i, _len;
|
71 | if (result == null) {
|
72 | result = [];
|
73 | }
|
74 | for (_i = 0, _len = array.length; _i < _len; _i++) {
|
75 | x = array[_i];
|
76 | if (Array.isArray(x)) {
|
77 | flatten(x, result);
|
78 | } else {
|
79 | result.push(x);
|
80 | }
|
81 | }
|
82 | return result;
|
83 | };
|
84 |
|
85 | writeToFile = function(filename, ruleBook) {
|
86 | var actions, contents, prop, rule, _i, _j, _len, _len1, _ref, _ref1;
|
87 | contents = "";
|
88 | _ref = ruleBook.getRules();
|
89 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
90 | rule = _ref[_i];
|
91 | rule.dependencies || (rule.dependencies = []);
|
92 | _ref1 = ['targets', 'dependencies', 'actions'];
|
93 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
94 | prop = _ref1[_j];
|
95 | if (rule[prop] != null) {
|
96 | rule[prop] = flatten([rule[prop]]);
|
97 | }
|
98 | }
|
99 | if (rule.targets != null) {
|
100 | contents += ("" + (rule.targets.join(' ')) + ": ") + ("" + (rule.dependencies.join(' ')) + "\n");
|
101 | if (rule.actions != null) {
|
102 | actions = ['@echo ""', "@echo \"\u001b[3;4m" + rule.targets + "\u001b[24m\""];
|
103 | actions = actions.concat(rule.actions);
|
104 | contents += "\t" + (actions.join('\n\t')) + "\n\n";
|
105 | } else {
|
106 | contents += '\n';
|
107 | }
|
108 | }
|
109 | }
|
110 | return fs.writeFileSync(filename, contents);
|
111 | };
|
112 |
|
113 | }).call(this);
|