-
-
Notifications
You must be signed in to change notification settings - Fork 907
/
index.js
executable file
·737 lines (643 loc) · 25 KB
/
index.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
var path = require('path'),
fs = require('fs'),
wrench = require('wrench'),
vm = require('vm'),
uglifyjs = require('uglify-js'),
jsonlint = require('jsonlint'),
sourceMapper = require('./sourceMapper'),
styler = require('./styler'),
_ = require("../../lib/alloy/underscore")._,
logger = require('../../common/logger'),
CompilerMakeFile = require('./CompilerMakeFile'),
U = require('../../utils'),
CU = require('./compilerUtils'),
CONST = require('../../common/constants'),
platforms = require('../../../platforms/index');
var alloyRoot = path.join(__dirname,'..','..'),
viewRegex = new RegExp('\\.' + CONST.FILE_EXT.VIEW + '$'),
controllerRegex = new RegExp('\\.' + CONST.FILE_EXT.CONTROLLER + '$'),
modelRegex = new RegExp('\\.' + CONST.FILE_EXT.MODEL + '$'),
compileConfig = {},
buildPlatform,
theme;
var times = {
first: null,
last: null,
msgs: []
};
function tiSdkVersionNumber(tiVersion) {
var parts = tiVersion.split && tiVersion.split('.');
return parts[0]*100 + parts[1]*10 + parts[2]*1; // *1 is to cast it to an integer
}
//////////////////////////////////////
////////// command function //////////
//////////////////////////////////////
module.exports = function(args, program) {
BENCHMARK();
var alloyConfig = {},
compilerMakeFile,
paths = U.getAndValidateProjectPaths(
program.outputPath || args[0] || process.cwd()
);
// Parse the tiapp.xml and make sure the sdk-version is at least 3.0.0
var tiVersion = U.tiapp.getTitaniumSdkVersion(U.tiapp.parse(paths.project));
if (tiVersion === null) {
logger.warn('Unable to determine Titanium SDK version from tiapp.xml.');
logger.warn('Your app may have unexpected behavior. Make sure your tiapp.xml is valid.');
} else if (tiSdkVersionNumber(tiVersion) < tiSdkVersionNumber(CONST.MINIMUM_TI_SDK)) {
logger.error('Alloy 1.0.0+ requires Titanium SDK ' + CONST.MINIMUM_TI_SDK + ' or higher.');
logger.error('Version "' + tiVersion + '" was found in the "sdk-version" field of your tiapp.xml.');
logger.error('If you are building with the old titanium.py script and are specifying an SDK version ');
logger.error('as a CLI argument that is different than the one in your tiapp.xml, please change the');
logger.error('version in your tiapp.xml file. ');
process.exit(1);
}
// construct compiler config from command line config parameters
// and print the configuration data
logger.debug('----- CONFIGURATION -----');
if (program.config && _.isString(program.config)) {
logger.debug('raw config = "' + program.config + '"');
_.each(program.config.split(','), function(v) {
var parts = v.split('=');
alloyConfig[parts[0]] = parts[1];
logger.debug(parts[0] + ' = ' + parts[1]);
});
}
if (!alloyConfig.deploytype) {
alloyConfig.deploytype = 'development';
logger.debug('deploytype = ' + alloyConfig.deploytype);
}
logger.debug('project path = ' + paths.project);
logger.debug('app path = ' + paths.app);
logger.debug('');
// make sure a platform was specified
buildPlatform = alloyConfig.platform;
if (!buildPlatform) {
U.die([
'You must define a target platform for the alloy compile command',
' Ex. "alloy compile --config platform=ios"'
]);
}
// wipe the controllers, models, and widgets
logger.debug('----- CLEANING RESOURCES -----');
logger.debug('Cleaning "Resources/alloy/' + CONST.DIR.COMPONENT + '" folder...');
U.rmdirContents(path.join(paths.resourcesAlloy,CONST.DIR.COMPONENT), ['BaseController.js']);
logger.debug('Cleaning "Resources/alloy/' + CONST.DIR.MODEL + '" folder...');
U.rmdirContents(path.join(paths.resourcesAlloy,CONST.DIR.MODEL));
logger.debug('Cleaning "Resources/alloy/' + CONST.DIR.WIDGET + '" folder...');
U.rmdirContents(path.join(paths.resourcesAlloy,CONST.DIR.WIDGET));
logger.debug(' ');
// GET RID OF ORPHAN FILES
U.deleteOrphanFiles(
paths.resources,
[
path.join(alloyRoot,'lib'),
path.join(paths.app,CONST.DIR.ASSETS),
path.join(paths.app,CONST.DIR.LIB),
path.join(paths.app,'vendor'),
],
{
exceptions: [
// still need to check for builtins
path.join('alloy'),
path.join('alloy','CFG.js'),
path.join('alloy','controllers'),
path.join('alloy','widgets'),
path.join('alloy','models')
],
platform: platforms[buildPlatform].titaniumFolder
}
);
// create compile config from paths and various alloy config files
logger.debug('----- CONFIG.JSON -----');
compileConfig = CU.createCompileConfig(paths.app, paths.project, alloyConfig);
// identify current theme, if any
(theme = compileConfig.theme) && logger.debug('theme = ' + theme);
logger.debug('');
// create generated controllers folder in resources
logger.debug('----- BASE RUNTIME FILES -----');
U.installPlugin(path.join(alloyRoot,'..'), paths.project);
// copy in all lib resources from alloy module
U.updateFiles(path.join(alloyRoot, 'lib'), paths.resources);
// create runtime folder structure for alloy
_.each(['COMPONENT','WIDGET','RUNTIME_STYLE'], function(type) {
var p = path.join(paths.resourcesAlloy, CONST.DIR[type]);
wrench.mkdirSyncRecursive(p, 0777);
});
// Copy in all developer assets, libs, and additional resources
_.each(['ASSETS','LIB','VENDOR'], function(type) {
U.updateFiles(path.join(paths.app,CONST.DIR[type]), paths.resources);
});
// copy in test specs if not in production
if (alloyConfig.deploytype !== 'production') {
U.updateFiles(path.join(paths.app,'specs'), path.join(paths.resources,'specs'));
}
logger.debug('');
// check theme for assets
if (theme) {
var themeAssetsPath = path.join(paths.app,'themes',theme,'assets');
if (path.existsSync(themeAssetsPath)) {
wrench.copyDirSyncRecursive(themeAssetsPath, paths.resources, {preserve:true});
}
}
logger.debug('');
// process project makefiles
compilerMakeFile = new CompilerMakeFile();
var alloyJMK = path.resolve(path.join(paths.app,"alloy.jmk"));
if (path.existsSync(alloyJMK)) {
logger.debug('Loading "alloy.jmk" compiler hooks...');
var script = vm.createScript(fs.readFileSync(alloyJMK), 'alloy.jmk');
// process alloy.jmk compile file
try {
script.runInNewContext(compilerMakeFile);
compilerMakeFile.isActive = true;
} catch(e) {
logger.error(e.stack);
U.die('Project build at "' + alloyJMK + '" generated an error during load.');
}
compilerMakeFile.trigger("pre:compile",_.clone(compileConfig));
logger.debug('');
}
// TODO: https://jira.appcelerator.org/browse/ALOY-477
if (buildPlatform === 'android') {
U.tiapp.upStackSizeForRhino(paths.project);
}
logger.info('----- MVC GENERATION -----');
// create the global style, if it exists
styler.setPlatform(buildPlatform);
styler.loadGlobalStyles(paths.app, theme ? {theme:theme} : {});
// Create collection of all widget and app paths
var widgetDirs = U.getWidgetDirectories(paths.project, paths.app);
var viewCollection = widgetDirs;
viewCollection.push({ dir: path.join(paths.project,CONST.ALLOY_DIR) });
// Process all models
var models = processModels(viewCollection);
// Create a regex for determining which platform-specific
// folders should be used in the compile process
var filteredPlatforms = _.reject(CONST.PLATFORM_FOLDERS_ALLOY, function(p) { return p === buildPlatform; });
filteredPlatforms = _.map(filteredPlatforms, function(p) { return p + '[\\\\\\/]'; });
var filterRegex = new RegExp('^(?:(?!' + filteredPlatforms.join('|') + '))');
// Process all views/controllers and generate their runtime
// commonjs modules and source maps.
var tracker = {};
_.each(viewCollection, function(collection) {
// generate runtime controllers from views
_.each(wrench.readdirSyncRecursive(path.join(collection.dir,CONST.DIR.VIEW)), function(view) {
if (viewRegex.test(view) && filterRegex.test(view)) {
// make sure this controller is only generated once
var fp = path.join(collection.dir,view.substring(0,view.lastIndexOf('.')));
if (tracker[fp]) { return; }
// generate runtime controller
logger.info('[' + view + '] ' + (collection.manifest ? collection.manifest.id + ' ' : '') + 'view processing...');
parseAlloyComponent(view, collection.dir, collection.manifest);
tracker[fp] = true;
}
});
// generate runtime controllers from any controller code that has no
// corresponding view markup
_.each(wrench.readdirSyncRecursive(path.join(collection.dir,CONST.DIR.CONTROLLER)), function(controller) {
if (controllerRegex.test(controller) && filterRegex.test(controller)) {
// make sure this controller is only generated once
var fp = path.join(collection.dir,controller.substring(0,controller.lastIndexOf('.')));
if (tracker[fp]) { return; }
// generate runtime controller
logger.info('[' + controller + '] ' + (collection.manifest ? collection.manifest.id + ' ' : '') + 'controller processing...');
parseAlloyComponent(controller, collection.dir, collection.manifest, true);
tracker[fp] = true;
}
});
});
logger.info('');
// generate app.js
logger.info('[app.js] Titanium entry point processing...');
var appJS = path.join(compileConfig.dir.resources,"app.js");
sourceMapper.generateCodeAndSourceMap({
target: {
filename: 'Resources/app.js',
filepath: appJS,
template: path.join(alloyRoot,'template','app.js')
},
data: {
'__MAPMARKER_ALLOY_JS__': {
filename: 'app/alloy.js',
filepath: path.join(paths.app,'alloy.js')
}
}
}, compileConfig);
logger.info('');
// optimize code
logger.info('----- OPTIMIZING -----');
optimizeCompiledCode(alloyConfig, paths);
// trigger our custom compiler makefile
if (compilerMakeFile.isActive) {
compilerMakeFile.trigger("post:compile",_.clone(compileConfig));
}
BENCHMARK('TOTAL', true);
};
///////////////////////////////////////
////////// private functions //////////
///////////////////////////////////////
function parseAlloyComponent(view,dir,manifest,noView) {
var parseType = noView ? 'controller' : 'view';
// validate parameters
if (!view) { U.die('Undefined ' + parseType + ' passed to parseAlloyComponent()'); }
if (!dir) { U.die('Failed to parse ' + parseType + ' "' + view + '", no directory given'); }
var dirRegex = new RegExp('^(?:' + CONST.PLATFORM_FOLDERS_ALLOY.join('|') + ')[\\\\\\/]*');
var basename = path.basename(view, '.' + CONST.FILE_EXT[parseType.toUpperCase()]);
dirname = path.dirname(view).replace(dirRegex,''), // /^(?:android|ios|mobileweb)[\\\/]*/,''),
viewName = basename,
template = {
viewCode: '',
modelVariable: CONST.BIND_MODEL_VAR,
parentVariable: CONST.PARENT_SYMBOL_VAR,
itemTemplateVariable: CONST.ITEM_TEMPLATE_VAR,
preCode: '',
postCode: '',
Widget: !manifest ? '' : "var " + CONST.WIDGET_OBJECT + " = new (require('alloy/widget'))('" + manifest.id + "');",
WPATH: !manifest ? '' : _.template(fs.readFileSync(path.join(alloyRoot,'template','wpath.js'),'utf8'),{WIDGETID:manifest.id}),
__MAPMARKER_CONTROLLER_CODE__: '',
},
widgetDir = dirname ? path.join(CONST.DIR.COMPONENT,dirname) : CONST.DIR.COMPONENT,
widgetStyleDir = dirname ? path.join(CONST.DIR.RUNTIME_STYLE,dirname) : CONST.DIR.RUNTIME_STYLE,
state = { parent: {}, styles: [] },
files = {};
// reset the bindings map
styler.bindingsMap = {};
CU.destroyCode = '';
CU.postCode = '';
CU.currentManifest = manifest;
// create a list of file paths
searchPaths = noView ? ['CONTROLLER'] : ['VIEW','STYLE','CONTROLLER'];
_.each(searchPaths, function(fileType) {
// get the path values for the file
var fileTypeRoot = path.join(dir,CONST.DIR[fileType]);
var filename = viewName+'.'+CONST.FILE_EXT[fileType];
var filepath = dirname ? path.join(dirname,filename) : filename;
// check for platform-specific versions of the file
var baseFile = path.join(fileTypeRoot,filepath);
if (buildPlatform) {
var platformSpecificFile = path.join(fileTypeRoot,buildPlatform,filepath);
if (path.existsSync(platformSpecificFile)) {
if (fileType === 'STYLE') {
files[fileType] = [
{ file:baseFile },
{ file:platformSpecificFile, platform:true }
];
} else {
files[fileType] = platformSpecificFile;
}
return;
}
}
files[fileType] = baseFile;
});
_.each(['COMPONENT','RUNTIME_STYLE'], function(fileType) {
files[fileType] = path.join(compileConfig.dir.resourcesAlloy,CONST.DIR[fileType]);
if (dirname) { files[fileType] = path.join(files[fileType],dirname); }
files[fileType] = path.join(files[fileType],viewName+'.js');
});
// we are processing a view, not just a controller
if (!noView) {
// validate view
if (!path.existsSync(files.VIEW)) {
logger.warn('No ' + CONST.FILE_EXT.VIEW + ' view file found for view ' + files.VIEW);
return;
}
// load global style, if present
//state.styles = compileConfig && compileConfig.globalStyle ? compileConfig.globalStyle : [];
state.styles = styler.globalStyle || [];
// Load the style and update the state
if (files.STYLE) {
var theStyles = _.isArray(files.STYLE) ? files.STYLE : [{file:files.STYLE}];
_.each(theStyles, function(style) {
if (fs.existsSync(style.file)) {
logger.info(' style: "' +
path.relative(path.join(dir,CONST.DIR.STYLE),style.file) + '"');
state.styles = styler.loadAndSortStyle(style.file, {
existingStyle: state.styles,
platform: style.platform
});
}
});
}
if (theme && !manifest) {
var themeStylesDir = path.join(compileConfig.dir.themes,theme,'styles');
var theStyle = dirname ? path.join(dirname,viewName+'.tss') : viewName+'.tss';
var themeStylesFile = path.join(themeStylesDir,theStyle);
var psThemeStylesFile = path.join(themeStylesDir,buildPlatform,theStyle);
if (path.existsSync(themeStylesFile)) {
logger.info(' theme: "' + path.join(theme.toUpperCase(),theStyle) + '"');
state.styles = styler.loadAndSortStyle(themeStylesFile, {
existingStyle: state.styles,
theme: true
});
}
if (path.existsSync(psThemeStylesFile)) {
logger.info(' theme: "' + path.join(theme.toUpperCase(),buildPlatform,theStyle) + '"');
state.styles = styler.loadAndSortStyle(psThemeStylesFile, {
existingStyle: state.styles,
platform: true,
theme: true
});
}
}
// Load view from file into an XML document root node
try {
logger.info(' view: "' + path.relative(path.join(dir,CONST.DIR.VIEW),files.VIEW)+ '"');
var docRoot = U.XML.getAlloyFromFile(files.VIEW);
} catch (e) {
U.die([
e.stack,
'Error parsing XML for view "' + view + '"'
]);
}
// make sure we have a Window, TabGroup, or SplitWindow
var rootChildren = U.XML.getElementsFromNodes(docRoot.childNodes);
if (viewName === 'index') {
valid = [
'Ti.UI.Window',
'Ti.UI.iPad.SplitWindow',
'Ti.UI.TabGroup',
].concat(CONST.MODEL_ELEMENTS);
_.each(rootChildren, function(node) {
var found = true;
var args = CU.getParserArgs(node, {}, { doSetId: false });
if (args.fullname === 'Alloy.Require') {
var inspect = CU.inspectRequireNode(node);
for (var j = 0; j < inspect.names.length; j++) {
if (!_.contains(valid, inspect.names[j])) {
found = false;
break;
}
}
} else {
found = _.contains(valid, args.fullname);
}
if (!found) {
U.die([
'Compile failed. index.xml must have a top-level container element.',
'Valid elements: [' + valid.join(',') + ']'
]);
}
});
}
// process any model/collection nodes
_.each(rootChildren, function(node, i) {
var fullname = CU.getNodeFullname(node);
var isModelElement = _.contains(CONST.MODEL_ELEMENTS,fullname);
if (isModelElement) {
var vCode = CU.generateNode(node, state, undefined, false, true);
template.viewCode += vCode.content;
template.preCode += vCode.pre;
// remove the model/collection nodes when done
docRoot.removeChild(node);
}
});
// rebuild the children list since model elements have been removed
rootChildren = U.XML.getElementsFromNodes(docRoot.childNodes);
// process the UI nodes
var assignedDefaultId = false;
_.each(rootChildren, function(node, i) {
var defaultId = undefined;
var fullname = CU.getNodeFullname(node);
if (!assignedDefaultId) {
assignedDefaultId = true;
defaultId = viewName;
}
template.viewCode += CU.generateNode(node, {parent:{},styles:state.styles}, defaultId, true);
});
}
// process the controller code
if (path.existsSync(files.CONTROLLER)) {
logger.info(' controller: "' + path.relative(path.join(dir,CONST.DIR.CONTROLLER),files.CONTROLLER) + '"');
}
var cCode = CU.loadController(files.CONTROLLER);
template.parentController = (cCode.parentControllerName != '') ? cCode.parentControllerName : "'BaseController'";
template.__MAPMARKER_CONTROLLER_CODE__ += cCode.controller;
template.preCode += cCode.pre;
// process the bindingsMap, if it contains any data bindings
var bTemplate = "$.<%= id %>.<%= prop %>=_.isFunction(<%= model %>.transform)?";
bTemplate += "<%= model %>.transform()['<%= attr %>']:<%= model %>.get('<%= attr %>');";
// for each model variable in the bindings map...
_.each(styler.bindingsMap, function(mapping,modelVar) {
// open the model binding handler
var handlerVar = CU.generateUniqueId();
template.viewCode += 'var ' + handlerVar + '=function() {';
CU.destroyCode += modelVar + ".off('" + CONST.MODEL_BINDING_EVENTS + "'," + handlerVar + ");";
// for each specific conditional within the bindings map....
_.each(_.groupBy(mapping, function(b) { return b.condition; }), function(bindings,condition) {
var bCode = '';
// for each binding belonging to this model/conditional pair...
_.each(bindings, function(binding) {
bCode += _.template(bTemplate, {
id: binding.id,
prop: binding.prop,
model: modelVar,
attr: binding.attr
});
});
// if this is a legit conditional, wrap the binding code in it
if (typeof condition !== 'undefined' && condition !== 'undefined') {
bCode = 'if(' + condition + '){' + bCode + '}';
}
template.viewCode += bCode;
});
template.viewCode += "};";
template.viewCode += modelVar + ".on('" + CONST.MODEL_BINDING_EVENTS + "'," + handlerVar + ");";
});
// add destroy() function to view for cleaning up bindings
template.viewCode += 'exports.destroy=function(){' + CU.destroyCode + '};';
// add any postCode after the controller code
template.postCode += CU.postCode;
// create generated controller module code for this view/controller or widget
var controllerCode = template.__MAPMARKER_CONTROLLER_CODE__;
delete template.__MAPMARKER_CONTROLLER_CODE__;
var code = _.template(fs.readFileSync(path.join(compileConfig.dir.template, 'component.js'), 'utf8'), template);
// prep the controller paths based on whether it's an app
// controller or widget controller
var targetFilepath = files.COMPONENT;
var runtimeStylePath = files.RUNTIME_STYLE;
if (manifest) {
wrench.mkdirSyncRecursive(path.join(compileConfig.dir.resourcesAlloy, CONST.DIR.WIDGET, manifest.id, widgetDir), 0777);
wrench.mkdirSyncRecursive(path.join(compileConfig.dir.resourcesAlloy, CONST.DIR.WIDGET, manifest.id, widgetStyleDir), 0777);
CU.copyWidgetResources(
[path.join(dir,CONST.DIR.ASSETS), path.join(dir,CONST.DIR.LIB)],
compileConfig.dir.resources,
manifest.id
);
targetFilepath = path.join(compileConfig.dir.resourcesAlloy, CONST.DIR.WIDGET, manifest.id, widgetDir, viewName + '.js');
runtimeStylePath = path.join(compileConfig.dir.resourcesAlloy, CONST.DIR.WIDGET, manifest.id, widgetStyleDir, viewName + '.js');
}
// generate the code and source map for the current controller
sourceMapper.generateCodeAndSourceMap({
target: {
filename: path.relative(compileConfig.dir.project,files.COMPONENT),
filepath: targetFilepath,
templateContent: code
},
data: {
__MAPMARKER_CONTROLLER_CODE__: {
filename: path.relative(compileConfig.dir.project,files.CONTROLLER),
fileContent: controllerCode
}
}
}, compileConfig);
// write the compiled style array to a runtime module
var relativeStylePath = path.relative(compileConfig.dir.project,runtimeStylePath);
logger.info(' created: "' + relativeStylePath + '"');
wrench.mkdirSyncRecursive(path.dirname(runtimeStylePath), 0777);
fs.writeFileSync(
runtimeStylePath,
'module.exports = ' + JSON.stringify(state.styles)
);
}
function findModelMigrations(name, inDir) {
try {
var migrationsDir = inDir || compileConfig.dir.migrations;
var files = fs.readdirSync(migrationsDir);
var part = '_'+name+'.'+CONST.FILE_EXT.MIGRATION;
// look for our model
files = _.reject(files,function(f) { return f.indexOf(part)==-1});
// sort them in the oldest order first
files = files.sort(function(a,b){
var x = a.substring(0,a.length - part.length -1);
var y = b.substring(0,b.length - part.length -1);
if (x<y) return -1;
if (x>y) return 1;
return 0;
});
var codes = [];
_.each(files,function(f) {
var mf = path.join(migrationsDir,f);
var m = fs.readFileSync(mf,'utf8');
var code = "(function(migration){\n "+
"migration.name = '" + name + "';\n" +
"migration.id = '" + f.substring(0,f.length-part.length).replace(/_/g,'') + "';\n" +
m +
"})";
codes.push(code);
});
logger.info("Found " + codes.length + " migrations for model: " + name);
return codes;
} catch(E) {
return [];
}
}
function processModels(dirs) {
var models = [];
var modelTemplateFile = path.join(alloyRoot,'template','model.js');
_.each(dirs, function(dirObj) {
var modelDir = path.join(dirObj.dir,CONST.DIR.MODEL);
if (!fs.existsSync(modelDir)) {
return;
}
var migrationDir = path.join(dirObj.dir,CONST.DIR.MIGRATION);
var manifest = dirObj.manifest;
var isWidget = typeof manifest !== 'undefined' && manifest !== null;
var pathPrefix = isWidget ? 'widgets/' + manifest.id + '/': '';
_.each(fs.readdirSync(modelDir), function(file) {
if (!modelRegex.test(file)) {
logger.warn('Non-model file "' + file + '" in ' + pathPrefix + 'models directory');
return;
}
logger.info('[' + pathPrefix + 'models/' + file + '] model processing...');
var fullpath = path.join(modelDir,file);
var basename = path.basename(fullpath, '.'+CONST.FILE_EXT.MODEL);
// generate model code based on model.js template and migrations
var code = _.template(fs.readFileSync(modelTemplateFile,'utf8'), {
basename: basename,
modelJs: fs.readFileSync(fullpath,'utf8'),
migrations: findModelMigrations(basename, migrationDir)
});
// write the model to the runtime file
var casedBasename = U.properCase(basename);
var modelRuntimeDir = path.join(compileConfig.dir.resourcesAlloy,'models');
if (isWidget) {
modelRuntimeDir = path.join(compileConfig.dir.resourcesAlloy,'widgets',manifest.id,'models');
}
wrench.mkdirSyncRecursive(modelRuntimeDir, 0777);
fs.writeFileSync(path.join(modelRuntimeDir,casedBasename+'.js'), code);
models.push(casedBasename);
});
});
return models;
};
function optimizeCompiledCode() {
var mods = [
'builtins',
'optimizer',
'compress'
],
modLocation = './ast/',
lastFiles = [],
files;
// Get the list of JS files from the Resources directory
// and exclude files that don't need to be optimized, or
// have already been optimized.
function getJsFiles() {
var exceptions = [
'app.js',
'alloy/CFG.js',
'alloy/controllers/',
'alloy/styles/',
'alloy/backbone.js',
'alloy/underscore.js'
];
return _.filter(wrench.readdirSyncRecursive(compileConfig.dir.resources), function(f) {
return /\.js\s*$/.test(f) && !_.find(exceptions, function(e) {
return f.indexOf(e) === 0;
});
});
}
while((files = _.difference(getJsFiles(),lastFiles)).length > 0) {
_.each(files, function(file) {
// generate AST from file
var fullpath = path.join(compileConfig.dir.resources,file);
logger.info('- ' + file);
try {
var ast = uglifyjs.parse(fs.readFileSync(fullpath,'utf8'), {
filename: file
});
} catch (e) {
U.die('Error generating AST for "' + fullpath + '"', e);
}
// process all AST operations
_.each(mods, function(mod) {
logger.trace(' processing "' + mod + '" module...');
ast.figure_out_scope();
ast = require(modLocation+mod).process(ast, compileConfig) || ast;
});
// Write out the optimized file
var stream = uglifyjs.OutputStream(sourceMapper.OPTIONS_OUTPUT);
ast.print(stream);
fs.writeFileSync(fullpath, stream.toString());
});
// Combine lastFiles and files, so on the next iteration we can make sure that the
// list of files to be processed has not grown, like in the case of builtins.
lastFiles = _.union(lastFiles, files);
}
}
function BENCHMARK(desc, isFinished) {
var places = Math.pow(10,5);
desc || (desc = '<no description>');
if (times.first === null) {
times.first = process.hrtime();
return;
}
function hrtimeInSeconds(t) {
return t[0] + (t[1] / 1000000000);
}
var total = process.hrtime(times.first);
var current = hrtimeInSeconds(total) - (times.last ? hrtimeInSeconds(times.last) : 0);
times.last = total;
var thisTime = Math.round((isFinished ? hrtimeInSeconds(total) : current)*places)/places;
times.msgs.push('[' + thisTime + 's] ' + desc);
if (isFinished) {
logger.trace(' ');
logger.trace('Benchmarking');
logger.trace('------------');
logger.trace(times.msgs);
logger.info('');
logger.info('Alloy compiled in ' + thisTime + 's');
}
}