1- using System . Text ;
2- using Microsoft . Build . Construction ;
3- using Microsoft . Build . Evaluation ;
4- using Microsoft . Build . Locator ;
1+ using MermaidGraph . Diagrams ;
52
63namespace 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>
118public 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}
0 commit comments