Skip to content

Commit b73c335

Browse files
Refactor
Getting ready for multiple render types
1 parent 7712503 commit b73c335

4 files changed

Lines changed: 156 additions & 113 deletions

File tree

mermaid-graph/Commands.cs

Lines changed: 6 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
using Microsoft.Build.Construction;
3-
using Microsoft.Build.Evaluation;
1+
using MermaidGraph.Diagrams;
42
using Microsoft.Build.Locator;
53

64
namespace MermaidGraph;
@@ -10,18 +8,9 @@ namespace MermaidGraph;
108
/// </summary>
119
public class Commands
1210
{
13-
public const string MermaidBegin = Fence + "mermaid";
14-
public const string Fence = "```";
1511

16-
private readonly StringBuilder _graph;
17-
18-
/// <summary>
19-
/// Initialize the graph output
20-
/// </summary>
2112
public Commands()
2213
{
23-
_graph = new StringBuilder();
24-
2514
// Ensure MSBuild is registered
2615
if (!MSBuildLocator.IsRegistered)
2716
{
@@ -35,18 +24,9 @@ public Commands()
3524
/// <param name="file">`.csproj` file.</param>
3625
public string Project(FileInfo file)
3726
{
38-
Header(file.Name);
39-
using var projectCollection = new ProjectCollection();
40-
var project = projectCollection.LoadProject(file.FullName);
41-
GraphProject(project);
42-
_graph.AppendLine(Fence);
43-
var graph = _graph.ToString();
44-
45-
// Cleanup
46-
_graph.Clear();
47-
projectCollection.UnloadAllProjects();
48-
49-
return graph;
27+
var graph = new ClassDiagram();
28+
29+
return graph.Project(file);
5030
}
5131

5232
/// <summary>
@@ -55,89 +35,8 @@ public string Project(FileInfo file)
5535
/// <param name="file">`.sln` file.</param>
5636
public string Solution(FileInfo file)
5737
{
58-
Header(file.Name);
59-
var solutionFile = SolutionFile.Parse(file.FullName);
60-
var solutionName = Path.GetFileNameWithoutExtension(file.Name);
61-
var solutionId = $"{solutionName}";
62-
_graph.AppendLine($$"""
63-
class {{solutionName}}{
64-
type solution
65-
}
66-
""");
67-
68-
using var projectCollection = new ProjectCollection();
38+
var graph = new ClassDiagram();
6939

70-
foreach (var project in solutionFile.ProjectsInOrder)
71-
{
72-
if (project.ProjectType != SolutionProjectType.KnownToBeMSBuildFormat) continue;
73-
74-
var projectPath = project.AbsolutePath;
75-
var projectName = Path.GetFileNameWithoutExtension(projectPath);
76-
_graph.AppendLine($" {solutionId} --> {projectName}");
77-
var projectFile = new FileInfo(projectPath);
78-
if (projectFile.Exists)
79-
{
80-
var referenceProject = projectCollection.LoadProject(projectFile.FullName);
81-
GraphProject(referenceProject);
82-
}
83-
}
84-
85-
_graph.AppendLine(Fence);
86-
var graph = _graph.ToString();
87-
88-
// Cleanup
89-
_graph.Clear();
90-
projectCollection.UnloadAllProjects();
91-
92-
return graph;
93-
}
94-
95-
private void Header(string title)
96-
{
97-
_graph.AppendLine(MermaidBegin);
98-
_graph.AppendLine($"""
99-
---
100-
title: {title}
101-
config:
102-
class:
103-
hideEmptyMembersBox: true
104-
---
105-
""");
106-
107-
_graph.AppendLine("classDiagram");
108-
}
109-
110-
private void GraphProject(Project project)
111-
{
112-
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
113-
var type = project.GetPropertyValue("OutputType");
114-
var targetFramework = project.GetPropertyValue("TargetFramework") ?? project.GetPropertyValue("TargetFrameworks");
115-
_graph.AppendLine($$"""
116-
class {{projectName}}{
117-
type {{type}}
118-
target {{targetFramework}}
119-
}
120-
""");
121-
122-
foreach (var item in project.GetItems("ProjectReference"))
123-
{
124-
var refPath = item.EvaluatedInclude;
125-
var refName = Path.GetFileNameWithoutExtension(refPath);
126-
_graph.AppendLine($" {projectName} ..> {refName}");
127-
}
128-
129-
foreach (var item in project.GetItems("PackageReference"))
130-
{
131-
var packageName = item.EvaluatedInclude;
132-
var version = item.GetMetadataValue("Version");
133-
_graph.AppendLine($$"""
134-
class {{packageName}}{
135-
type NuGet
136-
version {{version}}
137-
}
138-
""");
139-
140-
_graph.AppendLine($" {projectName} ..> {packageName}");
141-
}
40+
return graph.Solution(file);
14241
}
14342
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using Microsoft.Build.Construction;
2+
using Microsoft.Build.Evaluation;
3+
4+
namespace MermaidGraph.Diagrams;
5+
6+
internal class ClassDiagram : Diagram
7+
{
8+
public override void Header(string title)
9+
{
10+
base.Header(title);
11+
Graph.AppendLine("classDiagram");
12+
}
13+
14+
/// <summary>
15+
/// Generate the dependency graph of a Visual Studio Project.
16+
/// </summary>
17+
/// <param name="file">`.csproj` file.</param>
18+
public string Project(FileInfo file)
19+
{
20+
Header(file.Name);
21+
using var projectCollection = new ProjectCollection();
22+
var project = projectCollection.LoadProject(file.FullName);
23+
GraphProject(project);
24+
Graph.AppendLine(Fence);
25+
var graph = Graph.ToString();
26+
27+
// Cleanup
28+
Graph.Clear();
29+
projectCollection.UnloadAllProjects();
30+
31+
return graph;
32+
}
33+
34+
/// <summary>
35+
/// Generate the dependency graph of a Visual Studio Solution.
36+
/// </summary>
37+
/// <param name="file">`.sln` file.</param>
38+
public string Solution(FileInfo file)
39+
{
40+
Header(file.Name);
41+
var solutionFile = SolutionFile.Parse(file.FullName);
42+
var solutionName = Path.GetFileNameWithoutExtension(file.Name);
43+
var solutionId = $"{solutionName}";
44+
Graph.AppendLine($$"""
45+
class {{solutionName}}{
46+
type solution
47+
}
48+
""");
49+
50+
using var projectCollection = new ProjectCollection();
51+
52+
foreach (var project in solutionFile.ProjectsInOrder)
53+
{
54+
if (project.ProjectType != SolutionProjectType.KnownToBeMSBuildFormat) continue;
55+
56+
var projectPath = project.AbsolutePath;
57+
var projectName = Path.GetFileNameWithoutExtension(projectPath);
58+
Graph.AppendLine($" {solutionId} --> {projectName}");
59+
var projectFile = new FileInfo(projectPath);
60+
if (projectFile.Exists)
61+
{
62+
var referenceProject = projectCollection.LoadProject(projectFile.FullName);
63+
GraphProject(referenceProject);
64+
}
65+
}
66+
67+
Graph.AppendLine(Fence);
68+
var graph = Graph.ToString();
69+
70+
// Cleanup
71+
Graph.Clear();
72+
projectCollection.UnloadAllProjects();
73+
74+
return graph;
75+
}
76+
77+
private void GraphProject(Project project)
78+
{
79+
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
80+
var type = project.GetPropertyValue("OutputType");
81+
var targetFramework = project.GetPropertyValue("TargetFramework") ?? project.GetPropertyValue("TargetFrameworks");
82+
Graph.AppendLine($$"""
83+
class {{projectName}}{
84+
type {{type}}
85+
target {{targetFramework}}
86+
}
87+
""");
88+
89+
foreach (var item in project.GetItems("ProjectReference"))
90+
{
91+
var refPath = item.EvaluatedInclude;
92+
var refName = Path.GetFileNameWithoutExtension(refPath);
93+
Graph.AppendLine($" {projectName} ..> {refName}");
94+
}
95+
96+
foreach (var item in project.GetItems("PackageReference"))
97+
{
98+
var packageName = item.EvaluatedInclude;
99+
var version = item.GetMetadataValue("Version");
100+
Graph.AppendLine($$"""
101+
class {{packageName}}{
102+
type NuGet
103+
version {{version}}
104+
}
105+
""");
106+
107+
Graph.AppendLine($" {projectName} ..> {packageName}");
108+
}
109+
}
110+
}

mermaid-graph/Diagrams/Diagram.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Text;
2+
3+
namespace MermaidGraph.Diagrams;
4+
5+
public class Diagram
6+
{
7+
public const string Fence = "```";
8+
public const string MermaidBegin = Fence + "mermaid";
9+
10+
internal readonly StringBuilder Graph = new();
11+
12+
/// <summary>
13+
/// Initialize the graph output
14+
/// </summary>
15+
public virtual void Header(string title)
16+
{
17+
Graph.AppendLine(MermaidBegin);
18+
Graph.AppendLine($"""
19+
---
20+
title: {title}
21+
config:
22+
class:
23+
hideEmptyMembersBox: true
24+
---
25+
""");
26+
}
27+
28+
/// <summary>
29+
/// Get the mermaid diagram markdown text.
30+
/// </summary>
31+
/// <returns>The contents of the graph buffer.</returns>
32+
public override string ToString() => Graph.ToString();
33+
}

mermaid-graphTests/CommandsTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.ClearScript.V8;
1+
using MermaidGraph.Diagrams;
2+
using Microsoft.ClearScript.V8;
23
using NUnit.Framework;
34
using Assert = NUnit.Framework.Assert;
45

@@ -88,13 +89,13 @@ public void CommandLineFailTests(string? file, int hResult)
8889
Assert.That(Program.Main(file), Is.EqualTo(hResult));
8990
}
9091

91-
private static string ExtractMermaid(string markup)
92+
private static string ExtractMermaid(string? markup)
9293
{
93-
Assert.That(markup, Does.StartWith(Commands.MermaidBegin));
94-
markup = markup.Substring(Commands.MermaidBegin.Length + Environment.NewLine.Length);
94+
Assert.That(markup, Does.StartWith(Diagram.MermaidBegin));
95+
markup = markup.Substring(Diagram.MermaidBegin.Length + Environment.NewLine.Length);
9596

96-
Assert.That(markup, Does.EndWith(Commands.Fence + Environment.NewLine));
97-
return markup.Substring(0, markup.Length - Commands.MermaidBegin.Length + Environment.NewLine.Length);
97+
Assert.That(markup, Does.EndWith(Diagram.Fence + Environment.NewLine));
98+
return markup.Substring(0, markup.Length - Diagram.MermaidBegin.Length + Environment.NewLine.Length);
9899
}
99100

100101
private string? DetectType(string markup)

0 commit comments

Comments
 (0)