@@ -7,72 +7,92 @@ namespace MermaidGraph;
77/// <summary>
88/// The commands that can be run by `mermaid-graph`
99/// </summary>
10- public static class Commands
10+ public class Commands
1111{
12- private static readonly StringBuilder Graph = new ( ) ;
12+ private readonly StringBuilder _graph ;
1313
1414 private const string Fence = "```" ;
1515
16- static Commands ( )
16+ /// <summary>
17+ /// Initialize the graph output
18+ /// </summary>
19+ public Commands ( )
1720 {
18- Graph . AppendLine ( Fence + "mermaid" ) ;
19- Graph . AppendLine ( "graph TD" ) ;
21+ _graph = new StringBuilder ( ) ;
2022 }
2123
2224 /// <summary>
2325 /// Generate the dependency graph of a Visual Studio Project.
2426 /// </summary>
25- /// <param name="path">Full path to `.csproj` file.</param>
26- public static void Project ( string path )
27+ /// <param name="file"> `.csproj` file.</param>
28+ public string Project ( FileInfo file )
2729 {
28- GraphProject ( path ) ;
29- Graph . AppendLine ( Fence ) ;
30- Console . WriteLine ( Graph . ToString ( ) ) ;
31- }
32-
33- private static void GraphProject ( string path )
34- {
35- // Load project
36- var project = new Project ( path ) ;
37- var projectName = Path . GetFileNameWithoutExtension ( path ) ;
38-
39- foreach ( var item in project . GetItems ( "ProjectReference" ) )
40- {
41- string refPath = item . EvaluatedInclude ;
42- string refName = Path . GetFileNameWithoutExtension ( refPath ) ;
43- Graph . AppendLine ( $ " { projectName } --> { refName } ") ;
44- }
45-
46- foreach ( var item in project . GetItems ( "PackageReference" ) )
47- {
48- string packageName = item . EvaluatedInclude ;
49- Graph . AppendLine ( $ " { projectName } -->|NuGet| { packageName } ") ;
50- }
30+ Header ( file . Name ) ;
31+ GraphProject ( file ) ;
32+ _graph . AppendLine ( Fence ) ;
33+ var graph = _graph . ToString ( ) ;
34+ _graph . Clear ( ) ;
35+ return graph ;
5136 }
5237
5338 /// <summary>
5439 /// Generate the dependency graph of a Visual Studio Solution.
5540 /// </summary>
56- /// <param name="path">Full path to `.sln` file.</param>
57- public static void Solution ( string path )
41+ /// <param name="file"> `.sln` file.</param>
42+ public string Solution ( FileInfo file )
5843 {
59- var solutionFile = SolutionFile . Parse ( path ) ;
60- var solutionName = Path . GetFileNameWithoutExtension ( path ) ;
44+ Header ( file . Name ) ;
45+ var solutionFile = SolutionFile . Parse ( file . FullName ) ;
46+ var solutionName = Path . GetFileNameWithoutExtension ( file . Name ) ;
47+ var solutionId = $ "s{ solutionFile . GetHashCode ( ) } ({ solutionName } )";
6148 foreach ( var project in solutionFile . ProjectsInOrder )
6249 {
63- if ( project . ProjectType == SolutionProjectType . KnownToBeMSBuildFormat )
50+ if ( project . ProjectType != SolutionProjectType . KnownToBeMSBuildFormat ) continue ;
51+
52+ var projectPath = project . AbsolutePath ;
53+ var projectName = Path . GetFileNameWithoutExtension ( projectPath ) ;
54+ _graph . AppendLine ( $ " { solutionId } --> { projectName } ") ;
55+ var projectFile = new FileInfo ( projectPath ) ;
56+ if ( projectFile . Exists )
6457 {
65- var projectPath = project . AbsolutePath ;
66- var projectName = Path . GetFileNameWithoutExtension ( projectPath ) ;
67- Graph . AppendLine ( $ " { solutionName } --> { projectName } ") ;
68- if ( File . Exists ( projectPath ) )
69- {
70- GraphProject ( projectPath ) ;
71- }
58+ GraphProject ( projectFile ) ;
7259 }
7360 }
7461
75- Graph . AppendLine ( Fence ) ;
76- Console . WriteLine ( Graph . ToString ( ) ) ;
62+ _graph . AppendLine ( Fence ) ;
63+ var graph = _graph . ToString ( ) ;
64+ _graph . Clear ( ) ;
65+ return graph ;
66+ }
67+
68+ private void Header ( string title )
69+ {
70+ _graph . AppendLine ( Fence + "mermaid" ) ;
71+ _graph . AppendLine ( $ """
72+ ---
73+ title: { title }
74+ ---
75+ """ ) ;
76+
77+ _graph . AppendLine ( "graph TD" ) ;
78+ }
79+
80+ private void GraphProject ( FileInfo path )
81+ {
82+ var project = new Project ( path . FullName ) ;
83+ var projectName = Path . GetFileNameWithoutExtension ( path . Name ) ;
84+
85+ foreach ( var item in project . GetItems ( "ProjectReference" ) )
86+ {
87+ var refPath = item . EvaluatedInclude ;
88+ var refName = Path . GetFileNameWithoutExtension ( refPath ) ;
89+ _graph . AppendLine ( $ " { projectName } --> { refName } ") ;
90+ }
91+
92+ foreach ( var item in project . GetItems ( "PackageReference" ) )
93+ {
94+ var packageName = item . EvaluatedInclude ;
95+ _graph . AppendLine ( $ " { projectName } -->|NuGet| { packageName } ") ;
96+ }
7797 }
7898}
0 commit comments