Skip to content

Commit 7272e85

Browse files
switch to class diagram
- 97% test coverage
1 parent 5ccbc1e commit 7272e85

File tree

7 files changed

+365
-36
lines changed

7 files changed

+365
-36
lines changed

MermaidGraph.NET.md

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,83 @@
11
```mermaid
22
---
33
title: MermaidGraph.NET.sln
4+
config:
5+
class:
6+
hideEmptyMembersBox: true
47
---
5-
graph TD
6-
s34640832(MermaidGraph.NET) --> mermaid-graph
7-
mermaid-graph -->|NuGet| Microsoft.Build
8-
mermaid-graph -->|NuGet| Microsoft.Build.Locator
9-
mermaid-graph -->|NuGet| Microsoft.Build.Utilities.Core
10-
mermaid-graph -->|NuGet| System.CommandLine.DragonFruit
11-
```
8+
classDiagram
9+
class MermaidGraph.NET{
10+
type solution
11+
}
12+
MermaidGraph.NET --> mermaid-graph
13+
class mermaid-graph{
14+
type Exe
15+
target net9.0
16+
}
17+
class Microsoft.Build{
18+
type NuGet
19+
version 17.12.6
20+
}
21+
mermaid-graph ..> Microsoft.Build
22+
class Microsoft.Build.Locator{
23+
type NuGet
24+
version 1.7.8
25+
}
26+
mermaid-graph ..> Microsoft.Build.Locator
27+
class Microsoft.Build.Utilities.Core{
28+
type NuGet
29+
version 17.12.6
30+
}
31+
mermaid-graph ..> Microsoft.Build.Utilities.Core
32+
class System.CommandLine.DragonFruit{
33+
type NuGet
34+
version 0.4.0-alpha.22272.1
35+
}
36+
mermaid-graph ..> System.CommandLine.DragonFruit
37+
MermaidGraph.NET --> MermaidGraphTests
38+
class MermaidGraphTests{
39+
type Exe
40+
target net9.0
41+
}
42+
MermaidGraphTests ..> mermaid-graph
43+
class coverlet.collector{
44+
type NuGet
45+
version 6.0.4
46+
}
47+
MermaidGraphTests ..> coverlet.collector
48+
class coverlet.msbuild{
49+
type NuGet
50+
version 6.0.4
51+
}
52+
MermaidGraphTests ..> coverlet.msbuild
53+
class Microsoft.NET.Test.Sdk{
54+
type NuGet
55+
version 17.13.0
56+
}
57+
MermaidGraphTests ..> Microsoft.NET.Test.Sdk
58+
class MSTest.TestAdapter{
59+
type NuGet
60+
version 3.7.3
61+
}
62+
MermaidGraphTests ..> MSTest.TestAdapter
63+
class MSTest.TestFramework{
64+
type NuGet
65+
version 3.7.3
66+
}
67+
MermaidGraphTests ..> MSTest.TestFramework
68+
class NUnit{
69+
type NuGet
70+
version 4.3.2
71+
}
72+
MermaidGraphTests ..> NUnit
73+
class NUnit.Analyzers{
74+
type NuGet
75+
version 4.6.0
76+
}
77+
MermaidGraphTests ..> NUnit.Analyzers
78+
class NUnit3TestAdapter{
79+
type NuGet
80+
version 4.3.2
81+
}
82+
MermaidGraphTests ..> NUnit3TestAdapter
83+
```

MermaidGraph.NET.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1111
README.md = README.md
1212
EndProjectSection
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MermaidGraphTests", "mermaid-graphTests\MermaidGraphTests.csproj", "{FADD9D23-431E-4C1B-B2D4-D4FDF0674FCA}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{BDC56E20-7574-4734-B3F2-DD75C08B2FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{BDC56E20-7574-4734-B3F2-DD75C08B2FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{BDC56E20-7574-4734-B3F2-DD75C08B2FBC}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{FADD9D23-431E-4C1B-B2D4-D4FDF0674FCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{FADD9D23-431E-4C1B-B2D4-D4FDF0674FCA}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{FADD9D23-431E-4C1B-B2D4-D4FDF0674FCA}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{FADD9D23-431E-4C1B-B2D4-D4FDF0674FCA}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

README.md

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,110 @@
1+
![MermaidGraph.NET](mermaid-graph.png "MermaidGraph.NET")
2+
13
# MermaidGraph.NET
2-
Create a mermaid graph of the dependency diagram for a project, or whole solution.
4+
Create a mermaid graph of the dependency diagram for a project, or whole solution.
5+
6+
## Dotnet tool [![NuGet version (mermaid-graph)](https://img.shields.io/nuget/v/mermaid-graph.svg?style=flat-square)](https://www.nuget.org/packages/mermaid-graph/)
7+
8+
You can install as a dotnet tool so you can easily map all of your software projects:
9+
10+
`dotnet tool install --global mermaid-graph`
11+
12+
## Usage
13+
```
14+
Description:
15+
Outputs a mermaid graph of the dependency diagram for a project, or whole solution.
16+
17+
Usage:
18+
mermaid-graph [options]
19+
20+
Options:
21+
--path <path> Full path to the solution (*.sln) or project (*.csproj) file that will be mapped.
22+
--version Show version information
23+
-?, -h, --help Show help and usage information
24+
```
325

4-
## Example
26+
## Example output from this solution
527

628
```mermaid
729
---
830
title: MermaidGraph.NET.sln
31+
config:
32+
class:
33+
hideEmptyMembersBox: true
934
---
10-
graph TD
11-
s34640832(MermaidGraph.NET) --> mermaid-graph
12-
mermaid-graph -->|NuGet| Microsoft.Build
13-
mermaid-graph -->|NuGet| Microsoft.Build.Locator
14-
mermaid-graph -->|NuGet| Microsoft.Build.Utilities.Core
15-
mermaid-graph -->|NuGet| System.CommandLine.DragonFruit
16-
```
35+
classDiagram
36+
class MermaidGraph.NET{
37+
type solution
38+
}
39+
MermaidGraph.NET --> mermaid-graph
40+
class mermaid-graph{
41+
type Exe
42+
target net9.0
43+
}
44+
class Microsoft.Build{
45+
type NuGet
46+
version 17.12.6
47+
}
48+
mermaid-graph ..> Microsoft.Build
49+
class Microsoft.Build.Locator{
50+
type NuGet
51+
version 1.7.8
52+
}
53+
mermaid-graph ..> Microsoft.Build.Locator
54+
class Microsoft.Build.Utilities.Core{
55+
type NuGet
56+
version 17.12.6
57+
}
58+
mermaid-graph ..> Microsoft.Build.Utilities.Core
59+
class System.CommandLine.DragonFruit{
60+
type NuGet
61+
version 0.4.0-alpha.22272.1
62+
}
63+
mermaid-graph ..> System.CommandLine.DragonFruit
64+
MermaidGraph.NET --> MermaidGraphTests
65+
class MermaidGraphTests{
66+
type Exe
67+
target net9.0
68+
}
69+
MermaidGraphTests ..> mermaid-graph
70+
class coverlet.collector{
71+
type NuGet
72+
version 6.0.4
73+
}
74+
MermaidGraphTests ..> coverlet.collector
75+
class coverlet.msbuild{
76+
type NuGet
77+
version 6.0.4
78+
}
79+
MermaidGraphTests ..> coverlet.msbuild
80+
class Microsoft.NET.Test.Sdk{
81+
type NuGet
82+
version 17.13.0
83+
}
84+
MermaidGraphTests ..> Microsoft.NET.Test.Sdk
85+
class MSTest.TestAdapter{
86+
type NuGet
87+
version 3.7.3
88+
}
89+
MermaidGraphTests ..> MSTest.TestAdapter
90+
class MSTest.TestFramework{
91+
type NuGet
92+
version 3.7.3
93+
}
94+
MermaidGraphTests ..> MSTest.TestFramework
95+
class NUnit{
96+
type NuGet
97+
version 4.3.2
98+
}
99+
MermaidGraphTests ..> NUnit
100+
class NUnit.Analyzers{
101+
type NuGet
102+
version 4.6.0
103+
}
104+
MermaidGraphTests ..> NUnit.Analyzers
105+
class NUnit3TestAdapter{
106+
type NuGet
107+
version 4.3.2
108+
}
109+
MermaidGraphTests ..> NUnit3TestAdapter
110+
```

mermaid-graph/Commands.cs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text;
22
using Microsoft.Build.Construction;
33
using Microsoft.Build.Evaluation;
4+
using Microsoft.Build.Locator;
45

56
namespace MermaidGraph;
67

@@ -19,6 +20,12 @@ public class Commands
1920
public Commands()
2021
{
2122
_graph = new StringBuilder();
23+
24+
// Ensure MSBuild is registered
25+
if (!MSBuildLocator.IsRegistered)
26+
{
27+
MSBuildLocator.RegisterDefaults();
28+
}
2229
}
2330

2431
/// <summary>
@@ -28,10 +35,16 @@ public Commands()
2835
public string Project(FileInfo file)
2936
{
3037
Header(file.Name);
31-
GraphProject(file);
38+
using var projectCollection = new ProjectCollection();
39+
var project = projectCollection.LoadProject(file.FullName);
40+
GraphProject(project);
3241
_graph.AppendLine(Fence);
3342
var graph = _graph.ToString();
43+
44+
// Cleanup
3445
_graph.Clear();
46+
projectCollection.UnloadAllProjects();
47+
3548
return graph;
3649
}
3750

@@ -44,7 +57,15 @@ public string Solution(FileInfo file)
4457
Header(file.Name);
4558
var solutionFile = SolutionFile.Parse(file.FullName);
4659
var solutionName = Path.GetFileNameWithoutExtension(file.Name);
47-
var solutionId = $"s{solutionFile.GetHashCode()}({solutionName})";
60+
var solutionId = $"{solutionName}";
61+
_graph.AppendLine($$"""
62+
class {{solutionName}}{
63+
type solution
64+
}
65+
""");
66+
67+
using var projectCollection = new ProjectCollection();
68+
4869
foreach (var project in solutionFile.ProjectsInOrder)
4970
{
5071
if (project.ProjectType != SolutionProjectType.KnownToBeMSBuildFormat) continue;
@@ -55,13 +76,18 @@ public string Solution(FileInfo file)
5576
var projectFile = new FileInfo(projectPath);
5677
if (projectFile.Exists)
5778
{
58-
GraphProject(projectFile);
79+
var referenceProject = projectCollection.LoadProject(projectFile.FullName);
80+
GraphProject(referenceProject);
5981
}
6082
}
6183

6284
_graph.AppendLine(Fence);
6385
var graph = _graph.ToString();
86+
87+
// Cleanup
6488
_graph.Clear();
89+
projectCollection.UnloadAllProjects();
90+
6591
return graph;
6692
}
6793

@@ -71,28 +97,46 @@ private void Header(string title)
7197
_graph.AppendLine($"""
7298
---
7399
title: {title}
100+
config:
101+
class:
102+
hideEmptyMembersBox: true
74103
---
75104
""");
76105

77-
_graph.AppendLine("graph TD");
106+
_graph.AppendLine("classDiagram");
78107
}
79108

80-
private void GraphProject(FileInfo path)
109+
private void GraphProject(Project project)
81110
{
82-
var project = new Project(path.FullName);
83-
var projectName = Path.GetFileNameWithoutExtension(path.Name);
111+
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
112+
var type = project.GetPropertyValue("OutputType");
113+
var targetFramework = project.GetPropertyValue("TargetFramework") ?? project.GetPropertyValue("TargetFrameworks");
114+
_graph.AppendLine($$"""
115+
class {{projectName}}{
116+
type {{type}}
117+
target {{targetFramework}}
118+
}
119+
""");
84120

85121
foreach (var item in project.GetItems("ProjectReference"))
86122
{
87123
var refPath = item.EvaluatedInclude;
88124
var refName = Path.GetFileNameWithoutExtension(refPath);
89-
_graph.AppendLine($" {projectName} --> {refName}");
125+
_graph.AppendLine($" {projectName} ..> {refName}");
90126
}
91127

92128
foreach (var item in project.GetItems("PackageReference"))
93129
{
94130
var packageName = item.EvaluatedInclude;
95-
_graph.AppendLine($" {projectName} -->|NuGet| {packageName}");
131+
var version = item.GetMetadataValue("Version");
132+
_graph.AppendLine($$"""
133+
class {{packageName}}{
134+
type NuGet
135+
version {{version}}
136+
}
137+
""");
138+
139+
_graph.AppendLine($" {projectName} ..> {packageName}");
96140
}
97141
}
98142
}

mermaid-graph/Program.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
using Microsoft.Build.Locator;
2-
3-
namespace MermaidGraph;
1+
namespace MermaidGraph;
42

53
// ReSharper disable UnusedMember.Global
64

75
/// <summary>
86
/// mermaid-graph.exe
97
/// </summary>
10-
internal sealed class Program
8+
public sealed class Program
119
{
1210
/// <summary>
1311
/// Outputs a mermaid graph of the dependency diagram for a project, or whole solution.
1412
/// </summary>
1513
/// <param name="path">Full path to the solution (*.sln) or project (*.csproj) file that will be mapped.</param>
1614
/// <returns>HResult</returns>
17-
internal static int Main(string? path)
15+
public static int Main(string? path)
1816
{
1917
if (path is null)
2018
{
@@ -31,12 +29,6 @@ internal static int Main(string? path)
3129

3230
try
3331
{
34-
// Ensure MSBuild is registered
35-
if (!MSBuildLocator.IsRegistered)
36-
{
37-
MSBuildLocator.RegisterDefaults();
38-
}
39-
4032
if (path.EndsWith(".csproj"))
4133
{
4234
Console.WriteLine(new Commands().Project(file));

0 commit comments

Comments
 (0)