-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessMermaid.js
232 lines (202 loc) · 8.89 KB
/
processMermaid.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
// We'll remove empty object fields (Dependencies, Compositions, Aggregations, etc.) in the final YAML.
// Updated code below:
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
// Ensure this import is present at the top
const parser = require('./classDiagramParser.js').parser;
// Extend the parser's `yy` object to handle class-specific logic
parser.yy = {
namespaces: {},
addNamespace: function (namespace) {
if (!this.namespaces[namespace]) {
this.namespaces[namespace] = {};
}
this.currentNamespace = namespace; // Set the current namespace
},
addClass: function (className) {
const currentNamespace = this.currentNamespace || 'global';
if (!this.namespaces[currentNamespace]) {
this.namespaces[currentNamespace] = {};
}
if (!this.namespaces[currentNamespace][className]) {
this.namespaces[currentNamespace][className] = {
Name: className,
Namespace: currentNamespace,
Type: 'Class',
Attributes: {},
Methods: {},
Dependencies: {},
Compositions: {},
Aggregations: {},
Assocations: {},
Realizations: {},
Implementations: {},
Inheritance: '',
Lines: {},
DashedLinks: {},
};
}
},
addMembers: function (className, members) {
const currentNamespace = this.currentNamespace || 'global';
if (this.namespaces[currentNamespace] && this.namespaces[currentNamespace][className]) {
members.forEach((member) => {
//Method: + methodName(args): returnType
const matchMethod = member.trim().match(/([+\-#~])\s*([\w<>]+)\s+(\w+)\s*\(([^)]*)\)\s*[;]*([\*\$]*)*?$/);
const matchAttribute = member.trim().match(/([+\-#~])\s*([\w<>]+)\s+(\w+)\s*[;]*([\*\$]*)*?$/);
if (matchMethod) {
const [_, scopeSymbol, type, name, methodArgs] = matchMethod;
const scopeMap = { '+': 'Public', '-': 'Private', '#': 'Protected', '~': 'Package' };
const scope = scopeMap[scopeSymbol] || 'Public';
if (methodArgs) {
// Add method
this.namespaces[currentNamespace][className].Methods[name] = {
ReturnType: type,
Scope: scope,
Classifiers: '',
Arguments: methodArgs.split(',').map((arg) => {
const [argType, argName] = arg.trim().split(/\s+/);
return {
Type: argType,
Name: argName,
};
}),
};
}
else
{
this.namespaces[currentNamespace][className].Methods[name] = {
ReturnType: type,
Scope: scope,
Classifiers: ''
};
}
}
//Attribute: - attributeName: type
else if(matchAttribute) {
const [_, scopeSymbol, type, name] = matchAttribute;
const scopeMap = { '+': 'Public', '-': 'Private', '#': 'Protected', '~': 'Package' };
const scope = scopeMap[scopeSymbol] || 'Public';
// Add attribute
this.namespaces[currentNamespace][className].Attributes[name] = {
Type: type,
IsSystemType: !!type.match(/^[A-Z]/),
Scope: scope,
DefaultValue: '',
};
}
});
} else {
console.warn(
`Warning: Attempted to add members to class "${className}" in namespace "${currentNamespace}", but it does not exist.`
);
}
},
addRelation: function (relation) {
const { id1, id2, relation: relationType, multiplicity = '1', title } = relation;
for (const ns of Object.values(this.namespaces)) {
for (const className of Object.values(ns))
{
if (className.Name === id1) {
const relationEntry = {
Multiplicity: multiplicity,
// Type: relationType.lineType.toLowerCase().replace('_', ''),
Description: title.replace(':','').trim(),
};
const relationTypeLine = relationType.lineType.toLowerCase().replace('_', '');
if (relationTypeLine === 'composition') {
ns[id1].Compositions[id2] = relationEntry;
} else if (relationTypeLine === 'aggregation') {
ns[id1].Aggregations[id2] = relationEntry;
} else if (relationTypeLine === 'association') {
ns[id1].Assocations[id2] = relationEntry;
}
else if (relationTypeLine === 'realization') {
ns[id1].Realizations[id2] = relationEntry;
}
else if(relationTypeLine === 'inheritance') {
ns[id1].Inheritance = id2;
}
else if(relationTypeLine === 'implementation') {
ns[id1].Implementations[id2] = relationEntry;
}
else if(relationTypeLine === 'line') {
ns[id1].Lines[id2] = relationEntry;
}
else if(relationTypeLine === 'dottedline') {
ns[id1].DashedLinks[id2] = relationEntry;
}
else if (relationTypeLine === 'dependency') {
ns[id1].Dependencies[id2] = relationEntry;
}
}
}
}
},
cleanupLabel: function (label) {
return label.trim();
},
addClassesToNamespace: function (namespace, classes) {
this.addNamespace(namespace); // Ensure namespace is created
this.currentNamespace = namespace;
classes.forEach((className) => this.addClass(className));
},
relationType: {
EXTENSION: 'extension',
COMPOSITION: 'composition',
AGGREGATION: 'aggregation',
ASSOCIATION: 'association',
DEPENDENCY: 'dependency',
REALIZATION: 'realization'
},
lineType: {
LINE: 'line',
DOTTED_LINE: 'dotted_line'
},
};
// Helper function to remove empty object keys
function removeEmptyKeys(obj) {
if (typeof obj !== 'object' || obj === null) return;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key];
if (typeof value === 'object') {
// Recursively clean nested objects
removeEmptyKeys(value);
// After cleaning, remove the key if still empty
if (Object.keys(value).length === 0) {
delete obj[key];
}
} else if (Array.isArray(value) && value.length === 0) {
delete obj[key];
} else if (value === '' && (key !== 'Name' && key !== 'Namespace' && key !== 'Type')) {
// Optionally remove empty strings, except for these fields if needed
delete obj[key];
}
}
}
}
// Read the Mermaid file
const mermaidFilePath = path.join(__dirname, 'mermaid.md');
const mermaidFileContent = fs.readFileSync(mermaidFilePath, 'utf8');
// Parse the Mermaid file
try {
parser.parse(mermaidFileContent);
// Generate YAML files for each class
for (const [namespace, classes] of Object.entries(parser.yy.namespaces)) {
const namespaceDir = path.join(__dirname, 'output', namespace.replace(/\./g, '_'));
fs.mkdirSync(namespaceDir, { recursive: true });
for (const [className, classData] of Object.entries(classes)) {
// Remove empty keys before converting to YAML
removeEmptyKeys(classData);
const yamlOutput = yaml.dump(classData, { noRefs: true });
const outputFilePath = path.join(namespaceDir, `${className}.yml`);
fs.writeFileSync(outputFilePath, yamlOutput);
console.log(`Generated YAML for class: ${className}, namespace: ${namespace}`);
}
}
} catch (error) {
console.error('Error parsing Mermaid diagram:', error.message);
console.error(error.stack);
}