This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathGraphvizWriter.cs
66 lines (57 loc) · 2.38 KB
/
GraphvizWriter.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace NuGetPackageVisualizer
{
public class GraphvizWriter : IPackageWriter
{
public void Write(List<PackageViewModel> packages, string file)
{
if (string.IsNullOrWhiteSpace(file))
{
file = "packages.dot";
}
Debug.WriteLine($"Writing {file}.");
var colors = new GraphVizColorConfiguration();
var sb = new StringBuilder();
WriteHeader(sb);
foreach (var package in packages)
{
sb.AppendFormat(" \"{0}\"[fillcolor=\"{1}\",label=\"{2}\"];",
package.GraphId(),
GraphHelper.GenerateBackgroundColor(packages, package, colors),
package.DisplayVersion()).AppendLine();
var dependenciesToWrite = package.Dependencies.Select(dep =>
String.Format(" \"{0}\" -> \"{1}\";", package.GraphId(), DependencyNodeId(dep, packages))).ToArray();
sb.AppendLine(String.Join(Environment.NewLine, dependenciesToWrite));
}
WriteClose(sb);
File.WriteAllText(file, sb.ToString());
}
private static void WriteHeader(StringBuilder sb)
{
sb.AppendLine("digraph packages {");
sb.AppendLine(" node [shape=box, style=\"rounded,filled\"];");
}
private static void WriteClose(StringBuilder sb)
{
sb.AppendLine("}");
}
private static string DependencyNodeId(DependencyViewModel dep, IEnumerable<PackageViewModel> packages)
{
string targetId = dep.GraphId();
// If version on dep is not explicitly stated, we should use an existing package with same nuget id.
// This greatly minimizes the number of disconnected nodes.
if (string.IsNullOrWhiteSpace(dep.Version))
{
PackageViewModel existingModel = packages.FirstOrDefault(x => x.NugetId == dep.NugetId);
if (existingModel != null)
targetId = existingModel.GraphId();
}
return targetId;
}
}
}