Skip to content

Commit fae7751

Browse files
Consolidate
- Refactor - Change scope to be more appropriate - DRY - More tests
1 parent 9f4babc commit fae7751

10 files changed

Lines changed: 88 additions & 58 deletions

File tree

MermaidGraph.NET.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C33AF3DC-C3F9-45B6-828C-57CB2B60EC96}"
99
ProjectSection(SolutionItems) = preProject
1010
MermaidGraph.NET.md = MermaidGraph.NET.md
11+
.github\workflows\publish.yml = .github\workflows\publish.yml
1112
README.md = README.md
1213
EndProjectSection
1314
EndProject

mermaid-graph/Commands.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MermaidGraph.Diagrams;
1+
using MermaidGraph.Diagrams.Base;
22

33
namespace MermaidGraph;
44

@@ -14,7 +14,7 @@ public class Commands
1414
/// <param name="diagramType"></param>
1515
public static string Project(FileInfo file, DiagramType diagramType = DiagramType.Graph)
1616
{
17-
var graph = GetGraphType(diagramType);
17+
var graph = MermaidDiagram.GetDiagramType(diagramType);
1818

1919
return graph.Project(file);
2020
}
@@ -26,15 +26,8 @@ public static string Project(FileInfo file, DiagramType diagramType = DiagramTyp
2626
/// <param name="diagramType"></param>
2727
public static string Solution(FileInfo file, DiagramType diagramType = DiagramType.Graph)
2828
{
29-
var graph = GetGraphType(diagramType);
29+
var graph = MermaidDiagram.GetDiagramType(diagramType);
3030

3131
return graph.Solution(file);
3232
}
33-
34-
private static IMermaidDiagram GetGraphType(DiagramType diagramType) => diagramType switch
35-
{
36-
DiagramType.Class => new ClassDiagram(),
37-
DiagramType.Graph => new GraphDiagram(),
38-
_ => throw new NotImplementedException($"Option not supported: {diagramType}"),
39-
};
4033
}

mermaid-graph/Diagrams/DiagramType.cs renamed to mermaid-graph/Diagrams/Base/DiagramType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace MermaidGraph.Diagrams;
1+
namespace MermaidGraph.Diagrams.Base;
22

33
/// <summary>
44
/// Specifies the type of mermaid diagram to generate.

mermaid-graph/Diagrams/IMermaidDiagram.cs renamed to mermaid-graph/Diagrams/Base/IMermaidDiagram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace MermaidGraph.Diagrams;
1+
namespace MermaidGraph.Diagrams.Base;
22

33
/// <summary>
44
/// This file defines the IMermaidDiagram interface and the MermaidDiagram abstract class.

mermaid-graph/Diagrams/MermaidDiagram.cs renamed to mermaid-graph/Diagrams/Base/MermaidDiagram.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System.Text;
2+
using Microsoft.Build.Evaluation;
23
using Microsoft.Build.Locator;
34

4-
namespace MermaidGraph.Diagrams;
5+
namespace MermaidGraph.Diagrams.Base;
56

67
/// <summary>
78
/// The MermaidDiagram abstract class implements shared functionality for Mermaid diagram generation,
@@ -19,7 +20,7 @@ public abstract class MermaidDiagram : IMermaidDiagram
1920
/// </summary>
2021
public const string MermaidBegin = Fence + "mermaid";
2122

22-
internal readonly StringBuilder Graph = new();
23+
internal readonly StringBuilder Graph = new(256);
2324

2425
/// <summary>
2526
/// Initialize the MermaidDiagram class and ensure MSBuild is registered.
@@ -32,14 +33,27 @@ protected MermaidDiagram()
3233
}
3334
}
3435

36+
/// <summary>
37+
/// Factory method to get the appropriate graph type based on the provided DiagramType enum value.
38+
/// </summary>
39+
/// <param name="diagramType">The type of graph to generate.</param>
40+
/// <returns>The appropriate methods for generating a diagram of that type.</returns>
41+
/// <exception cref="NotImplementedException">If an enum type is added without corresponding diagram class.</exception>
42+
public static IMermaidDiagram GetDiagramType(DiagramType diagramType) => diagramType switch
43+
{
44+
DiagramType.Class => new ClassDiagram(),
45+
DiagramType.Graph => new GraphDiagram(),
46+
_ => throw new NotImplementedException($"Option not supported: {diagramType}"),
47+
};
48+
3549
/// <summary>
3650
/// Initialize the graph output.
3751
/// </summary>
3852
public virtual void Header(string title)
3953
{
4054
Graph.Clear();
41-
Graph.AppendLine(MermaidBegin);
4255
Graph.AppendLine($"""
56+
{MermaidBegin}
4357
---
4458
title: {title}
4559
config:
@@ -56,8 +70,25 @@ public virtual void Header(string title)
5670
public override string ToString() => Graph.ToString();
5771

5872
/// <inheritdoc />
59-
public abstract string Project(FileInfo file);
73+
public virtual string Project(FileInfo file)
74+
{
75+
Header(file.Name);
76+
using var projectCollection = new ProjectCollection();
77+
var project = projectCollection.LoadProject(file.FullName);
78+
GraphProject(project);
79+
Graph.AppendLine(Fence);
80+
81+
projectCollection.UnloadAllProjects();
82+
83+
return Graph.ToString();
84+
}
6085

6186
/// <inheritdoc />
6287
public abstract string Solution(FileInfo file);
88+
89+
/// <summary>
90+
/// This method must be implemented in all derived classes to generate the graph for a project.
91+
/// </summary>
92+
/// <param name="project">A project to graph.</param>
93+
internal abstract void GraphProject(Project project);
6394
}

mermaid-graph/Diagrams/ClassDiagram.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using Microsoft.Build.Construction;
1+
using MermaidGraph.Diagrams.Base;
2+
using Microsoft.Build.Construction;
23
using Microsoft.Build.Evaluation;
34

45
namespace MermaidGraph.Diagrams;
56

6-
internal class ClassDiagram : MermaidDiagram
7+
/// <summary>
8+
/// Generates a Mermaid class diagram.
9+
/// </summary>
10+
public sealed class ClassDiagram : MermaidDiagram
711
{
812
/// <inheritdoc />
913
public override void Header(string title)
@@ -12,23 +16,6 @@ public override void Header(string title)
1216
Graph.AppendLine("classDiagram");
1317
}
1418

15-
/// <summary>
16-
/// Generate the dependency graph of a Visual Studio Project.
17-
/// </summary>
18-
/// <param name="file">`.csproj` file.</param>
19-
public override string Project(FileInfo file)
20-
{
21-
Header(file.Name);
22-
using var projectCollection = new ProjectCollection();
23-
var project = projectCollection.LoadProject(file.FullName);
24-
GraphProject(project);
25-
Graph.AppendLine(Fence);
26-
27-
projectCollection.UnloadAllProjects();
28-
29-
return Graph.ToString();
30-
}
31-
3219
/// <summary>
3320
/// Generate the dependency graph of a Visual Studio Solution.
3421
/// </summary>
@@ -69,7 +56,7 @@ type solution
6956
return Graph.ToString();
7057
}
7158

72-
private void GraphProject(Project project)
59+
internal override void GraphProject(Project project)
7360
{
7461
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
7562
var type = project.GetPropertyValue("OutputType");

mermaid-graph/Diagrams/GraphDiagram.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using Microsoft.Build.Construction;
1+
using MermaidGraph.Diagrams.Base;
2+
using Microsoft.Build.Construction;
23
using Microsoft.Build.Evaluation;
34

45
namespace MermaidGraph.Diagrams;
56

6-
internal class GraphDiagram : MermaidDiagram
7+
/// <summary>
8+
/// Generates a Mermaid dependency graph for Visual Studio projects and solutions.
9+
/// </summary>
10+
public sealed class GraphDiagram : MermaidDiagram
711
{
812
/// <inheritdoc />
913
public override void Header(string title)
@@ -12,23 +16,6 @@ public override void Header(string title)
1216
Graph.AppendLine("graph TD");
1317
}
1418

15-
/// <summary>
16-
/// Generate the dependency graph of a Visual Studio Project.
17-
/// </summary>
18-
/// <param name="file">`.csproj` file.</param>
19-
public override string Project(FileInfo file)
20-
{
21-
Header(file.Name);
22-
using var projectCollection = new ProjectCollection();
23-
var project = projectCollection.LoadProject(file.FullName);
24-
GraphProject(project);
25-
Graph.AppendLine(Fence);
26-
27-
projectCollection.UnloadAllProjects();
28-
29-
return Graph.ToString();
30-
}
31-
3219
/// <summary>
3320
/// Generate the dependency graph of a Visual Studio Solution.
3421
/// </summary>
@@ -64,7 +51,7 @@ public override string Solution(FileInfo file)
6451
return Graph.ToString();
6552
}
6653

67-
private void GraphProject(Project project)
54+
internal override void GraphProject(Project project)
6855
{
6956
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
7057

mermaid-graph/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MermaidGraph.Diagrams;
1+
using MermaidGraph.Diagrams.Base;
22

33
namespace MermaidGraph;
44

mermaid-graphTests/CommandsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MermaidGraph.Diagrams;
1+
using MermaidGraph.Diagrams.Base;
22
using Microsoft.ClearScript.V8;
33
using NUnit.Framework;
44
using Assert = NUnit.Framework.Assert;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using MermaidGraph.Diagrams;
2+
using MermaidGraph.Diagrams.Base;
3+
using NUnit.Framework;
4+
using Assert = NUnit.Framework.Assert;
5+
6+
namespace MermaidGraph.Tests;
7+
8+
[TestFixture]
9+
public class MermaidDiagramTests
10+
{
11+
[Test]
12+
public void GetDiagramType_ShouldReturnCorrectInstance()
13+
{
14+
// Arrange & Act
15+
var classDiagram = MermaidDiagram.GetDiagramType(DiagramType.Class);
16+
var graphDiagram = MermaidDiagram.GetDiagramType(DiagramType.Graph);
17+
18+
// Assert
19+
Assert.That(classDiagram, Is.TypeOf<ClassDiagram>());
20+
Assert.That(graphDiagram, Is.TypeOf<GraphDiagram>());
21+
}
22+
23+
[Test]
24+
public void GetDiagramType_ShouldThrowForUnsupportedType()
25+
{
26+
// Arrange & Act
27+
Assert.Throws<NotImplementedException>(()=>
28+
MermaidDiagram.GetDiagramType((DiagramType)999));
29+
}
30+
}
31+

0 commit comments

Comments
 (0)