-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cs
More file actions
143 lines (122 loc) · 4.53 KB
/
Commands.cs
File metadata and controls
143 lines (122 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Text;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;
namespace MermaidGraph;
/// <summary>
/// The commands that can be run by `mermaid-graph`
/// </summary>
public class Commands
{
public const string MermaidBegin = Fence + "mermaid";
public const string Fence = "```";
private readonly StringBuilder _graph;
/// <summary>
/// Initialize the graph output
/// </summary>
public Commands()
{
_graph = new StringBuilder();
// Ensure MSBuild is registered
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
}
/// <summary>
/// Generate the dependency graph of a Visual Studio Project.
/// </summary>
/// <param name="file">`.csproj` file.</param>
public string Project(FileInfo file)
{
Header(file.Name);
using var projectCollection = new ProjectCollection();
var project = projectCollection.LoadProject(file.FullName);
GraphProject(project);
_graph.AppendLine(Fence);
var graph = _graph.ToString();
// Cleanup
_graph.Clear();
projectCollection.UnloadAllProjects();
return graph;
}
/// <summary>
/// Generate the dependency graph of a Visual Studio Solution.
/// </summary>
/// <param name="file">`.sln` file.</param>
public string Solution(FileInfo file)
{
Header(file.Name);
var solutionFile = SolutionFile.Parse(file.FullName);
var solutionName = Path.GetFileNameWithoutExtension(file.Name);
var solutionId = $"{solutionName}";
_graph.AppendLine($$"""
class {{solutionName}}{
type solution
}
""");
using var projectCollection = new ProjectCollection();
foreach (var project in solutionFile.ProjectsInOrder)
{
if (project.ProjectType != SolutionProjectType.KnownToBeMSBuildFormat) continue;
var projectPath = project.AbsolutePath;
var projectName = Path.GetFileNameWithoutExtension(projectPath);
_graph.AppendLine($" {solutionId} --> {projectName}");
var projectFile = new FileInfo(projectPath);
if (projectFile.Exists)
{
var referenceProject = projectCollection.LoadProject(projectFile.FullName);
GraphProject(referenceProject);
}
}
_graph.AppendLine(Fence);
var graph = _graph.ToString();
// Cleanup
_graph.Clear();
projectCollection.UnloadAllProjects();
return graph;
}
private void Header(string title)
{
_graph.AppendLine(MermaidBegin);
_graph.AppendLine($"""
---
title: {title}
config:
class:
hideEmptyMembersBox: true
---
""");
_graph.AppendLine("classDiagram");
}
private void GraphProject(Project project)
{
var projectName = Path.GetFileNameWithoutExtension(project.FullPath);
var type = project.GetPropertyValue("OutputType");
var targetFramework = project.GetPropertyValue("TargetFramework") ?? project.GetPropertyValue("TargetFrameworks");
_graph.AppendLine($$"""
class {{projectName}}{
type {{type}}
target {{targetFramework}}
}
""");
foreach (var item in project.GetItems("ProjectReference"))
{
var refPath = item.EvaluatedInclude;
var refName = Path.GetFileNameWithoutExtension(refPath);
_graph.AppendLine($" {projectName} ..> {refName}");
}
foreach (var item in project.GetItems("PackageReference"))
{
var packageName = item.EvaluatedInclude;
var version = item.GetMetadataValue("Version");
_graph.AppendLine($$"""
class {{packageName}}{
type NuGet
version {{version}}
}
""");
_graph.AppendLine($" {projectName} ..> {packageName}");
}
}
}