forked from ThomasArdal/NuGetPackageVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDGMLWriter.cs
45 lines (40 loc) · 1.9 KB
/
DGMLWriter.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
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace NuGetPackageVisualizer
{
public class DGMLWriter : IPackageWriter
{
public void Write(List<PackageViewModel> packages, string file)
{
if (string.IsNullOrWhiteSpace(file))
{
file = "packages.dgml";
}
XNamespace ns = "http://schemas.microsoft.com/vs/2009/dgml";
var colors = new DgmlColorConfiguration();
var nodes =
packages
.Select(
package =>
new XElement(ns + "Node",
new XAttribute("Id", package.Id),
new XAttribute("Label", string.Format("{0} ({1})", package.NugetId, package.LocalVersion)),
new XAttribute("Background", GraphHelper.GenerateBackgroundColor(packages, package, colors))))
.ToList();
var links =
(packages
.SelectMany(
package => package.Dependencies, (package, dep) =>
new XElement(ns + "Link",
new XAttribute("Source", package.Id),
new XAttribute("Target", packages.Any(x => x.NugetId == dep.NugetId && x.LocalVersion == dep.Version) ? packages.First(x => x.NugetId == dep.NugetId && x.LocalVersion == dep.Version).Id : string.Format("{0} ({1})", dep.NugetId, dep.Version)))))
.ToList();
var document =
new XDocument(
new XDeclaration("1.0", "utf-8", string.Empty),
new XElement(ns + "DirectedGraph", new XElement(ns + "Nodes", nodes), new XElement(ns + "Links", links)));
document.Save(file);
}
}
}