-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
153 lines (134 loc) · 4.02 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var fs = require('fs');
var path = require('path');
var colors = require('colors');
var index = require('./index.js');
var compiler = require('./compiler.js');
var errors = require('./errors.js');
function Builder(modulePath, options, version) {
this.modulePath = modulePath;
this.options = options;
this.version = version;
this.spillers = [];
}
Builder.prototype.build = function() {
if (!this.buildModules(this.modulePath, [], this.options, false, this.version)) {
return false;
}
}
/// Sets the spiller that is used by the compiler to generate output.
Builder.prototype.addSpiller = function(name, spiller) {
this.spillers.push({spiller: spiller, name: name});
};
/// Returns the spiller used by the compiler to generate output.
Builder.prototype.getSpiller = function(name) {
for(var i = 0; i < this.spillers.length; i++) {
if (this.spillers[i].name === name) {
return this.spillers[i].spiller;
}
}
return null;
};
Builder.prototype.buildModules = function(modulePath, spillerPath, options, inTest, version) {
var index = this.spillers.length;
if (!this.buildModules2(modulePath, spillerPath, options, inTest, version)) {
return false;
}
while(index < this.spillers.length) {
var s = this.spillers.pop();
s.spiller.spill();
}
return true;
};
Builder.prototype.buildModules2 = function(modulePath, spillerPath, options, inTest, version) {
// console.log("Building", modulePath, "...");
// Try to read package.json
var pkg;
try {
if (!inTest) {
pkg = JSON.parse(fs.readFileSync(path.join(modulePath, "package.json"), 'utf8'));
}
} catch(e) {
}
// Is there something to compile? Documentation or code?
var comp;
if (pkg && pkg.gismo && (pkg.gismo.uniqueId || (options.doc && pkg.gismo.docModule))) {
var opt = { };
for(var key in options) {
opt[key] = options[key];
}
// Generate doc only but no code?
if (pkg.gismo.docModule && !pkg.gismo.uniqueId) {
opt.nocode = true;
}
try {
comp = new compiler.Compiler(this, modulePath, opt, version);
} catch(err) {
index.displayError(err);
return false;
}
}
if (comp && comp.isModuleMode()) {
for(var i = 0; i < this.spillers.length; i++) {
this.spillers[i].spiller.addModule(spillerPath, comp, pkg)
}
}
if (pkg && pkg.gismo && pkg.gismo.buildOrder) {
for(var i = 0; i < pkg.gismo.buildOrder.length; i++) {
if (!this.buildModules(path.join(modulePath, pkg.gismo.buildOrder[i]), spillerPath.concat(pkg.gismo.buildOrder[i]), options, false, version)) {
return false;
}
}
} else {
try {
var filenames = fs.readdirSync(modulePath).sort();
} catch(e) {
console.log(("Directory " + modulePath + " does not exist. Check package.json on the parent directory").blue);
return false;
}
for(var i = 0; i < filenames.length; i++) {
var fname = filenames[i];
if (fname === ".DS_Store") {
continue;
}
var info = fs.statSync(path.join(modulePath, fname));
if (info.isSymbolicLink()) {
continue;
}
if (pkg && pkg.gismo && info.isDirectory() && fname === "test") {
if (!this.buildModules(path.join(modulePath, fname), spillerPath.concat(fname), options, true, version)) {
return false;
}
} else if (info.isDirectory()) {
if (!this.buildModules(path.join(modulePath, fname), spillerPath.concat(fname), options, false, version)) {
return false;
}
}
}
}
if (comp) {
console.log("Compile ", modulePath);
// index.compileModule(modulePath, options);
try {
comp.compileModule();
} catch(err) {
index.displayError(err);
return false;
}
}
return true;
}
Builder.prototype.throwError = function(token, messageFormat) {
var error,
args = Array.prototype.slice.call(arguments, 2),
msg = messageFormat.replace(
/%(\d)/g,
function (whole, index) {
if (index >= args.length)
throw new Error('Implementation Error: Message reference must be in range');
return args[index];
}
);
var error = new errors.CompilerError(msg);
throw error;
}
exports.Builder = Builder;