11using System . Text ;
2+ using Microsoft . Build . Evaluation ;
23using Microsoft . Build . Locator ;
34
4- namespace MermaidGraph . Diagrams ;
5+ namespace MermaidGraph . Diagrams . Base ;
56
67/// <summary>
78/// The MermaidDiagram abstract class implements shared functionality for Mermaid diagram generation,
@@ -19,7 +20,7 @@ public abstract class MermaidDiagram : IMermaidDiagram
1920 /// </summary>
2021 public const string MermaidBegin = Fence + "mermaid" ;
2122
22- internal readonly StringBuilder Graph = new ( ) ;
23+ internal readonly StringBuilder Graph = new ( 256 ) ;
2324
2425 /// <summary>
2526 /// Initialize the MermaidDiagram class and ensure MSBuild is registered.
@@ -32,14 +33,27 @@ protected MermaidDiagram()
3233 }
3334 }
3435
36+ /// <summary>
37+ /// Factory method to get the appropriate graph type based on the provided DiagramType enum value.
38+ /// </summary>
39+ /// <param name="diagramType">The type of graph to generate.</param>
40+ /// <returns>The appropriate methods for generating a diagram of that type.</returns>
41+ /// <exception cref="NotImplementedException">If an enum type is added without corresponding diagram class.</exception>
42+ public static IMermaidDiagram GetDiagramType ( DiagramType diagramType ) => diagramType switch
43+ {
44+ DiagramType . Class => new ClassDiagram ( ) ,
45+ DiagramType . Graph => new GraphDiagram ( ) ,
46+ _ => throw new NotImplementedException ( $ "Option not supported: { diagramType } ") ,
47+ } ;
48+
3549 /// <summary>
3650 /// Initialize the graph output.
3751 /// </summary>
3852 public virtual void Header ( string title )
3953 {
4054 Graph . Clear ( ) ;
41- Graph . AppendLine ( MermaidBegin ) ;
4255 Graph . AppendLine ( $ """
56+ { MermaidBegin }
4357 ---
4458 title: { title }
4559 config:
@@ -56,8 +70,25 @@ public virtual void Header(string title)
5670 public override string ToString ( ) => Graph . ToString ( ) ;
5771
5872 /// <inheritdoc />
59- public abstract string Project ( FileInfo file ) ;
73+ public virtual string Project ( FileInfo file )
74+ {
75+ Header ( file . Name ) ;
76+ using var projectCollection = new ProjectCollection ( ) ;
77+ var project = projectCollection . LoadProject ( file . FullName ) ;
78+ GraphProject ( project ) ;
79+ Graph . AppendLine ( Fence ) ;
80+
81+ projectCollection . UnloadAllProjects ( ) ;
82+
83+ return Graph . ToString ( ) ;
84+ }
6085
6186 /// <inheritdoc />
6287 public abstract string Solution ( FileInfo file ) ;
88+
89+ /// <summary>
90+ /// This method must be implemented in all derived classes to generate the graph for a project.
91+ /// </summary>
92+ /// <param name="project">A project to graph.</param>
93+ internal abstract void GraphProject ( Project project ) ;
6394}
0 commit comments