-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (109 loc) · 3.98 KB
/
main.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
var doctrine = require("doctrine");
var fs = require('fs');
var recursiveReadSync = require('recursive-readdir-sync');
var glob = require('glob');
var lodash = require('lodash');
var plantuml = require('node-plantuml');
//var filesPath = glob.sync('../collectivoo/blocks/l-app/**/*.js');
//var filesPath = glob.sync('../quiz/blocks/l-quiz/**/*.js');
//var filesPath = glob.sync('../sber-together/blocks/l-sber-vmeste/**/*.js');
//var filesPath = glob.sync('../mission/blocks/l-mission/**/*.js');
var filesPath = glob.sync('../active-age/blocks/l-active-age/**/*.js');
var classStorage = {};
filesPath.map(function(filePath) {
var file = fs.readFileSync(filePath, 'utf8');
if (file.indexOf('goog.provide') >= 0 || file.indexOf('goog.module') >= 0) {
var jsclass = {
file: file
};
if (file.indexOf('goog.provide') >= 0)
var namespace = file.match(/goog.provide\('(.*)'\);/)[1];
if (file.indexOf('goog.module') >= 0)
namespace = file.match(/goog.module\('(.*)'\);/)[1];
classStorage[namespace] = jsclass;
}
});
for (var j in classStorage) {
var jsclass = classStorage[j];
var file = jsclass.file;
delete jsclass.file;
var regex = /[^\S\r\n]*\/(?:\*{2})([\W\w]+?)\*\/([\W\w]+?)?(?=\s+\/\*{1,2}|$)/g;
var matches, jsdocs = [];
while (matches = regex.exec(file)) {
jsdocs.push(matches[1]);
}
jsdocs.map(function(jsdoc) {
var tags = doctrine.parse(jsdoc, { unwrap: true }).tags;
tags.map(function(tag) {
if (tag.title == 'extends') {
jsclass.extends = tag.type.name;
}
if (tag.title == 'type' && (tag.type.name || tag.type.applications)) {
jsclass.associations = jsclass.associations ? jsclass.associations : [];
if (tag.type.name && !isBaseType(tag.type.name)) {
jsclass.associations.push(tag.type.name);
}
if (tag.type.applications && !isBaseType(tag.type.applications[0].name)) {
jsclass.associations.push(tag.type.applications[0].name);
}
//TODO перечисления через |
}
})
});
if (jsclass.associations) {
jsclass.associations = lodash.uniq(jsclass.associations);
}
}
function isBaseType(type) {
if (type == 'string' ||
type == 'String' ||
type == 'object' ||
type == 'Object' ||
type == 'number' ||
type == 'Number' ||
type == 'array' ||
type == 'Array' ||
type == 'bool' ||
type == 'Bool' ||
type == 'boolean' ||
type == 'Boolean' ||
type == 'int'
)
return true;
}
function getName(namespace) {
if (namespace == undefined || namespace.indexOf('.') == -1) {
return namespace;
} else {
return namespace.split('.').pop();
}
}
var umlText = '@startuml\n';
for (var namespace in classStorage) {
var jsclass = classStorage[namespace];
if (getName(namespace) !== 'View') {
if (jsclass.extends) {
if (jsclass.extends == 'cl.iControl.Control') {
umlText += 'class ' + getName(namespace);
umlText += ' <extend ' + getName(jsclass.extends) + '>';
} else {
umlText += 'class ' + getName(jsclass.extends) + ' <|-- ' + getName(namespace);
}
umlText += '\n';
}
if (jsclass.associations) {
jsclass.associations.forEach(function (association) {
if (getName(association) !== 'View') {
umlText += getName(namespace) + ' --> ' + getName(association);
umlText += '\n';
}
})
}
umlText += '\n';
}
}
umlText += '@enduml';
//console.log(classStorage);
fs.writeFileSync('uml.pu', umlText, 'utf8');
var gen = plantuml.generate("uml.pu", {format: 'png'});
gen.out.pipe(fs.createWriteStream("uml.png"));