-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrouper.js
294 lines (237 loc) · 7.43 KB
/
grouper.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
const fs = require("fs");
const path = require("path");
const config = require("./ojs-config.json");
/**
* Splits a file into smaller strings
* based on the class in that file
*/
class Splitter {
/**
* Gets the class Signature
* @param {string} content
* @param {int} start
* @param {object<>} signature {name: string, signature: string, start: number, end: number}
*/
classSignature(content, start) {
const signature = {
name: "",
definition: "",
start: -1,
end: -1,
parent: null,
};
let startAt = start;
let output = [];
let tmp = "";
let pushTmp = (index) => {
if (tmp.length === 0) return;
if (output.length === 0) startAt = index;
output.push(tmp);
tmp = "";
};
for (let i = start; i < content.length; i++) {
let ch = content[i];
if (/[\s\r\t\n]/.test(ch)) {
pushTmp(i);
continue;
}
if (/\{/.test(ch)) {
pushTmp(i);
signature.end = i;
break;
}
tmp += ch;
}
signature.start = startAt;
if (output.length && output[0] !== "class") {
let temp = [];
temp[0] = output[0];
temp[1] = output.splice(1).join(" ");
output = temp;
}
if (output.length % 2 !== 0)
throw Error(
`Invalid Class File. Could not parse \`${content}\` from index ${start} because it doesn't have the proper syntax. ${content.substring(
start
)}`
);
if (output.length > 2) {
signature.parent = output[3];
}
signature.name = output[1];
signature.definition = output.join(" ");
return signature;
}
/**
* Splits the content of the file by
* class
* @param {string} content file content
* @return {Map<string,string>} class map
*/
classes(content) {
content = content.trim();
const stack = [];
const map = new Map();
const qMap = new Map([
[`'`, true],
[`"`, true],
["`", true],
]);
let index = 0;
let code = "";
while (index < content.length) {
let signature = this.classSignature(content, index);
index = signature.end;
let ch = content[index];
stack.push(ch);
code += signature.definition + " ";
code += ch;
let text = [];
index++;
while (stack.length && index < content.length) {
ch = content[index];
code += ch;
if (qMap.has(ch)) {
text.push(ch);
index++;
while (text.length && index < content.length) {
ch = content[index];
code += ch;
let last = text.length - 1;
if (qMap.has(ch) && ch === text[last]) {
text.pop();
} else if (
ch === "\n" &&
(text[last] === '"' || text[last] === "'")
) {
text.pop();
}
index++;
}
continue;
}
if (/\{/.test(ch)) stack.push(ch);
if (/\}/.test(ch)) stack.pop();
index++;
}
signature.name = signature.name.split(/\(/)[0];
map.set(signature.name, {
extends: signature.parent,
code,
name: signature.name,
signature: signature.definition,
});
code = "";
}
return map;
}
}
class Grouper extends Splitter {
/**
*
* @param {array} filePaths
* @returns all the classes from all the files with the filePaths array
*/
async classesFromFilePath(filePaths) {
let cls = "";
const params = {};
const names = {};
let i = 1;
for (let filePath of filePaths) {
const classesContent = fs.readFileSync(filePath, "utf-8");
const map = this.classes(classesContent);
for(let [key, {code, name, extends: parent, signature}] of map) {
if(/OpenScript\.Mediator/.test(parent) && !/.*Mediator$/.test(name)) {
let newName = `${name}Mediator`;
let newSig = signature.replace(`${name} `, `${newName} `);
let newCode = code.replace(signature, newSig);
code = newCode;
name = newName;
signature = newSig;
}
if(name in names) {
let newName = `${name}${i++}`;
let newSig = signature.replace(`${name} `, `${newName} `);
let newCode = code.replace(signature, newSig);
code = newCode;
name = newName;
signature = newSig;
}
names[name] = true;
cls += code + "\n";
params[name] = true;
}
}
return {code: cls, params: Object.keys(params)};
}
/**
*
* @param {*} filePaths wraps all the code from a specified file path in the ojs function
*/
async wrapClassesFromFilePath(filePaths) {
try {
let {code, params} = await this.classesFromFilePath(filePaths);
let finalCode = `${code}\nojs(${params.join(',')});`
this.consolidateToFile(finalCode);
} catch (error) {
console.error("Error printing class names:", error);
}
}
/**
*
* @param {string} consolidatedCode takes in the consolidated code and then creates a
* dotOjs file with that has all the code from all the classes wrapped in the ojs function
*/
async consolidateToFile(consolidatedCode) {
const consolidatedFile = config.output_dir + "/" + config.output_file;
fs.writeFileSync(consolidatedFile, consolidatedCode, "utf-8");
console.log(`Wrapped code has been written to ${consolidatedFile}`);
}
/**
*
* @param {string} dir
* @param {string} files
* @returns an array of all the paths in the given directory
*/
async getFilePaths(dir, files = []) {
const fileList = fs.readdirSync(dir);
for (const file of fileList) {
const name = `${dir}/${file}`;
if (fs.statSync(name).isDirectory()) {
this.getFilePaths(name, files);
} else {
files.push(name);
}
}
return files;
}
/**
*
* @returns all the grouped code by making use of the getFilePaths method and
* wrapClassesFromFilePath
*/
async group() {
try {
let AllPaths = [];
for (let key in config.dir) {
let dir = config.dir[key];
let directories = await grouper.getFilePaths(dir);
AllPaths.push(directories);
}
let paths = AllPaths;
let configPaths = [];
for (let path of paths) {
for (let directory of path) {
configPaths.push(directory);
}
}
console.log(configPaths);
let code = await grouper.wrapClassesFromFilePath(configPaths);
return code;
} catch (error) {
console.error("Error getting all classes:", error);
}
}
}
const grouper = new Grouper();
grouper.group();