-
Notifications
You must be signed in to change notification settings - Fork 17
/
plantuml.js
160 lines (123 loc) · 4.73 KB
/
plantuml.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
let isSwiftClass = 'source.lang.swift.decl.class'
let isSwiftStruct = 'source.lang.swift.decl.struct'
let isSwiftEnum = 'source.lang.swift.decl.enum'
let isSwiftExtension = 'source.lang.swift.decl.extension'
let isSwiftProtocol = 'source.lang.swift.decl.protocol'
let isPublic = 'source.lang.swift.accessibility.internal'
let isPrivate = 'source.lang.swift.accessibility.private'
let linkTypeInheritance = '--|>'
let linkTypeRealize = '..|>'
let linkTypeDependency = '<..'
let linkTypeAssociation = '-->'
let linkTypeAggregation = '--o'
let linkTypeComposition = '--*'
let linkTypeGeneric = '--'
var STR2REPLACE = 'STR2REPLACE'
var plantumlTemplate = `
@startuml
' STYLE START
hide empty members
skinparam shadowing false
' STYLE END
`+STR2REPLACE+`
@enduml
`
let styleStruct = `<< (S, SkyBlue) struct >>`
let styleExtn = `<< (E,orchid) extension >>`
let styleEnum = `<< (E,LightSteelBlue) enum >>`
let styleProtocol = `<< (P,GoldenRod) protocol >>`
function uniqName(item, index, relationship){
var newName = item.name
var linkTypeKey = item.name + "LinkType"
if (uniqElementNames.includes(item.name)) {
newName += `${index}`
if (item.kind == isSwiftExtension) {
var connect = `${item.name} ${linkTypeDependency} ${newName} : ${relationship}`
extnConnections.push(connect)
}
}
// else if(item.kind == isSwiftExtension) {
// newName += `${index}`
// var connect = `${item.name} <.. ${newName} : ${relationship}`
// extnConnections.push(connect)
// }
else {
uniqElementNames.push(item.name)
uniqElementAndTypes[item.name] = relationship
if (relationship == "inherits"){
uniqElementAndTypes[linkTypeKey] = linkTypeInheritance
}
else if (relationship == "confirms to") {
uniqElementAndTypes[linkTypeKey] = linkTypeRealize
}
else if (relationship == "ext") {
uniqElementAndTypes[linkTypeKey] = linkTypeDependency
}
else if (relationship == "association") {
uniqElementAndTypes[linkTypeKey] = linkTypeAssociation
}
else if (relationship == "aggregation") {
uniqElementAndTypes[linkTypeKey] = linkTypeAggregation
}
else if (relationship == "composition") {
uniqElementAndTypes[linkTypeKey] = linkTypeComposition
}
else {
uniqElementAndTypes[linkTypeKey] = linkTypeGeneric
}
}
return newName
}
var srcjs = require('./out.json')
// console.log(`total no. of elements:` + srcjs.length)
var msg = ''
var i = 0
var uniqElementNames = []
var uniqElementAndTypes = {}
var connections = []
var extnConnections = []
srcjs.forEach(function (item){
var strItem = ''
if (item && item.kind == isSwiftClass && item.name){
strItem += 'class "' + item.name + '" as ' + uniqName(item, i, "inherits") + ' {\n'
}
else if (item && item.kind == isSwiftStruct && item.name){
strItem += 'class "' + item.name + '" as ' + uniqName(item, i, "inherits") + ' ' + styleStruct + ' {\n'
}
else if (item && item.kind == isSwiftExtension && item.name){
strItem += 'class "' + item.name + '" as ' + uniqName(item, i, "ext") + ' ' + styleExtn + ' {\n'
}
else if (item && item.kind == isSwiftEnum && item.name) {
strItem += 'class "' + item.name + '" as ' + uniqName(item, i, "") + ' ' + styleEnum + ' {\n'
}
else if (item && item.kind == isSwiftProtocol && item.name) {
strItem += 'class "' + item.name + '" as ' + uniqName(item, i, "confirms to") + ' ' + styleProtocol + ' {\n'
}
var methods = ''
if (item.members && item.members.length>0){
item.members.forEach(function (method) {
var msig = ' '
msig += (method.scope == isPublic)? '+': '-'
msig += method.name + '\n'
methods += msig
})
}
if (item.inherits && item.inherits.length > 0) {
item.inherits.forEach(function (obj) {
var linkTo = obj["key.name"]
var namedConnection = (uniqElementAndTypes[linkTo]) ? ": " + uniqElementAndTypes[linkTo] : ""
var linkTypeKey = item.name + "LinkType"
if (uniqElementAndTypes[linkTo] == "confirms to"){
linkTypeKey = linkTo + "LinkType"
}
var connect = `${item.name} ${uniqElementAndTypes[linkTypeKey]} ${linkTo} ${namedConnection}`
connections.push(connect)
})
}
strItem += methods+ '\n}\n'
msg += strItem
i++
}
);
var out = plantumlTemplate.replace(STR2REPLACE, msg + "\n" + connections.join("\n") + "\n" + extnConnections.join("\n"))
console.log(out)