Skip to content

Commit bbd9d2a

Browse files
Implement --noNuget
1 parent b235ca2 commit bbd9d2a

10 files changed

Lines changed: 100 additions & 23 deletions

File tree

mermaid-graph/Commands.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ public class Commands
1010
/// <summary>
1111
/// Generate the dependency graph of a Visual Studio Project.
1212
/// </summary>
13-
public static string Project(FileInfo file, DiagramType diagramType = DiagramType.Graph, string? filter = null)
13+
public static string Project(FileInfo file, DiagramType diagramType = DiagramType.Graph, string? filter = null, bool excludeNuget = false)
1414
{
1515
var graph = MermaidDiagram.GetDiagramType(diagramType);
1616

17-
return graph.Project(file, filter);
17+
return graph.Project(file, filter, excludeNuget);
1818
}
1919

2020
/// <summary>
2121
/// Generate the dependency graph of a Visual Studio Solution.
2222
/// </summary>
23-
public static string Solution(FileInfo file, DiagramType diagramType = DiagramType.Graph, string? filter = null)
23+
public static string Solution(FileInfo file, DiagramType diagramType = DiagramType.Graph, string? filter = null, bool excludeNuget = false)
2424
{
2525
var graph = MermaidDiagram.GetDiagramType(diagramType);
2626

27-
return graph.Solution(file, filter);
27+
return graph.Solution(file, filter, excludeNuget);
2828
}
2929
}

mermaid-graph/Diagrams/Base/IMermaidDiagram.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ public interface IMermaidDiagram
1212
/// </summary>
1313
/// <param name="file">The project file</param>
1414
/// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test)</param>
15+
/// <param name="excludeNuget">Do not include NuGet packages in the graph</param>
1516
/// <returns>Mermaid Markdown</returns>
16-
public string Project(FileInfo file, string? filter = null);
17+
public string Project(FileInfo file, string? filter = null, bool excludeNuget = false);
1718

1819
/// <summary>
1920
/// Generate the diagram from a visual studio solution file (*.sln)
2021
/// </summary>
2122
/// <param name="file">The solution file.</param>
2223
/// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test)</param>
24+
/// <param name="excludeNuget">Do not include NuGet packages in the graph</param>
2325
/// <returns>Mermaid Markdown</returns>
24-
public string Solution(FileInfo file, string? filter = null);
26+
public string Solution(FileInfo file, string? filter = null, bool excludeNuget = false);
2527
}

mermaid-graph/Diagrams/Base/MermaidDiagram.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ public virtual void Header(string title)
7070
public override string ToString() => Graph.ToString();
7171

7272
/// <inheritdoc />
73-
public virtual string Project(FileInfo file, string? filter = null)
73+
public virtual string Project(FileInfo file, string? filter = null, bool excludeNuget = false)
7474
{
7575
Header(file.Name);
7676
using var projectCollection = new ProjectCollection();
7777
var project = projectCollection.LoadProject(file.FullName);
78-
GraphProject(project, filter);
78+
GraphProject(project, filter, excludeNuget);
7979
Graph.AppendLine(Fence);
8080

8181
projectCollection.UnloadAllProjects();
@@ -84,12 +84,13 @@ public virtual string Project(FileInfo file, string? filter = null)
8484
}
8585

8686
/// <inheritdoc />
87-
public abstract string Solution(FileInfo file, string? filter = null);
87+
public abstract string Solution(FileInfo file, string? filter = null, bool excludeNuget = false);
8888

8989
/// <summary>
9090
/// This method must be implemented in all derived classes to generate the graph for a project.
9191
/// </summary>
9292
/// <param name="project">A project to graph.</param>
9393
/// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test)</param>
94-
internal abstract void GraphProject(Project project, string? filter = null);
94+
/// <param name="excludeNuget">Do not include NuGet packages in the graph</param>
95+
internal abstract void GraphProject(Project project, string? filter = null, bool excludeNuget = false);
9596
}

mermaid-graph/Diagrams/ClassDiagram.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public override void Header(string title)
1717
}
1818

1919
/// <inheritdoc />
20-
public override string Solution(FileInfo file, string? filter = null)
20+
public override string Solution(FileInfo file, string? filter = null, bool excludeNuget = false)
2121
{
2222
Header(file.Name);
2323
var solutionFile = SolutionFile.Parse(file.FullName);
@@ -46,7 +46,7 @@ type solution
4646
if (projectFile.Exists)
4747
{
4848
var referenceProject = projectCollection.LoadProject(projectFile.FullName);
49-
GraphProject(referenceProject, filter);
49+
GraphProject(referenceProject, filter, excludeNuget);
5050
}
5151
}
5252

@@ -57,7 +57,7 @@ type solution
5757
return Graph.ToString();
5858
}
5959

60-
internal override void GraphProject(Project project, string? filter = null)
60+
internal override void GraphProject(Project project, string? filter = null, bool excludeNuget = false)
6161
{
6262
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
6363
var type = project.GetPropertyValue("OutputType");
@@ -82,6 +82,7 @@ class {{projectName}}{
8282
Graph.AppendLine($" {projectName} ..> {refName}");
8383
}
8484

85+
if (excludeNuget) return;
8586
foreach (var item in project.GetItems("PackageReference"))
8687
{
8788
var packageName = item.EvaluatedInclude;

mermaid-graph/Diagrams/GraphDiagram.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public override void Header(string title)
1717
}
1818

1919
/// <inheritdoc />
20-
public override string Solution(FileInfo file, string? filter = null)
20+
public override string Solution(FileInfo file, string? filter = null, bool excludeNuget = false)
2121
{
2222
Header(file.Name);
2323
var solutionFile = SolutionFile.Parse(file.FullName);
@@ -41,7 +41,7 @@ public override string Solution(FileInfo file, string? filter = null)
4141
if (projectFile.Exists)
4242
{
4343
var referenceProject = projectCollection.LoadProject(projectFile.FullName);
44-
GraphProject(referenceProject, filter);
44+
GraphProject(referenceProject, filter, excludeNuget);
4545
}
4646
}
4747

@@ -52,7 +52,7 @@ public override string Solution(FileInfo file, string? filter = null)
5252
return Graph.ToString();
5353
}
5454

55-
internal override void GraphProject(Project project, string? filter = null)
55+
internal override void GraphProject(Project project, string? filter = null, bool excludeNuget = false)
5656
{
5757
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
5858

@@ -67,6 +67,7 @@ internal override void GraphProject(Project project, string? filter = null)
6767
Graph.AppendLine($" {projectName} --> {refName}");
6868
}
6969

70+
if (excludeNuget) return;
7071
foreach (var item in project.GetItems("PackageReference"))
7172
{
7273
var packageName = item.EvaluatedInclude;

mermaid-graph/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ public sealed class Program
1212
/// </summary>
1313
/// <param name="path">Full path to the solution (*.sln) or project (*.csproj) file that will be mapped.</param>
1414
/// <param name="type">The type of diagram to generate (e.g., Graph or Class).</param>
15-
/// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test)</param>
15+
/// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test).</param>
16+
/// <param name="noNuget">Do not include NuGet packages in the graph.</param>
1617
/// <returns>HResult</returns>
17-
public static int Main(string? path, DiagramType type = DiagramType.Graph, string? filter = null)
18+
public static int Main(string? path, DiagramType type = DiagramType.Graph, string? filter = null, bool noNuget = false)
1819
{
1920
if (path is null)
2021
{
@@ -35,13 +36,13 @@ public static int Main(string? path, DiagramType type = DiagramType.Graph, strin
3536
{
3637
if (path.EndsWith(".csproj"))
3738
{
38-
Console.WriteLine(Commands.Project(file, type, filter));
39+
Console.WriteLine(Commands.Project(file, type, filter, noNuget));
3940
return 0;
4041
}
4142

4243
if (path.EndsWith(".sln"))
4344
{
44-
Console.WriteLine(Commands.Solution(file, type, filter));
45+
Console.WriteLine(Commands.Solution(file, type, filter, noNuget));
4546
return 0;
4647
}
4748
}

mermaid-graph/mermaid-graph.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
<ItemGroup>
6666
<PackageReference Include="Microsoft.Build" Version="17.13.9" ExcludeAssets="runtime" />
6767
<PackageReference Include="Microsoft.Build.Locator" Version="1.9.1" />
68-
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.13.9" ExcludeAssets="runtime" />
6968
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
7069
</ItemGroup>
7170

mermaid-graphTests/CommandsTests.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using MermaidGraph.Diagrams.Base;
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using MermaidGraph.Diagrams.Base;
25
using Microsoft.ClearScript.V8;
36
using NUnit.Framework;
47
using Assert = NUnit.Framework.Assert;
@@ -126,6 +129,63 @@ public void DiagramsShouldNotContainFilteredContent(DiagramType type, string fil
126129
$"Graph should not contain filtered content: {filter}");
127130
}
128131

132+
[Test]
133+
[TestCase(DiagramType.Graph)]
134+
[TestCase(DiagramType.Class)]
135+
public void Project_ShouldExcludeNuget_WhenExcludeNugetIsTrue(DiagramType type)
136+
{
137+
var filePath = FindFileDownTree("*.csproj");
138+
Assert.That(filePath, Is.Not.Null);
139+
var info = new FileInfo(filePath!);
140+
Assert.That(info.Exists);
141+
142+
var graph = Commands.Project(info, type, excludeNuget: true);
143+
Assert.That(graph, Does.Not.Contain("NuGet"), "Graph should not contain NuGet references when noNuget is true.");
144+
}
145+
146+
[Test]
147+
[TestCase(DiagramType.Graph)]
148+
[TestCase(DiagramType.Class)]
149+
public void Solution_ShouldExcludeNuget_WhenExcludeNugetIsTrue(DiagramType type)
150+
{
151+
var solutionPath = FindFileDownTree("*.sln");
152+
Assert.That(solutionPath, Is.Not.Null);
153+
var info = new FileInfo(solutionPath!);
154+
Assert.That(info.Exists);
155+
156+
var graph = Commands.Solution(info, type, excludeNuget: true);
157+
Assert.That(graph, Does.Not.Contain("NuGet"), "Graph should not contain NuGet references when noNuget is true.");
158+
}
159+
160+
[Test]
161+
[TestCase(DiagramType.Graph)]
162+
[TestCase(DiagramType.Class)]
163+
public void Project_ShouldIncludeNuget_WhenExcludeNugetIsFalse(DiagramType type)
164+
{
165+
var filePath = FindFileDownTree("*.csproj");
166+
Assert.That(filePath, Is.Not.Null);
167+
var info = new FileInfo(filePath!);
168+
Assert.That(info.Exists);
169+
170+
var graph = Commands.Project(info, type, excludeNuget: false);
171+
Assert.That(graph, Does.Contain("NuGet"), "Graph should contain NuGet references when noNuget is false.");
172+
}
173+
174+
[Test]
175+
[TestCase(DiagramType.Graph)]
176+
[TestCase(DiagramType.Class)]
177+
public void Solution_ShouldIncludeNuget_WhenExcludeNugetIsFalse(DiagramType type)
178+
{
179+
var solutionPath = FindFileDownTree("*.sln");
180+
Assert.That(solutionPath, Is.Not.Null);
181+
var info = new FileInfo(solutionPath!);
182+
Assert.That(info.Exists);
183+
184+
var graph = Commands.Solution(info, type, excludeNuget: false);
185+
Assert.That(graph, Does.Contain("NuGet"), "Graph should contain NuGet references when noNuget is false.");
186+
}
187+
188+
129189
private static string ExtractMermaid(string? markup)
130190
{
131191
Assert.That(markup, Does.StartWith(MermaidDiagram.MermaidBegin));

mermaid-graphTests/MermaidDiagramTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using MermaidGraph.Diagrams;
23
using MermaidGraph.Diagrams.Base;
34
using NUnit.Framework;

mermaid-graphTests/MermaidGraphTests.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44
<TargetFramework>net9.0</TargetFramework>
55
<RootNamespace>MermaidGraph.Tests</RootNamespace>
66
<LangVersion>latest</LangVersion>
7-
<ImplicitUsings>enable</ImplicitUsings>
7+
<ImplicitUsings>disable</ImplicitUsings>
88
<Nullable>enable</Nullable>
9+
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
10+
</PropertyGroup>
11+
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
13+
<WarningLevel>8</WarningLevel>
14+
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
15+
</PropertyGroup>
16+
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
18+
<WarningLevel>8</WarningLevel>
19+
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
920
</PropertyGroup>
1021

1122
<ItemGroup>

0 commit comments

Comments
 (0)