/
prelviz.go
269 lines (240 loc) · 6.64 KB
/
prelviz.go
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
package prelviz
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/awalterschulze/gographviz"
"github.com/samber/lo"
)
type Prelviz struct {
projectModuleName string
packageInfoMap map[string]*PackageInfo
config *Config
output io.Writer
dotLayout string
}
type NodeInfo struct {
Name string
DirectoryPath string
IsGrouping bool
ContainsPackageNum int
}
func NewPrelviz(projectDirectoryPath, outputFilePath, dotLayout string) (*Prelviz, error) {
moduleName, err := GetModuleName(projectDirectoryPath)
if err != nil {
return nil, err
}
packageInfoMap, err := NewPackageInfoMap(projectDirectoryPath)
if err != nil {
return nil, err
}
config, err := NewConfig(projectDirectoryPath, moduleName)
if err != nil {
return nil, err
}
var output io.Writer
if outputFilePath == "" {
output = os.Stdout
} else {
output, err = os.Create(outputFilePath)
if err != nil {
return nil, err
}
}
return &Prelviz{
projectModuleName: moduleName,
packageInfoMap: packageInfoMap,
config: config,
output: output,
dotLayout: dotLayout,
}, nil
}
func (m *Prelviz) Run() error {
// add graph
var (
graphDefaultAttrs = map[string]string{
"charset": `"UTF-8"`,
"label": `"package relation"`,
"labelloc": `"t"`,
"labeljust": `"c"`,
"bgcolor": `"#343434"`,
"fontsize": "18",
"fontcolor": `"white"`,
"style": `"filled"`,
"rankdir": `"TB"`,
"margin": "0.5",
"layout": fmt.Sprintf(`"%s"`, m.dotLayout),
}
nodeDefaultAttrs = map[string]string{
"shape": `"record"`,
"style": `"solid,filled"`,
"fontcolor": "6",
"fontsize": "14",
"color": "7",
"colorscheme": `"spectral11"`,
}
)
graphAst, _ := gographviz.ParseString(`digraph d {}`)
graph := gographviz.NewGraph()
if err := gographviz.Analyse(graphAst, graph); err != nil {
return err
}
graphAttrs, err := gographviz.NewAttrs(graphDefaultAttrs)
if err != nil {
return err
}
graph.Attrs.Extend(graphAttrs)
// add node
for nodeName, info := range m.nodeInfoMap() {
if info.IsGrouping {
if graph.IsNode(nodeName) {
continue
}
if err = graph.AddNode("G", m.toDotLangFormat(nodeName), lo.Assign(
nodeDefaultAttrs,
map[string]string{
"fillcolor": "9",
"label": fmt.Sprintf(`"{path: %s|pkg: %d}"`, info.DirectoryPath, info.ContainsPackageNum),
},
)); err != nil {
return err
}
} else {
if err = graph.AddNode("G", m.toDotLangFormat(nodeName), lo.Assign(
nodeDefaultAttrs,
map[string]string{
"fillcolor": "10",
"label": fmt.Sprintf(`"{pkg: %s|path: %s}"`, info.Name, info.DirectoryPath),
},
)); err != nil {
return err
}
}
}
// add edge
for srcNodeName, relationMap := range m.nodeRelationCountMap() {
for dstNodeName, relationNum := range relationMap {
if m.isNgRelation(srcNodeName, dstNodeName) {
if err = graph.AddEdge(m.toDotLangFormat(srcNodeName), m.toDotLangFormat(dstNodeName), true, map[string]string{
"color": `"red"`,
"weight": fmt.Sprintf(`"%d"`, relationNum),
"label": fmt.Sprintf(`"dep:%d"`, relationNum),
"fontcolor": `"white"`,
"decorate": `"true"`,
}); err != nil {
return err
}
} else {
if err = graph.AddEdge(m.toDotLangFormat(srcNodeName), m.toDotLangFormat(dstNodeName), true, map[string]string{
"color": `"white"`,
"weight": fmt.Sprintf(`"%d"`, relationNum),
"label": fmt.Sprintf(`"dep:%d"`, relationNum),
"fontcolor": `"white"`,
"decorate": `"true"`,
}); err != nil {
return err
}
}
}
}
if _, err = fmt.Fprint(m.output, graph.String()); err != nil {
return err
}
return nil
}
func (m *Prelviz) nodeInfoMap() map[string]*NodeInfo {
nodeInfoMap := make(map[string]*NodeInfo)
for pkgDirPath, info := range m.packageInfoMap {
if m.isExcludePackageWithDirPath(pkgDirPath) {
continue
}
nodeName := m.nodeName(pkgDirPath)
if _, ok := nodeInfoMap[nodeName]; ok {
nodeInfoMap[nodeName].ContainsPackageNum++
} else {
if m.isGroupingNode(pkgDirPath) {
nodeInfoMap[nodeName] = &NodeInfo{
DirectoryPath: m.groupingPackageDirectoryPath(pkgDirPath),
IsGrouping: true,
ContainsPackageNum: 1,
}
} else {
nodeInfoMap[nodeName] = &NodeInfo{
Name: info.Name,
DirectoryPath: pkgDirPath,
IsGrouping: false,
ContainsPackageNum: 1,
}
}
}
}
return nodeInfoMap
}
func (m *Prelviz) nodeRelationCountMap() map[string]map[string]int {
nodeRelationCountMap := make(map[string]map[string]int)
for pkgDirPath, info := range m.packageInfoMap {
if m.isExcludePackageWithDirPath(pkgDirPath) {
continue
}
nodeName := m.nodeName(pkgDirPath)
for importPath, usageMap := range info.ImportUsageMap {
if !m.isTargetPackage(importPath) {
continue
}
importPathNodeName := m.importPathNodeName(importPath)
if importPathNodeName == nodeName {
continue
}
if m.isExcludePackage(importPath) {
continue
}
if _, ok := nodeRelationCountMap[nodeName]; !ok {
nodeRelationCountMap[nodeName] = map[string]int{
importPathNodeName: len(usageMap),
}
} else {
nodeRelationCountMap[nodeName][importPathNodeName] += len(usageMap)
}
}
}
return nodeRelationCountMap
}
func (m *Prelviz) importPathNodeName(importPath string) string {
dirPath := strings.TrimPrefix(importPath, m.projectModuleName)
dirPath = strings.TrimPrefix(dirPath, "/")
return m.nodeName(dirPath)
}
func (m *Prelviz) isGroupingNode(pkgDirPath string) bool {
if m.config == nil {
return false
}
return m.config.IsGroupingPackage(pkgDirPath)
}
func (m *Prelviz) groupingPackageDirectoryPath(pkgDirPath string) string {
return m.config.GroupingPackageDirectoryPath(pkgDirPath)
}
func (m *Prelviz) nodeName(pkgDirPath string) string {
nodeName := filepath.Join(m.projectModuleName, pkgDirPath)
if m.config.IsGroupingPackage(pkgDirPath) {
return filepath.Join(m.projectModuleName, m.config.GroupingPackageDirectoryPath(pkgDirPath))
}
return nodeName
}
func (m *Prelviz) isTargetPackage(importPath string) bool {
return strings.Contains(importPath, m.projectModuleName)
}
func (m *Prelviz) toDotLangFormat(in string) string {
return fmt.Sprintf(`"%s"`, in)
}
func (m *Prelviz) isNgRelation(from, to string) bool {
return m.config.IsNgRelation(from, to)
}
func (m *Prelviz) isExcludePackage(pkg string) bool {
return m.config.IsExcludePackage(pkg)
}
func (m *Prelviz) isExcludePackageWithDirPath(pkgDirPath string) bool {
pkg := filepath.Join(m.projectModuleName, pkgDirPath)
return m.config.IsExcludePackage(pkg)
}