-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculateDeps.js
257 lines (182 loc) · 5.88 KB
/
CalculateDeps.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
const path = require("path")
const fs = require("fs")
const _ = require("lodash");
const strip_comments = require('strip-comments');
class CalculateDeps {
constructor() {
this._files = [];
this._modules = [];
this._singles = [];
this._ignores = [];
}
refreshDeps(sources, dest, singles, ignores) {
this._singles = singles;
this._ignores = ignores;
this._loadModules(this._getAllFiles(sources));
this._analyseModules();
this._emit(dest);
return true;
}
_emit(dest) {
let textToPrint = String(fs.readFileSync('container.template'));
let imports = this._getImports();
let declares = this._getDeclares();
let addModules = this._getAddModules();
let addDependsAll = this._getAddDepends();
textToPrint = textToPrint.replace('TEMP_IMPORT_LIST_ALL', imports);
textToPrint = textToPrint.replace('TEMP_DECLARATIONS', declares);
textToPrint = textToPrint.replace('TEMP_ADDMODULES', addModules);
textToPrint = textToPrint.replace('TEMP_ADDDEPENDS', addDependsAll);
fs.writeFileSync(dest, textToPrint, 'utf8');
}
_getImports() {
let importstatement = '';
for (let c = 0; c < this._modules.length; c++) {
let mod = this._modules[c];
if (mod.name != null) {
let path2 = mod.path.replace('.ts', '');
let path3 = "../../" + path2.substr(path2.indexOf('src'), path2.length);
path3 = path3.replace(/\\/g, "/");
//Debug.info(path3);
importstatement += "import " + mod.name + " from '" + path3 + "';\n";
}
}
return importstatement;
}
_getDeclares() {
let declares = '';
for (let c = 0; c < this._modules.length; c++) {
let mod = this._modules[c];
if (mod.name != null) {
declares += 'private _' + mod.name + ": any;\n";
}
}
return declares;
}
_getAddModules() {
let moduleList = '';
for (let c = 0; c < this._modules.length; c++) {
let mod = this._modules[c];
if (mod.name != null) {
moduleList += "this._" + mod.name + " = this._smartDepend.addModule(" + mod.name + ", " + String(mod.isSingle) + ");\n";
}
}
return moduleList;
}
_getAddDepends() {
let dependsList = '';
for (let c = 0; c < this._modules.length; c++) {
let mod = this._modules[c];
if (mod.name != null && mod.deps.length > 0) {
for (let d = 0; d < mod.deps.length; d++) {
dependsList += 'this._smartDepend.addDependency(this._' + mod.name + ", this._" + mod.deps[d] + ");\n";
}
dependsList += "\n\n";
}
}
return dependsList;
}
_analyseModules() {
for (let c = 0; c < this._modules.length; c++) {
let mod = this._modules[c];
this._readModule(mod);
}
}
_loadModules(files) {
for (let c = 0; c < files.length; c++) {
let mod = this._createModule(files[c]);
this._modules.push(mod);
}
}
_readModule(mod) {
let path2 = mod.path;
let textToPrint = strip_comments(String(fs.readFileSync(path2)));
let isClass = textToPrint.indexOf('class') > -1;
if (isClass) {
let relevantArea = textToPrint.substr(textToPrint.indexOf('class'), 5000);
let relevantArea2 = textToPrint.substr(textToPrint.indexOf('constructor'), 5000);
let depArea = relevantArea2.substr(relevantArea2.indexOf('('), 5000);
depArea = depArea.split(")")[0] + ")";
let mainTokens = relevantArea.split(' ');
let name = mainTokens[1];
if (!this._isIgnore(name)) {
let isSingle = this._isSingleton(name);
depArea = depArea.substr(1, depArea.length-2);
depArea = depArea.replace(/(?:\r\n|\r|\n)/g, '');
let depList = this._createDeps(depArea);
if (depList[0] != '') {
mod.name = name;
mod.deps = depList;
mod.isSingle = isSingle;
} else {
mod.name = name;
mod.isSingle = isSingle;
}
}
}
}
_createDeps(rawDeps) {
let arr = rawDeps.split(',');
for (let c = 0; c < arr.length; c++) {
arr[c] = arr[c].trim();
}
for (let c = 0; c < arr.length; c++) {
let elm = arr[c];
if (elm != '') {
let token = elm.split(':');
elm = token[1].trim();
if (elm.indexOf("I") == 0) {
if (elm[1].toUpperCase() == elm[1]) {
elm = elm.substring(1, elm.length);
}
}
arr[c] = elm;
}
}
return arr;
}
_isIgnore(name) {
for (let c = 0; c < this._ignores.length; c++) {
let ignore = this._ignores[c];
if (name == ignore) return true;
}
return false;
}
_isSingleton(name) {
for (let c = 0; c < this._singles.length; c++) {
let single = this._singles[c];
//Debug.info("lhs(%s) == rhs(%s)", name, single);
if (name == single) return true;
}
return false;
}
_getAllFiles(dirs) {
let files = [];
for (let c = 0; c < dirs.length; c++) {
let dir = dirs[c];
let f = [];
f = this._readDir(dir, f);
for (let d = 0; d < f.length; d++) {
files.push(f[d]);
}
}
return files;
}
_readDir(dir, filelist) {
let files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach((file) => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = this._readDir(path.join(dir, file), filelist);
}
else {
filelist.push(path.resolve(path.join(dir, file)));
}
});
return filelist;
}
_createModule(path2) {
return {name: null, path: path2, deps: [], isSingle: false};
}
}
module.exports = CalculateDeps;