-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocBuilder.cs
292 lines (250 loc) · 10.9 KB
/
DocBuilder.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// DocGen Source
//
// Copyright (c) 2019 ADAM MAJCHEREK ALL RIGHTS RESERVED
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using JEM;
namespace DocGen
{
internal class DocBuilder
{
internal Assembly Assembly { get; }
internal XmlDocument Document { get; }
internal string DeployDir { get; }
internal string ExamplesDir { get; }
private readonly XmlNodeList _nodes;
internal DocBuilder(Assembly a, XmlDocument doc, string deployDir, string examplesDir)
{
Assembly = a;
Document = doc;
DeployDir = deployDir;
ExamplesDir = examplesDir;
_nodes = doc.DocumentElement?.SelectNodes("/doc/members/member") ?? throw new NullReferenceException();
}
internal List<string> Build()
{
var allTypes = Assembly.GetTypes();
var buildFiles = new List<string>();
foreach (var type in allTypes)
{
Program.Process($"Generating from type: {type.FullName}");
var fileName = BuildType(type);
if (string.IsNullOrEmpty(fileName))
{
Program.Process($"Target type ({type.Name}) refused to generate content.", true);
continue;
}
buildFiles.Add(fileName);
Program.Process($"Content successfully generated from: {type.FullName}");
}
Program.Process($"Generating Sidebar from {buildFiles.Count} files.");
// TODO: Support multiple assemblies
var str = new StringBuilder();
if (File.Exists(AppConfig.Loaded.SidebarBefore))
{
str.AppendLine(File.ReadAllText(AppConfig.Loaded.SidebarBefore));
str.AppendLine();
}
str.AppendLine($"- {AppConfig.Loaded.TreeTitleName}");
foreach (var f in buildFiles)
{
str.AppendLine($" - [{f}]({DocSyntax.GetMarkdownFile($"{AppConfig.Loaded.DeploySidebarPath}{f}")})");
}
if (File.Exists(AppConfig.Loaded.SidebarEnd))
{
str.AppendLine();
str.AppendLine(File.ReadAllText(AppConfig.Loaded.SidebarEnd));
}
WriteAllText($"{DeployDir}{EnvironmentUtility.DirectorySeparator}{AppConfig.Loaded.SidebarName}", str.ToString());
if (File.Exists(AppConfig.Loaded.ReadMe))
{
File.Copy(AppConfig.Loaded.ReadMe, $"{DeployDir}{EnvironmentUtility.DirectorySeparator}README.md");
}
return buildFiles;
}
private string BuildType(Type t)
{
if (!t.IsVisible)
{
Program.Process($"Target type ({t.Name}) is not visible!", true);
return null;
}
if (DocBuilderHelper.IsTypeArrayContainsNotAllowed(t))
{
Program.Process($"Target type ({t.Name}) is not allowed to be generated!", true);
return null;
}
var fileName = DocSyntax.CollectTypeName(t);
var filePath = DocSyntax.GetMarkdownFile($"{DeployDir}{EnvironmentUtility.DirectorySeparator}{fileName}");
if (DocSyntax.FixVarName(ref filePath))
{
Program.Process($"Target type ({t.Name}) has invalid filePath ({filePath}).", true);
return null;
}
var str = new StringBuilder();
var subStr = new StringBuilder();
if (t.IsClass)
{
Program.Process($"New class: {t.Name}");
// HEADER
str.AppendLine($"# {t.Name}");
if (t.BaseType != null && t.BaseType != typeof(object))
{
str.AppendLine($"<small>class in `{Path.GetFileNameWithoutExtension(new Uri(t.Assembly.CodeBase).AbsolutePath)}` " +
$"/ inherits from {DocClassUtil.GetTypeMarkdown(t.BaseType, false)}</small>");
}
else
{
str.AppendLine($"<small>{(t.IsAbstract && t.IsSealed ? "static " : string.Empty)}class in `{Path.GetFileNameWithoutExtension(new Uri(t.Assembly.CodeBase).AbsolutePath)}`</small>");
}
str.AppendLine(string.Empty);
// SUMMARY
DocXmlUtil.CollectSummaryFromNode(_nodes, t.FullName, out var classSummary);
str.AppendLine("### Description");
str.AppendLine(classSummary);
// EVENTS
DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Target, false);
// FIELDS
DocBuilderHelper.BuildFields(t, str, _nodes, TypeContent.Target, false);
// PROPERTIES
DocBuilderHelper.BuildProperties(t, str, _nodes, TypeContent.Target, false);
// METHODS
DocBuilderHelper.BuildMethods(t, str, _nodes, TypeContent.Target, false);
// STATIC EVENTS
DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Target, true);
// STATIC FIELDS
DocBuilderHelper.BuildFields(t, str, _nodes, TypeContent.Target, true);
// STATIC PROPERTIES
DocBuilderHelper.BuildProperties(t, str, _nodes, TypeContent.Target, true);
// STATIC METHODS
DocBuilderHelper.BuildMethods(t, str, _nodes, TypeContent.Target, true);
subStr.Clear();
// INHERITED MEMBERS
// EVENTS
DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Inherited, false);
// INHERITED MEMBERS
// FIELDS
DocBuilderHelper.BuildFields(t, subStr, _nodes, TypeContent.Inherited, false);
// INHERITED MEMBERS
// PROPERTIES
DocBuilderHelper.BuildProperties(t, subStr, _nodes, TypeContent.Inherited, false);
// INHERITED MEMBERS
// METHODS
DocBuilderHelper.BuildMethods(t, subStr, _nodes, TypeContent.Inherited, false);
// INHERITED MEMBERS
// STATIC EVENTS
DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Inherited, true);
// INHERITED MEMBERS
// STATIC FIELDS
DocBuilderHelper.BuildFields(t, subStr, _nodes, TypeContent.Inherited, true);
// INHERITED MEMBERS
// STATIC PROPERTIES
DocBuilderHelper.BuildProperties(t, subStr, _nodes, TypeContent.Inherited, true);
// INHERITED MEMBERS
// STATIC METHODS
DocBuilderHelper.BuildMethods(t, subStr, _nodes, TypeContent.Inherited, true);
if (!string.IsNullOrEmpty(subStr.ToString()) && !string.IsNullOrWhiteSpace(subStr.ToString()))
{
str.AppendLine();
str.AppendLine();
str.AppendLine("## Inherited Members");
str.AppendLine(subStr.ToString());
}
// EXAMPLES
BuildExample(t, str);
}
else if (t.IsEnum)
{
Program.Process($"New Enum: {t.Name}");
// HEADER
str.AppendLine($"# {t.Name}");
if (t.BaseType != null && t.BaseType != typeof(Object))
{
str.AppendLine($"<small>enumeration in `{Path.GetFileNameWithoutExtension(new Uri(t.Assembly.CodeBase).AbsolutePath)}`</small>");
}
str.AppendLine(string.Empty);
// SUMMARY
DocXmlUtil.CollectSummaryFromNode(_nodes, t.FullName, out var classSummary);
str.AppendLine("### Description");
str.AppendLine(classSummary);
// PROPERTIES
DocBuilderHelper.BuildEnum(t, str, _nodes);
// EXAMPLES
BuildExample(t, str);
}
if (File.Exists(AppConfig.Loaded.FileEnd))
{
str.AppendLine();
str.AppendLine(File.ReadAllText(AppConfig.Loaded.FileEnd));
}
WriteAllText(filePath, str.ToString());
return fileName;
}
private void BuildExample(Type t, StringBuilder str)
{
var exampleFile = FindExampleFile(t);
if (string.IsNullOrEmpty(exampleFile))
return;
str.AppendLine();
str.AppendLine("## Examples");
str.AppendLine();
var exampleStr = File.ReadAllText(exampleFile);
var allTypes = t.Assembly.GetTypes();
foreach (var a in allTypes)
{
var s = a.Name;
exampleStr = exampleStr.Replace($"(#{s})", $"({DocSyntax.CollectMarkDownReference(a)})");
}
str.AppendLine(exampleStr);
}
private string FindExampleFile(Type t)
{
if (!Directory.Exists(ExamplesDir))
return null;
string fileName = ExamplesDir + EnvironmentUtility.DirectorySeparator + t.Name + ".md";
if (File.Exists(fileName))
return fileName;
else
{
if (t.IsClass)
{
string[] allFiles = Directory.GetFiles(ExamplesDir, "*.md");
var methods = t.GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (var f in allFiles)
{
var exampleName = Path.GetFileNameWithoutExtension(f);
foreach (var m in methods)
{
if (m.DeclaringType != t)
continue;
var returnType = m.ReturnType.Name;
if (exampleName == returnType)
return f;
var genericParameters = m.GetGenericArguments();
foreach (var g in genericParameters)
{
var parameterType = g.Name;
if (exampleName == parameterType)
return f;
}
}
}
}
}
return null;
}
private static void WriteAllText(string filePath, string str)
{
var dir = Path.GetDirectoryName(filePath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
File.WriteAllText(filePath, str);
}
}
}