|
| 1 | +using System.Text; |
| 2 | +using Microsoft.Build.Evaluation; |
| 3 | +using Microsoft.Build.Locator; |
| 4 | + |
| 5 | +namespace MermaidGraph.Diagrams.Base; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// The MermaidDiagram abstract class implements shared functionality for Mermaid diagram generation, |
| 9 | +/// including initializing the graph output and managing the graph buffer. |
| 10 | +/// </summary> |
| 11 | +public abstract class MermaidDiagram : IMermaidDiagram |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Code block fence. |
| 15 | + /// </summary> |
| 16 | + public const string Fence = "```"; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Mermaid code block. |
| 20 | + /// </summary> |
| 21 | + public const string MermaidBegin = Fence + "mermaid"; |
| 22 | + |
| 23 | + internal StringBuilder Graph { get; } = new(256); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initialize the MermaidDiagram class and ensure MSBuild is registered. |
| 27 | + /// </summary> |
| 28 | + protected MermaidDiagram() |
| 29 | + { |
| 30 | + if (!MSBuildLocator.IsRegistered) |
| 31 | + { |
| 32 | + MSBuildLocator.RegisterDefaults(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 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 | + |
| 49 | + /// <summary> |
| 50 | + /// Initialize the graph output. |
| 51 | + /// </summary> |
| 52 | + public virtual void Header(string title) |
| 53 | + { |
| 54 | + Graph.Clear(); |
| 55 | + Graph.AppendLine($""" |
| 56 | + {MermaidBegin} |
| 57 | + --- |
| 58 | + title: {title} |
| 59 | + config: |
| 60 | + class: |
| 61 | + hideEmptyMembersBox: true |
| 62 | + --- |
| 63 | + """); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Get the mermaid diagram Markdown text. |
| 68 | + /// </summary> |
| 69 | + /// <returns>The contents of the graph buffer.</returns> |
| 70 | + public override string ToString() => Graph.ToString(); |
| 71 | + |
| 72 | + /// <inheritdoc /> |
| 73 | + public virtual string Project(FileInfo file, string? filter = null, bool excludeNuget = false) |
| 74 | + { |
| 75 | + Header(file.Name); |
| 76 | + using var projectCollection = new ProjectCollection(); |
| 77 | + var project = projectCollection.LoadProject(file.FullName); |
| 78 | + GraphProject(project, filter, excludeNuget); |
| 79 | + Graph.AppendLine(Fence); |
| 80 | + |
| 81 | + projectCollection.UnloadAllProjects(); |
| 82 | + |
| 83 | + return Graph.ToString(); |
| 84 | + } |
| 85 | + |
| 86 | + /// <inheritdoc /> |
| 87 | + public abstract string Solution(FileInfo file, string? filter = null, bool excludeNuget = false); |
| 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 | + /// <param name="filter">Exclude projects whose name matches the filter. (e.g., Test)</param> |
| 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); |
| 96 | +} |
0 commit comments