Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / build / build-tests.js
1 if (system.args.length !== 1) {
2     system.print('Usage:');
3     system.print('  hammerjs build-test.js');
4     system.exit(-1);
5 }
6
7 // Traverses the specified path and collects all *.js files.
8 // Note: the traversal is recursive to all subdirectories.
9
10 var scanDirectory = function (path) {
11     var entries = [],
12         subdirs;
13     if (fs.exists(path) && fs.isFile(path) && path.match('.js$')) {
14         entries.push(path);
15     } else if (fs.isDirectory(path)) {
16         fs.list(path).forEach(function (e) {
17             subdirs = scanDirectory(path + '/' + e);
18             subdirs.forEach(function (s) {
19                 entries.push(s);
20             });
21         });
22     }
23     return entries;
24 };
25
26 var getFileContent = function(fname) {
27     var f = fs.open(fname, 'r'),
28         content = '', 
29         line;
30     while (true) {
31         line = f.readLine();
32         if (line.length === 0) {
33             break;
34         }
35         content += line;
36     }
37     f.close();
38     return content;
39 };
40
41 var writeFile = function(fname, content) {
42     var f = fs.open(fname, 'w'),
43         lines = content.split('\n'),
44         length = lines.length,
45         i = 0;
46
47     for (; i < length; i++) {
48         f.writeLine(lines[i]);
49     }
50     f.close();
51 };
52
53 // Loads the content of a json file and return an object.
54 var json2js = function (fname) {
55     var content = getFileContent(fname);
56     return eval('(' + content + ')');
57 };
58
59 var generateIndexFor = function(config) {
60     var packages = json2js(config.sources).packages,
61         tpl = getFileContent(config.tpl),
62         sources = '',
63         specs, slength, j, specPaths, source, name, package, files, file, i, length;
64     
65     tpl = tpl.replace('_TITLE_', config.title);
66     
67     for (name in packages) {
68         package = packages[name];
69         if (config.packages.indexOf(package.name) != -1) {
70             files = package.files;
71             length = files.length;
72             
73             for (i = 0; i < length; i++) {
74                 file = files[i];
75                 source = file.path + file.name;
76                 if (config.srcMustMatch) {
77                     if (!source.match(config.srcMustMatch)) {
78                         continue;
79                     }
80                 }
81                 if (config.excludes.indexOf(source) === -1) {
82                     sources +=  (config.prepare[source] || '') + '{ type: "js", src: "' + source.replace(config.srcReplace[0], config.srcReplace[1]) + '"},\n' + (config.overrides[source] || '');
83                 }
84             }
85         }
86     }
87     
88     specPaths = config.specPaths; 
89     length = specPaths.length;
90     for (i = 0; i < length; i++) {
91         specs = scanDirectory(specPaths[i]);
92         slength = specs.length;
93         for (j = 0; j < slength; j++) {
94             source = specs[j];
95
96             if (config.excludes.indexOf(source) === -1) {
97                 sources += '{ type: "js", src: "' +  source.replace(config.srcReplace[0], config.srcReplace[1]) + '"}'+ (i === length - 1 && j === slength - 1 ? ']' : ',') + '\n';
98             }
99         }
100         
101     }
102     tpl = tpl.replace('_INCLUDES_', sources);
103     writeFile(config.destination, tpl);
104 };
105
106 var indexes = json2js('tests/indexes.json'),
107     ilength = indexes.length,
108     x = 0;
109     
110 for (; x < ilength; x++) {
111     system.print('Generating test index for ' + indexes[x].title);
112     generateIndexFor(indexes[x]);
113 }
114
115 system.exit(0);