-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassDiagrammerFactory.cs
79 lines (65 loc) · 3.94 KB
/
ClassDiagrammerFactory.cs
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
using System.Text.RegularExpressions;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.TypeSystem;
namespace NetAmermaid
{
using CD = ClassDiagrammer;
/* See class diagram syntax
* reference (may be outdated!) https://mermaid.js.org/syntax/classDiagram.html
* lexical definition https://github.com/mermaid-js/mermaid/blob/develop/packages/mermaid/src/diagrams/class/parser/classDiagram.jison */
/// <summary>Produces mermaid class diagram syntax for a filtered list of types from a specified .Net assembly.</summary>
public partial class ClassDiagrammerFactory
{
private readonly XmlDocumentationFormatter? xmlDocs;
private readonly DecompilerSettings decompilerSettings;
private ITypeDefinition[]? selectedTypes;
private Dictionary<IType, string>? uniqueIds;
private Dictionary<IType, string>? labels;
private Dictionary<string, string>? outsideReferences;
public ClassDiagrammerFactory(XmlDocumentationFormatter? xmlDocs)
{
this.xmlDocs = xmlDocs;
decompilerSettings = new DecompilerSettings(LanguageVersion.Latest)
{
AutomaticProperties = true // for IsHidden to return true for backing fields
};
}
public CD BuildModel(string assemblyPath, string? include, string? exclude)
{
CSharpDecompiler decompiler = new(assemblyPath, decompilerSettings);
IEnumerable<ITypeDefinition> allTypes = decompiler.TypeSystem.MainModule.TypeDefinitions;
selectedTypes = FilterTypes(allTypes,
include == null ? null : new(include, RegexOptions.Compiled),
exclude == null ? null : new(exclude, RegexOptions.Compiled)).ToArray();
// generate dict to read names from later
uniqueIds = GenerateUniqueIds(selectedTypes);
labels = new();
outsideReferences = new();
Dictionary<string, CD.Type[]> typesByNamespace = selectedTypes.GroupBy(t => t.Namespace).OrderBy(g => g.Key).ToDictionary(g => g.Key,
ns => ns.OrderBy(t => t.FullName).Select(type => type.Kind == TypeKind.Enum ? BuildEnum(type) : BuildType(type)).ToArray());
MetadataModule mainModule = decompiler.TypeSystem.MainModule;
string[] excluded = allTypes.Except(selectedTypes).Select(t => t.ReflectionName).ToArray();
return new CD
{
SourceAssemblyName = mainModule.AssemblyName,
SourceAssemblyVersion = mainModule.AssemblyVersion.ToString(),
TypesByNamespace = typesByNamespace,
OutsideReferences = outsideReferences,
Excluded = excluded
};
}
/// <summary>The default strategy for pre-filtering the <paramref name="typeDefinitions"/> available in the HTML diagrammer.
/// Applies <see cref="IsIncludedByDefault(ITypeDefinition)"/> as well as
/// matching by <paramref name="include"/> and not by <paramref name="exclude"/>.</summary>
/// <returns>The types to effectively include in the HTML diagrammer.</returns>
protected virtual IEnumerable<ITypeDefinition> FilterTypes(IEnumerable<ITypeDefinition> typeDefinitions, Regex? include, Regex? exclude)
=> typeDefinitions.Where(type => IsIncludedByDefault(type)
&& (include == null || include.IsMatch(type.ReflectionName)) // applying optional whitelist filter
&& (exclude == null || !exclude.IsMatch(type.ReflectionName))); // applying optional blacklist filter
/// <summary>The strategy for deciding whether a <paramref name="type"/> should be included
/// in the HTML diagrammer by default. Excludes compiler-generated and their nested types.</summary>
protected virtual bool IsIncludedByDefault(ITypeDefinition type)
=> !type.IsCompilerGeneratedOrIsInCompilerGeneratedClass();
}
}