-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Mermaid dependency graphs
- Loading branch information
Showing
14 changed files
with
461 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.IO; | ||
|
||
namespace Chisel; | ||
|
||
internal sealed class GraphvizWriter : GraphWriter | ||
{ | ||
public GraphvizWriter(TextWriter writer) : base(writer) | ||
{ | ||
} | ||
|
||
protected override void WriteHeader(GraphDirection graphDirection) | ||
{ | ||
Writer.WriteLine("# Generated by https://github.com/0xced/Chisel"); | ||
Writer.WriteLine(); | ||
Writer.WriteLine("digraph"); | ||
Writer.WriteLine("{"); | ||
|
||
if (graphDirection == GraphDirection.LeftToRight) | ||
Writer.WriteLine(" rankdir=LR"); | ||
else if (graphDirection == GraphDirection.TopToBottom) | ||
Writer.WriteLine(" rankdir=TB"); | ||
|
||
Writer.WriteLine(" node [ fontname = \"Segoe UI, sans-serif\", shape = box, style = filled, color = aquamarine ]"); | ||
Writer.WriteLine(); | ||
} | ||
|
||
protected override void WriteFooter() | ||
{ | ||
Writer.WriteLine("}"); | ||
} | ||
|
||
protected override void WriteNode(Package package) | ||
{ | ||
Writer.Write($" \"{package.Id}\""); | ||
if (package.State == PackageState.Ignore) | ||
{ | ||
Writer.Write(" [ color = lightgray ]"); | ||
} | ||
else if (package.State == PackageState.Remove) | ||
{ | ||
Writer.Write(" [ color = lightcoral ]"); | ||
} | ||
else if (package.Type == PackageType.Project) | ||
{ | ||
Writer.Write(" [ color = skyblue ]"); | ||
} | ||
else if (package.Type == PackageType.Unknown) | ||
{ | ||
Writer.Write(" [ color = khaki ]"); | ||
} | ||
|
||
Writer.WriteLine(); | ||
} | ||
|
||
protected override void WriteEdge(Package package, Package dependency) | ||
{ | ||
Writer.WriteLine($" \"{package.Id}\" -> \"{dependency.Id}\""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.IO; | ||
|
||
namespace Chisel; | ||
|
||
internal sealed class MermaidWriter : GraphWriter | ||
{ | ||
public MermaidWriter(TextWriter writer) : base(writer) | ||
{ | ||
} | ||
|
||
protected override void WriteHeader(GraphDirection graphDirection) | ||
{ | ||
Writer.WriteLine("%% Generated by https://github.com/0xced/Chisel"); | ||
Writer.WriteLine(); | ||
Writer.Write("graph"); | ||
if (graphDirection == GraphDirection.LeftToRight) | ||
Writer.Write(" LR"); | ||
else if (graphDirection == GraphDirection.TopToBottom) | ||
Writer.Write(" TB"); | ||
Writer.WriteLine(); | ||
|
||
Writer.WriteLine(); | ||
Writer.WriteLine("classDef default fill:aquamarine,stroke:aquamarine"); | ||
Writer.WriteLine("classDef project fill:skyblue,stroke:skyblue"); | ||
Writer.WriteLine("classDef unknown fill:khaki,stroke:khaki"); | ||
Writer.WriteLine("classDef ignored fill:lightgray,stroke:lightgray"); | ||
Writer.WriteLine("classDef removed fill:lightcoral,stroke:lightcoral"); | ||
Writer.WriteLine(); | ||
} | ||
|
||
protected override void WriteFooter() | ||
{ | ||
} | ||
|
||
protected override void WriteNode(Package package) | ||
{ | ||
Writer.Write($"class {package.Id} "); | ||
if (package.State == PackageState.Ignore) | ||
{ | ||
Writer.WriteLine("ignored"); | ||
} | ||
else if (package.State == PackageState.Remove) | ||
{ | ||
Writer.WriteLine("removed"); | ||
} | ||
else if (package.Type == PackageType.Unknown) | ||
{ | ||
Writer.WriteLine("unknown"); | ||
} | ||
else if (package.Type == PackageType.Project) | ||
{ | ||
Writer.WriteLine("project"); | ||
} | ||
else | ||
{ | ||
Writer.WriteLine("default"); | ||
} | ||
} | ||
|
||
protected override void WriteEdge(Package package, Package dependency) | ||
{ | ||
Writer.WriteLine($"{package.Id} --> {dependency.Id}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Chisel; | ||
|
||
internal abstract class GraphWriter | ||
{ | ||
public static GraphWriter Graphviz(TextWriter writer) => new GraphvizWriter(writer); | ||
|
||
public static GraphWriter Mermaid(TextWriter writer) => new MermaidWriter(writer); | ||
|
||
protected readonly TextWriter Writer; | ||
|
||
protected GraphWriter(TextWriter writer) => Writer = writer; | ||
|
||
public void Write(DependencyGraph graph, GraphDirection graphDirection, bool writeIgnoredPackages) | ||
{ | ||
WriteHeader(graphDirection); | ||
WriteEdges(graph, writeIgnoredPackages); | ||
Writer.WriteLine(); | ||
WriteNodes(graph, writeIgnoredPackages); | ||
WriteFooter(); | ||
} | ||
|
||
protected abstract void WriteHeader(GraphDirection graphDirection); | ||
protected abstract void WriteFooter(); | ||
protected abstract void WriteNode(Package package); | ||
protected abstract void WriteEdge(Package package, Package dependency); | ||
|
||
private static bool FilterIgnored(Package package, bool writeIgnoredPackages) => writeIgnoredPackages || package.State != PackageState.Ignore; | ||
|
||
private void WriteNodes(DependencyGraph graph, bool writeIgnoredPackages) | ||
{ | ||
foreach (var package in graph.Packages.Where(e => FilterIgnored(e, writeIgnoredPackages)).OrderBy(e => e.Id)) | ||
{ | ||
WriteNode(package); | ||
} | ||
} | ||
|
||
private void WriteEdges(DependencyGraph graph, bool writeIgnoredPackages) | ||
{ | ||
foreach (var (package, dependencies) in graph.Dependencies.Select(e => (e.Key, e.Value)).Where(e => FilterIgnored(e.Key, writeIgnoredPackages)).OrderBy(e => e.Key.Id)) | ||
{ | ||
foreach (var dependency in dependencies.Where(e => FilterIgnored(e, writeIgnoredPackages)).OrderBy(e => e.Id)) | ||
{ | ||
WriteEdge(package, dependency); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.