1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var fs = require('fs'),
|
9 | path = require('path'),
|
10 | Graph = require('./utils/graph');
|
11 |
|
12 |
|
13 | var sourceFiles = [],
|
14 | sourceTree = path.join(__dirname, "../foundation");
|
15 |
|
16 | function 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 |
|
32 | processDirectory(sourceTree);
|
33 |
|
34 |
|
35 | var javascriptFiles = sourceFiles.filter(function(sourcePath) {
|
36 |
|
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 |
|
44 | var g = new Graph();
|
45 | var map = {};
|
46 |
|
47 | function 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 |
|
64 | var corePath = path.join(sourceTree, 'core');
|
65 | javascriptFiles.forEach(function(sourcePath) {
|
66 | var dependencies = scRequireDependencies(sourcePath) || [],
|
67 | dependencyPath;
|
68 |
|
69 | dependencyPath = sourcePath.slice(0, -3);
|
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 |
|
78 | var orderedFiles = [], sortedVertices = g.topologicalSort(), that=this;
|
79 | sortedVertices.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 |
|
86 | global.window = global;
|
87 | global.navigator = { userAgent: 'node' };
|
88 | global.sc_require = function do_nothing(){};
|
89 | global.sc_resource = function sc_resource(){};
|
90 | global.YES = true ;
|
91 | global.NO = false ;
|
92 | global.SC = {};
|
93 | global.SproutCore = SC;
|
94 | global.SC.isNode = true;
|
95 | global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
|
96 | global.sc_assert = function(assertion, msg) {
|
97 | if (!assertion) {
|
98 | debugger;
|
99 | throw msg || "sc_assert()";
|
100 | }
|
101 | };
|
102 |
|
103 |
|
104 | orderedFiles.forEach(function(path) { require(path); });
|
105 |
|
106 |
|
107 | global.BT = { foundationSourcePath: sourceTree };
|
108 | for (var key in SC) {
|
109 | if (!SC.hasOwnProperty(key)) continue;
|
110 | else global.BT[key] = SC[key];
|
111 | }
|
112 | delete global.SC;
|