-
Notifications
You must be signed in to change notification settings - Fork 26
/
GraphvizWriter.kt
55 lines (42 loc) · 1.6 KB
/
GraphvizWriter.kt
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
package com.jraska.github.client.graph
object GraphvizWriter {
fun toGraphviz(dependencyGraph: DependencyGraph, groups: Set<String> = emptySet()): String {
val longestPathConnections = dependencyGraph.longestPath()
.nodeNames.zipWithNext()
.toSet()
val stringBuilder = StringBuilder()
stringBuilder.append("digraph G {\n")
if(groups.isNotEmpty()) {
stringBuilder.append("ranksep = 1.5\n")
}
groups.forEach {
stringBuilder.append(generateGroup(dependencyGraph, it))
}
dependencyGraph.nodes().flatMap { node -> node.dependsOn.map { node.key to it.key } }
.forEach { connection ->
stringBuilder.append("\"${connection.first}\"")
.append(" -> ")
.append("\"${connection.second}\"")
if (longestPathConnections.contains(connection)) {
stringBuilder.append(" [color=red style=bold]")
}
stringBuilder.append("\n")
}
stringBuilder.append("}")
return stringBuilder.toString()
}
private fun generateGroup(dependencyGraph: DependencyGraph, groupName: String): String {
val builder = StringBuilder()
.append("subgraph cluster_").append(groupName.replace(":", "")).appendln("{")
.appendln("style = filled;")
.appendln("color = lightgrey;")
.appendln("node[style = filled, color = white];")
.append("label = \"").append(groupName).appendln("\"")
dependencyGraph.nodes().filter { it.key.startsWith(groupName) }.forEach {
builder.append("\"").append(it.key).appendln("\"")
}
return builder.appendln("}")
.appendln()
.toString()
}
}