-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJakefile
More file actions
executable file
·302 lines (259 loc) · 7.83 KB
/
Jakefile
File metadata and controls
executable file
·302 lines (259 loc) · 7.83 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var path = require('path'),
fs = require('fs'),
mingler = require('mingler'),
existsSync = 'existsSync' in fs ? fs.existsSync : path.existsSync,
colors;
try {
colors = require('colors');
} catch(e) {
// Nothing to do...
}
// Default Settings
var settings = {
minify: false,
silent: false,
colors: true,
font: false,
standalone: false,
noparsers: false
};
// Log colors if the npm module colors is available
var logcolors = {
info: 'cyan',
warn: 'yellow',
error: 'red'
};
// Constants
var kBuildDir = 'build/';
var kSourceDir = 'src/';
var kMainFile = 'subito.js';
var kBuildFilename = 'subito.js';
var kBuildFontFilename = 'subito.font.js';
var kBuildMinFilename = 'subito.min.js';
var kBuildMinFontFilename = 'subito.font.min.js';
// Utility function which respects the 'silent' setting
function log(text, type, acolor, nolabel) {
type = (type in console && typeof console[type] == 'function') ? type : 'info';
if(!settings.silent) {
if(!nolabel) {
text = ' ['+type.toUpperCase()+'] ' + text;
} else {
text = ' ' + text;
}
if(settings.colors && colors) {
color = logcolors[acolor || type] || 'grey';
console[type](text[color]);
} else {
console[type](text);
}
}
}
// Utility function which collects files in directory recursively
// Inspired by jshint's source
function collect(filepath, files, ignore) {
if(ignore && filepath.search(ignore) != -1) {
return false;
}
if(fs.statSync(filepath).isDirectory()) {
fs.readdirSync(filepath).forEach(function(f) {
collect(path.join(filepath, f), files, ignore);
});
} else if(filepath.match(/\.js$/)) {
files.push(filepath)
}
}
// Default task - build
desc('Default both lints and builds the project');
task({'default': ['external', 'lint', 'build']}, function(parameters) {
// Nothing to do here...
});
// External task, builds teoria
desc('Build the external resources (Teoria)');
task('external', {async: true}, function() {
var curdir = process.cwd();
process.chdir('external/teoria');
if (!existsSync('README.md')) {
process.chdir(curdir);
return fail('You need to init and update the submodules. Check the README');
}
if (!existsSync('teoria.js')) {
log('Building externals...');
jake.exec('jake build', function() {
log('Successfully built external resources');
process.chdir(curdir);
complete();
}, {printStdout: true, printStderr: true});
} else {
process.chdir(curdir);
complete();
}
});
// Builds the files required for running the examples
desc('Builds the files required by the examples');
task({'examples': ['external']}, function() {
// Build the subito.font.min.js
var buildTask = jake.Task['build'], active, i, tasks;
tasks = {
'minified font file': {
args: ['font', 'minify'],
run: false
},
'minified file': {
args: ['minify'],
run: false
}
};
function taskCallback() {
console.log('');
// Reset settings
if(active) {
tasks[active].args.forEach(function(arg) {
settings[arg] = !settings[arg];
});
}
for(i in tasks) {
if(!tasks[i].run) {
active = i;
log('Building ' + i);
tasks[i].run = true;
buildTask.reenable();
buildTask.invoke.apply(buildTask, tasks[i].args);
return;
}
}
complete();
}
buildTask.addListener('complete', taskCallback);
taskCallback();
}, {async: true});
// Building, which means concatenating and
// optionally minifying all source files.
desc('Concatenates all files into build/subito.js');
task({'build': ['external']}, function() {
var params, exists, concatenation, filename;
filename = kBuildFilename;
// List settings
params = Array.prototype.slice.call(arguments);
params.forEach(function(el) {
if(el in settings) {
settings[el] = !settings[el];
} else {
log('Ignoring invalid settings: ' + el, 'warn');
}
});
log('Building Subito');
// Ensure build directory exists
exists = existsSync(kBuildDir);
if(!exists) {
log('Creating build directory');
fs.mkdirSync(kBuildDir, 0777);
}
// Concatenate files
process.chdir(kSourceDir);
// When concatenation is completed
mingler.on('complete', function(concatenation) {
// Should the source be minified?
if(settings.minify) {
var ugly, ast, ratio, compressed;
try {
ugly = require('uglify-js');
} catch(e) {
return log('uglify-js module doesn\'t appear to be installed. Use:'
+ '`npm install uglify-js`', 'error');
}
log('Minifying');
ast = ugly.parser.parse(concatenation);
ast = ugly.uglify.ast_mangle(ast);
ast = ugly.uglify.ast_squeeze(ast);
compressed = ugly.uglify.gen_code(ast);
ratio = 100 - (compressed.length/concatenation.length) * 100;
ratio = ratio.toString().substr(0, 4);
log('Saved ' + ratio + '% of the original size');
concatenation = compressed;
filename = (settings.font) ? kBuildMinFontFilename : kBuildMinFilename;
} else if(settings.font) {
filename = kBuildFontFilename;
}
// Write to file and close!
log('Writing to output file \'' + filename + '\'');
fs.writeFileSync(kBuildDir + filename, concatenation, 'utf8');
log('Concatenation completed - Goodbye!');
// Clear listeners
mingler.un('complete');
mingler.un('error');
mingler.un('warning');
mingler.un('concatenate');
complete();
});
mingler.on('error', function(error) {
log(error, 'error');
process.exit(1);
});
mingler.on('warning', function(warning) {
log(warning, 'warn');
});
mingler.on('concatenate', function(feedback) {
if(!settings.font && feedback.filename.indexOf('fonts/') == 0) {
feedback.discard();
log('Ignoring font file ' + feedback.filename, 'info', 'grey');
} else if(!settings.standalone && feedback.filename == 'standalone.js') {
feedback.discard();
log('Ignoring standalone helper ' + feedback.filename, 'info', 'grey');
} else if(settings.noparsers && feedback.filename.indexOf('parsers/') == 0) {
feedback.discard();
log('Ignoring parser file ' + feedback.filename, 'info', 'grey');
} else {
log("Concatenating: " + feedback.filename, 'info', 'grey');
}
});
mingler.mingle(kMainFile, function(concatenation) {
process.chdir('../');
});
}, {async: true});
// Lints the files according to .jshintrc
desc('Lint all files according to coding standards');
task('lint', function() {
var jshint;
try {
jshint = require('jshint');
} catch(e) {
log('jshint doesn\'t appear to be installed. Do a `npm install -g jshint`',
'error');
return false;
}
// Load configuration
var config = fs.readFileSync('./.jshintrc', 'utf8');
config = JSON.parse(config);
// List all files in src/
var files = [], errors = [], errorfilecount = 0;
collect('src/', files, /fonts\/|fonts\\.*/);
files.forEach(function(file) {
var content, results;
try {
content = fs.readFileSync(file, 'utf8');
} catch(e) {
log('Unable to open file ' + file, 'error');
}
content = content.replace(/^\uFEFF/, ''); // Remove Unicode BOM
if(!jshint.JSHINT(content, config)) {
errorfilecount++;
jshint.JSHINT.errors.forEach(function(error) {
if(error) {
errors.push({file: file, error: error});
}
});
}
});
log(files.length.toString() + ' files linted, ' + (files.length-errorfilecount)
+ ' successfully, ' + errorfilecount + ' with errors!', 'info');
errors.forEach(function(error) {
log(error.file + ': line ' + error.error.line
+ ', col ' + error.error.character + ', ' + error.error.reason,
'error', 'grey', true);
});
if(errors.length == 0) {
log('Lint passed', 'info');
} else {
log('Lint failed!', 'error');
}
});