Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json

name: publish
permissions:
contents: read
pull-requests: write

on:
workflow_dispatch: # Allow running the workflow manually from the GitHub UI
push:
Expand Down
9 changes: 9 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Condition="!Exists('packages.config')">
<PrivateAssets>all</PrivateAssets>
<Version>3.7.115</Version>
</PackageReference>
</ItemGroup>
</Project>
25 changes: 18 additions & 7 deletions MermaidGraph.NET.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ classDiagram
}
class Microsoft.Build{
type NuGet
version 17.12.6
version 17.13.9
}
mermaid-graph ..> Microsoft.Build
class Microsoft.Build.Locator{
type NuGet
version 1.7.8
version 1.9.1
}
mermaid-graph ..> Microsoft.Build.Locator
class Microsoft.Build.Utilities.Core{
type NuGet
version 17.12.6
version 17.13.9
}
mermaid-graph ..> Microsoft.Build.Utilities.Core
class System.CommandLine.DragonFruit{
Expand All @@ -50,19 +50,29 @@ classDiagram
version 6.0.4
}
MermaidGraphTests ..> coverlet.msbuild
class Microsoft.ClearScript.V8{
type NuGet
version 7.5.0
}
MermaidGraphTests ..> Microsoft.ClearScript.V8
class Microsoft.ClearScript.V8.Native.win-x64{
type NuGet
version 7.5.0
}
MermaidGraphTests ..> Microsoft.ClearScript.V8.Native.win-x64
class Microsoft.NET.Test.Sdk{
type NuGet
version 17.13.0
}
MermaidGraphTests ..> Microsoft.NET.Test.Sdk
class MSTest.TestAdapter{
type NuGet
version 3.7.3
version 3.8.3
}
MermaidGraphTests ..> MSTest.TestAdapter
class MSTest.TestFramework{
type NuGet
version 3.7.3
version 3.8.3
}
MermaidGraphTests ..> MSTest.TestFramework
class NUnit{
Expand All @@ -72,12 +82,13 @@ classDiagram
MermaidGraphTests ..> NUnit
class NUnit.Analyzers{
type NuGet
version 4.6.0
version 4.7.0
}
MermaidGraphTests ..> NUnit.Analyzers
class NUnit3TestAdapter{
type NuGet
version 4.3.2
version 5.0.0
}
MermaidGraphTests ..> NUnit3TestAdapter
```

2 changes: 2 additions & 0 deletions MermaidGraph.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C33AF3DC-C3F9-45B6-828C-57CB2B60EC96}"
ProjectSection(SolutionItems) = preProject
MermaidGraph.NET.md = MermaidGraph.NET.md
.github\workflows\publish.yml = .github\workflows\publish.yml
README.md = README.md
version.json = version.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MermaidGraphTests", "mermaid-graphTests\MermaidGraphTests.csproj", "{FADD9D23-431E-4C1B-B2D4-D4FDF0674FCA}"
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Create a mermaid graph of the dependency diagram for a project, or whole solution.

## 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/)
## Dotnet tool [![NuGet version (mermaid-graph)](https://img.shields.io/nuget/v/mermaid-graph.svg?style=flat-round)](https://www.nuget.org/packages/mermaid-graph/)

You can install as a dotnet tool so you can easily map all of your software projects:

Expand All @@ -21,6 +21,8 @@ Usage:
Options:
--path <path> Full path to the solution (*.sln) or project (*.csproj) file that will be mapped.
--type <Class|Graph> The type of diagram to generate (e.g., Graph or Class). [default: Graph]
--filter <filter> Exclude projects whose name matches the filter. (e.g., Test). []
--no-nuget Do not include NuGet packages in the graph. [default: False]
--version Show version information
-?, -h, --help Show help and usage information
```
Expand Down Expand Up @@ -53,17 +55,17 @@ classDiagram
}
class Microsoft.Build{
type NuGet
version 17.12.6
version 17.13.9
}
mermaid-graph ..> Microsoft.Build
class Microsoft.Build.Locator{
type NuGet
version 1.7.8
version 1.9.1
}
mermaid-graph ..> Microsoft.Build.Locator
class Microsoft.Build.Utilities.Core{
type NuGet
version 17.12.6
version 17.13.9
}
mermaid-graph ..> Microsoft.Build.Utilities.Core
class System.CommandLine.DragonFruit{
Expand All @@ -89,12 +91,12 @@ classDiagram
MermaidGraphTests ..> coverlet.msbuild
class Microsoft.ClearScript.V8{
type NuGet
version 7.4.5
version 7.5.0
}
MermaidGraphTests ..> Microsoft.ClearScript.V8
class Microsoft.ClearScript.V8.Native.win-x64{
type NuGet
version 7.4.5
version 7.5.0
}
MermaidGraphTests ..> Microsoft.ClearScript.V8.Native.win-x64
class Microsoft.NET.Test.Sdk{
Expand All @@ -104,12 +106,12 @@ classDiagram
MermaidGraphTests ..> Microsoft.NET.Test.Sdk
class MSTest.TestAdapter{
type NuGet
version 3.7.3
version 3.8.3
}
MermaidGraphTests ..> MSTest.TestAdapter
class MSTest.TestFramework{
type NuGet
version 3.7.3
version 3.8.3
}
MermaidGraphTests ..> MSTest.TestFramework
class NUnit{
Expand All @@ -119,12 +121,12 @@ classDiagram
MermaidGraphTests ..> NUnit
class NUnit.Analyzers{
type NuGet
version 4.6.0
version 4.7.0
}
MermaidGraphTests ..> NUnit.Analyzers
class NUnit3TestAdapter{
type NuGet
version 4.3.2
version 5.0.0
}
MermaidGraphTests ..> NUnit3TestAdapter
```
25 changes: 7 additions & 18 deletions mermaid-graph/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MermaidGraph.Diagrams;
using MermaidGraph.Diagrams.Base;

namespace MermaidGraph;

Expand All @@ -10,31 +10,20 @@ public class Commands
/// <summary>
/// Generate the dependency graph of a Visual Studio Project.
/// </summary>
/// <param name="file">`.csproj` file.</param>
/// <param name="diagramType"></param>
public static string Project(FileInfo file, DiagramType diagramType = DiagramType.Graph)
public static string Project(FileInfo file, DiagramType diagramType = DiagramType.Graph, string? filter = null, bool excludeNuget = false)
{
var graph = GetGraphType(diagramType);
var graph = MermaidDiagram.GetDiagramType(diagramType);

return graph.Project(file);
return graph.Project(file, filter, excludeNuget);
}

/// <summary>
/// Generate the dependency graph of a Visual Studio Solution.
/// </summary>
/// <param name="file">`.sln` file.</param>
/// <param name="diagramType"></param>
public static string Solution(FileInfo file, DiagramType diagramType = DiagramType.Graph)
public static string Solution(FileInfo file, DiagramType diagramType = DiagramType.Graph, string? filter = null, bool excludeNuget = false)
{
var graph = GetGraphType(diagramType);
var graph = MermaidDiagram.GetDiagramType(diagramType);

return graph.Solution(file);
return graph.Solution(file, filter, excludeNuget);
}

private static IMermaidDiagram GetGraphType(DiagramType diagramType) => diagramType switch
{
DiagramType.Class => new ClassDiagram(),
DiagramType.Graph => new GraphDiagram(),
_ => throw new NotImplementedException($"Option not supported: {diagramType}"),
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MermaidGraph.Diagrams;
namespace MermaidGraph.Diagrams.Base;

/// <summary>
/// Specifies the type of mermaid diagram to generate.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MermaidGraph.Diagrams;
namespace MermaidGraph.Diagrams.Base;

/// <summary>
/// This file defines the IMermaidDiagram interface and the MermaidDiagram abstract class.
Expand All @@ -11,13 +11,17 @@ public interface IMermaidDiagram
/// Generate the diagram from a visual studio project file (*.csproj)
/// </summary>
/// <param name="file">The project file</param>
/// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test)</param>
/// <param name="excludeNuget">Do not include NuGet packages in the graph</param>
/// <returns>Mermaid Markdown</returns>
public string Project(FileInfo file);
public string Project(FileInfo file, string? filter = null, bool excludeNuget = false);

/// <summary>
/// Generate the diagram from a visual studio solution file (*.sln)
/// </summary>
/// <param name="file">The solution file.</param>
/// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test)</param>
/// <param name="excludeNuget">Do not include NuGet packages in the graph</param>
/// <returns>Mermaid Markdown</returns>
public string Solution(FileInfo file);
public string Solution(FileInfo file, string? filter = null, bool excludeNuget = false);
}
96 changes: 96 additions & 0 deletions mermaid-graph/Diagrams/Base/MermaidDiagram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Text;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;

namespace MermaidGraph.Diagrams.Base;

/// <summary>
/// The MermaidDiagram abstract class implements shared functionality for Mermaid diagram generation,
/// including initializing the graph output and managing the graph buffer.
/// </summary>
public abstract class MermaidDiagram : IMermaidDiagram
{
/// <summary>
/// Code block fence.
/// </summary>
public const string Fence = "```";

/// <summary>
/// Mermaid code block.
/// </summary>
public const string MermaidBegin = Fence + "mermaid";

internal StringBuilder Graph { get; } = new(256);

/// <summary>
/// Initialize the MermaidDiagram class and ensure MSBuild is registered.
/// </summary>
protected MermaidDiagram()
{
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
}

/// <summary>
/// Factory method to get the appropriate graph type based on the provided DiagramType enum value.
/// </summary>
/// <param name="diagramType">The type of graph to generate.</param>
/// <returns>The appropriate methods for generating a diagram of that type.</returns>
/// <exception cref="NotImplementedException">If an enum type is added without corresponding diagram class.</exception>
public static IMermaidDiagram GetDiagramType(DiagramType diagramType) => diagramType switch
{
DiagramType.Class => new ClassDiagram(),
DiagramType.Graph => new GraphDiagram(),
_ => throw new NotImplementedException($"Option not supported: {diagramType}"),
};

/// <summary>
/// Initialize the graph output.
/// </summary>
public virtual void Header(string title)
{
Graph.Clear();
Graph.AppendLine($"""
{MermaidBegin}
---
title: {title}
config:
class:
hideEmptyMembersBox: true
---
""");
}

/// <summary>
/// Get the mermaid diagram Markdown text.
/// </summary>
/// <returns>The contents of the graph buffer.</returns>
public override string ToString() => Graph.ToString();

/// <inheritdoc />
public virtual string Project(FileInfo file, string? filter = null, bool excludeNuget = false)
{
Header(file.Name);
using var projectCollection = new ProjectCollection();
var project = projectCollection.LoadProject(file.FullName);
GraphProject(project, filter, excludeNuget);
Graph.AppendLine(Fence);

projectCollection.UnloadAllProjects();

return Graph.ToString();
}

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

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