Skip to content

Commit b3ded83

Browse files
Merge pull request #3 from A9G-Data-Droid/DiagramTypes
v1.0.0 Release Candidate
2 parents 7712503 + 9f4babc commit b3ded83

File tree

10 files changed

+364
-144
lines changed

10 files changed

+364
-144
lines changed

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@ Usage:
1919
mermaid-graph [options]
2020
2121
Options:
22-
--path <path> Full path to the solution (*.sln) or project (*.csproj) file that will be mapped.
23-
--version Show version information
24-
-?, -h, --help Show help and usage information
25-
```
22+
--path <path> Full path to the solution (*.sln) or project (*.csproj) file that will be mapped.
23+
--type <Class|Graph> The type of diagram to generate (e.g., Graph or Class). [default: Graph]
24+
--version Show version information
25+
-?, -h, --help Show help and usage information
26+
```
2627

2728
## Example output from this solution
2829

30+
You can run the following command to generate a class diagram for this solution:
31+
32+
```powershell
33+
.\mermaid-graph.exe --path "MermaidGraph.NET.sln" --type Class
34+
```
35+
36+
This will generate a mermaid graph in the console output, which can be piped to a file and used in a markdown document.
37+
2938
```mermaid
3039
---
3140
title: MermaidGraph.NET.sln
@@ -78,6 +87,16 @@ classDiagram
7887
version 6.0.4
7988
}
8089
MermaidGraphTests ..> coverlet.msbuild
90+
class Microsoft.ClearScript.V8{
91+
type NuGet
92+
version 7.4.5
93+
}
94+
MermaidGraphTests ..> Microsoft.ClearScript.V8
95+
class Microsoft.ClearScript.V8.Native.win-x64{
96+
type NuGet
97+
version 7.4.5
98+
}
99+
MermaidGraphTests ..> Microsoft.ClearScript.V8.Native.win-x64
81100
class Microsoft.NET.Test.Sdk{
82101
type NuGet
83102
version 17.13.0

mermaid-graph/Commands.cs

Lines changed: 16 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,40 @@
1-
using System.Text;
2-
using Microsoft.Build.Construction;
3-
using Microsoft.Build.Evaluation;
4-
using Microsoft.Build.Locator;
1+
using MermaidGraph.Diagrams;
52

63
namespace MermaidGraph;
74

85
/// <summary>
9-
/// The commands that can be run by `mermaid-graph`
6+
/// The commands that can be run by `mermaid-graph`.
107
/// </summary>
118
public class Commands
129
{
13-
public const string MermaidBegin = Fence + "mermaid";
14-
public const string Fence = "```";
15-
16-
private readonly StringBuilder _graph;
17-
18-
/// <summary>
19-
/// Initialize the graph output
20-
/// </summary>
21-
public Commands()
22-
{
23-
_graph = new StringBuilder();
24-
25-
// Ensure MSBuild is registered
26-
if (!MSBuildLocator.IsRegistered)
27-
{
28-
MSBuildLocator.RegisterDefaults();
29-
}
30-
}
31-
3210
/// <summary>
3311
/// Generate the dependency graph of a Visual Studio Project.
3412
/// </summary>
3513
/// <param name="file">`.csproj` file.</param>
36-
public string Project(FileInfo file)
14+
/// <param name="diagramType"></param>
15+
public static string Project(FileInfo file, DiagramType diagramType = DiagramType.Graph)
3716
{
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;
17+
var graph = GetGraphType(diagramType);
18+
19+
return graph.Project(file);
5020
}
5121

5222
/// <summary>
5323
/// Generate the dependency graph of a Visual Studio Solution.
5424
/// </summary>
5525
/// <param name="file">`.sln` file.</param>
56-
public string Solution(FileInfo file)
26+
/// <param name="diagramType"></param>
27+
public static string Solution(FileInfo file, DiagramType diagramType = DiagramType.Graph)
5728
{
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();
29+
var graph = GetGraphType(diagramType);
6930

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");
31+
return graph.Solution(file);
10832
}
10933

110-
private void GraphProject(Project project)
34+
private static IMermaidDiagram GetGraphType(DiagramType diagramType) => diagramType switch
11135
{
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-
}
142-
}
36+
DiagramType.Class => new ClassDiagram(),
37+
DiagramType.Graph => new GraphDiagram(),
38+
_ => throw new NotImplementedException($"Option not supported: {diagramType}"),
39+
};
14340
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Microsoft.Build.Construction;
2+
using Microsoft.Build.Evaluation;
3+
4+
namespace MermaidGraph.Diagrams;
5+
6+
internal class ClassDiagram : MermaidDiagram
7+
{
8+
/// <inheritdoc />
9+
public override void Header(string title)
10+
{
11+
base.Header(title);
12+
Graph.AppendLine("classDiagram");
13+
}
14+
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+
32+
/// <summary>
33+
/// Generate the dependency graph of a Visual Studio Solution.
34+
/// </summary>
35+
/// <param name="file">`.sln` file.</param>
36+
public override string Solution(FileInfo file)
37+
{
38+
Header(file.Name);
39+
var solutionFile = SolutionFile.Parse(file.FullName);
40+
var solutionName = Path.GetFileNameWithoutExtension(file.Name);
41+
var solutionId = $"{solutionName}";
42+
Graph.AppendLine($$"""
43+
class {{solutionName}}{
44+
type solution
45+
}
46+
""");
47+
48+
using var projectCollection = new ProjectCollection();
49+
50+
foreach (var project in solutionFile.ProjectsInOrder)
51+
{
52+
if (project.ProjectType != SolutionProjectType.KnownToBeMSBuildFormat) continue;
53+
54+
var projectPath = project.AbsolutePath;
55+
var projectName = Path.GetFileNameWithoutExtension(projectPath);
56+
Graph.AppendLine($" {solutionId} --> {projectName}");
57+
var projectFile = new FileInfo(projectPath);
58+
if (projectFile.Exists)
59+
{
60+
var referenceProject = projectCollection.LoadProject(projectFile.FullName);
61+
GraphProject(referenceProject);
62+
}
63+
}
64+
65+
Graph.AppendLine(Fence);
66+
67+
projectCollection.UnloadAllProjects();
68+
69+
return Graph.ToString();
70+
}
71+
72+
private void GraphProject(Project project)
73+
{
74+
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
75+
var type = project.GetPropertyValue("OutputType");
76+
var targetFramework = project.GetPropertyValue("TargetFramework") ?? project.GetPropertyValue("TargetFrameworks");
77+
Graph.AppendLine($$"""
78+
class {{projectName}}{
79+
type {{type}}
80+
target {{targetFramework}}
81+
}
82+
""");
83+
84+
foreach (var item in project.GetItems("ProjectReference"))
85+
{
86+
var refPath = item.EvaluatedInclude;
87+
var refName = Path.GetFileNameWithoutExtension(refPath);
88+
Graph.AppendLine($" {projectName} ..> {refName}");
89+
}
90+
91+
foreach (var item in project.GetItems("PackageReference"))
92+
{
93+
var packageName = item.EvaluatedInclude;
94+
var version = item.GetMetadataValue("Version");
95+
Graph.AppendLine($$"""
96+
class {{packageName}}{
97+
type NuGet
98+
version {{version}}
99+
}
100+
""");
101+
102+
Graph.AppendLine($" {projectName} ..> {packageName}");
103+
}
104+
}
105+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MermaidGraph.Diagrams;
2+
3+
/// <summary>
4+
/// Specifies the type of mermaid diagram to generate.
5+
/// </summary>
6+
public enum DiagramType
7+
{
8+
/// <summary>
9+
/// Represents a general dependency graph.
10+
/// </summary>
11+
Graph,
12+
13+
/// <summary>
14+
/// Represents a class diagram.
15+
/// </summary>
16+
Class
17+
}

0 commit comments

Comments
 (0)