UNPKG

3.72 kBJavaScriptView Raw
1// ==========================================================================
2// Project: Blossom - Modern, Cross-Platform Application Framework
3// Copyright: ©2012 Fohr Motion Picture Studios. All rights reserved.
4// License: Licensed under the MIT license (see BUILDTOOLS-LICENSE).
5// ==========================================================================
6/*globals global require __dirname */
7
8var fs = require('fs'),
9 path = require('path'),
10 Graph = require('./utils/graph'); // needed for topological sorting
11
12// First we grab the files in foundation.
13var sourceFiles = [],
14 sourceTree = path.join(__dirname, "../foundation");
15
16function processDirectory(dirname) {
17 var files = fs.readdirSync(dirname);
18 files.forEach(function(filename) {
19 if (filename === "node") return;
20 var sourcePath = path.join(dirname, filename);
21 var stat = fs.statSync(sourcePath);
22 if (stat.isFile()) {
23 sourceFiles.push(sourcePath);
24 } else if (stat.isDirectory()) {
25 processDirectory(sourcePath);
26 } else {
27 console.log("the file is something strange");
28 }
29 });
30}
31
32processDirectory(sourceTree);
33
34// Next, we figure out the right order to require those files.
35var javascriptFiles = sourceFiles.filter(function(sourcePath) {
36 // console.log(sourcePath, sourcePath.slice(0, -3), sourcePath.slice(0, -3) === '.js');
37 if (sourcePath.slice(0,4) === "test") return false;
38 else if (sourcePath.slice(0,4) === "node") return false;
39 else if (sourcePath.slice(0,4) === "protocols") return false;
40 else if (sourcePath.match(/test_suite/)) return false;
41 else return sourcePath.slice(-3) === '.js';
42});
43
44var g = new Graph();
45var map = {};
46
47function scRequireDependencies(sourcePath) {
48 var contents = fs.readFileSync(sourcePath, "utf-8"),
49 ary = [], lines = contents.split('\n'), that=this;
50
51 var re = new RegExp("sc_require\\((['\"])(.*)\\1\\)");
52 lines.forEach(function(line) {
53 var statements = line.split(';');
54 statements.forEach(function(statement) {
55 var result = re.exec(statement);
56 if (result) {
57 ary.push(result[2]);
58 }
59 });
60 });
61 return ary;
62}
63
64var corePath = path.join(sourceTree, 'core');
65javascriptFiles.forEach(function(sourcePath) {
66 var dependencies = scRequireDependencies(sourcePath) || [],
67 dependencyPath;
68
69 dependencyPath = sourcePath.slice(0, -3); // drop the '.js'
70 map[dependencyPath] = sourcePath;
71 g.addVertex(dependencyPath);
72 dependencies.forEach(function(name) {
73 g.addEdge(path.join(sourceTree, name), dependencyPath);
74 });
75 g.addEdge(corePath, dependencyPath);
76});
77
78var orderedFiles = [], sortedVertices = g.topologicalSort(), that=this;
79sortedVertices.forEach(function(vertex) {
80 var dep = map[vertex];
81 if (dep) orderedFiles.push(dep);
82 else console.log('could not find blossom/foundation framework dependency: '+vertex);
83});
84
85// Now we need to set up global for compatibility with Blossom
86global.window = global;
87global.navigator = { userAgent: 'node' };
88global.sc_require = function do_nothing(){};
89global.sc_resource = function sc_resource(){};
90global.YES = true ;
91global.NO = false ;
92global.SC = {};
93global.SproutCore = SC;
94global.SC.isNode = true;
95global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
96global.sc_assert = function(assertion, msg) {
97 if (!assertion) {
98 debugger;
99 throw msg || "sc_assert()";
100 }
101};
102
103// And we're ready to require the files in the correct order.
104orderedFiles.forEach(function(path) { require(path); });
105
106// Finally, we undo the SC namespace and create the BT namespace.
107global.BT = { foundationSourcePath: sourceTree };
108for (var key in SC) {
109 if (!SC.hasOwnProperty(key)) continue;
110 else global.BT[key] = SC[key];
111}
112delete global.SC;