-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectsDepGraph.cs
161 lines (140 loc) · 5.4 KB
/
ProjectsDepGraph.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DepAnalyzer
{
/// <summary>
/// Project dependency graph visualizing control
/// Using SolutionParser to access projects
/// </summary>
public partial class ProjectsDepGraph : UserControl
{
private string[] roots; // cache of root nodes for update
private List<Project> graphProjs; // list of projects to unsubscribe
public ProjectsDepGraph()
{
InitializeComponent();
roots = null;
graphProjs = null;
}
/// <summary>
/// Updates graph image
/// </summary>
/// <param name="rootNames">Dependency graph source names</param>
public void GenerateGraphImageForRoots(string[] rootNames)
{
if (GraphPictureBox.InvokeRequired)
{
GraphPictureBox.Invoke(new MethodInvoker(() => { GenerateGraphImageForRoots(rootNames); }));
return;
}
string dotCode = GenerateGraphForRoots(rootNames);
Image img = GenerateDotGraphImage(dotCode);
GraphPictureBox.Image = img;
GraphPictureBox.Size = img.Size;
//Size = img.Size;
}
private void OnProjectUpdate(object sender, EventArgs e)
{
GenerateGraphImageForRoots(roots);
}
private string ProjectStatusColor(Project proj)
{
if (proj.IsExternal)
return "lightcyan";
if (proj.Status == Project.BuildStatus.Wait)
return "gray";
if (proj.Status == Project.BuildStatus.Progress)
return "skyblue";
if (proj.Status == Project.BuildStatus.Success)
return "palegreen";
if (proj.Status == Project.BuildStatus.Failed)
return "tomato";
return "white";
}
private string GenerateGraphForRoots(string[] rootNames)
{
// DOT language documentation: http://www.graphviz.org/pdf/dotguide.pdf
string codeStr;
List<string> codeLines = new List<string>();
List<string> topRoots = new List<string>();
// Build graph from roots
bool signEvent = false;
if (roots != rootNames)
{
signEvent = true;
roots = rootNames;
if (graphProjs != null)
{
foreach (Project proj in graphProjs)
{
proj.StatusChanged -= OnProjectUpdate;
}
}
}
graphProjs = new List<Project>();
foreach (string rootName in rootNames)
{
Project proj = SolutionParser.ProjTable[rootName];
if (!proj.HasParent) // top root node
{
codeStr = String.Format("\"{0}\";", rootName);
topRoots.Add(codeStr);
}
codeStr = String.Format("\"{0}\" [shape=box,style=filled,color={1}];", rootName, ProjectStatusColor(proj));
codeLines.Add(codeStr);
foreach (Project depProj in proj.ProjectTree)
{
graphProjs.AddUnique(depProj);
}
}
codeStr = "{ rank = same;" + String.Join(" ", topRoots.ToArray()) + "}";
codeLines.Add(codeStr);
// Add graph edges
foreach (Project proj in graphProjs)
{
if (signEvent)
proj.StatusChanged += OnProjectUpdate;
string projName = proj.Name;
foreach (Project depProj in proj.DepProjects)
{
string depName = depProj.Name;
codeStr = String.Format("node[shape=ellipse,style=filled,color={0}];", ProjectStatusColor(depProj));
codeLines.Add(codeStr);
string style = String.Empty;
if (depProj.IsExternal)
style = " [style=dotted]";
codeStr = String.Format("\"{0}\" -> \"{1}\"{2};", projName, depName, style);
codeLines.Add(codeStr);
}
}
// Generate graph image
string dotCode = "digraph G {\n" + String.Join("\n", codeLines.ToArray()) + "}";
return dotCode;
}
private static Image GenerateDotGraphImage(string dotCode)
{
// Generate dot graph
WINGRAPHVIZLib.DOTClass dot = new WINGRAPHVIZLib.DOTClass();
WINGRAPHVIZLib.BinaryImage img = dot.ToPNG(dotCode);
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(img.ToBase64String());
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Stream
ms.Write(imageBytes, 0, imageBytes.Length);
// Convert Stream to Image
Image image = Image.FromStream(ms, true);
return image;
}
private void copyImageToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetImage(GraphPictureBox.Image);
}
}
}