-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
286 lines (241 loc) · 16 KB
/
Program.cs
File metadata and controls
286 lines (241 loc) · 16 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
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
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;
using System.Linq;
using System.Xml;
using System.Reflection;
using XmlDocToSlateMD.Documentation;
namespace XmlDocToSlateMD
{
class MainClass
{
static AssemblyDoc CurrentAssembly;
static Assembly reflectedAssembly;
static List<Type> reflectedMethodParams;
static Type reflectedType;
static MethodBase reflectedMethod;
static BaseDoc CurrentType;
static BaseDoc CurrentMethod;
static BaseDoc PreviousDoc;
static int currentParam = 0;
public static void Main(string[] args)
{
try {
// load .dll files in the current directory to
foreach (var file in Directory.GetFiles(Environment.CurrentDirectory, "*", SearchOption.AllDirectories)) {
if (file.ToLowerInvariant().EndsWith(".dll")) {
Assembly.LoadFile(file);
}
}
foreach (var file in Directory.GetFiles(Environment.CurrentDirectory, "*", SearchOption.AllDirectories)) {
if (file.ToLowerInvariant().EndsWith(".xml")) {
string xmlStr = File.ReadAllText(file);
xmlStr = Regex.Replace(xmlStr,
"<see cref=\"[A-Z?]:(.*)\" \\/>",
m => "<a href=\"#" +
m.Groups[1].Value.ToLower().Replace('.', '-') + "\">" +
m.Groups[1].Value.Split('.').Last() +
"</a>"
);
xmlStr = Regex.Replace(xmlStr,
@"<(c|code)>([^<])*<\/(c|code)>",
m => "<" +
m.Groups[1].Value +
">" +
m.Groups[2].Captures.Join() +
"</" +
m.Groups[1].Value +
">"
);
using (var stream = xmlStr.ToStream()) {
// TODO: Add reflection stuff, to:
// * determine if properties has setters or only getters
using (var xml = System.Xml.XmlReader.Create(stream)) {
while (xml.Read()) {
if (xml.IsStartElement()) {
BaseDoc CurrentDoc = null;
switch (xml.Name) {
case "assembly":
CurrentAssembly = new AssemblyDoc();
CurrentDoc = CurrentAssembly;
break;
case "name":
xml.Read();
CurrentAssembly.Name = xml.Value;
reflectedAssembly = AppDomain.CurrentDomain.GetAssemblies().ToList().First(a => a.GetName().Name == xml.Value);
break;
case "member":
var memberName = xml["name"];
char type = memberName[0];
if (memberName.Contains("Hooks")) {
PreviousDoc = null;
break;
}
switch (type) {
case 'T':
CurrentDoc = new TypeDoc(CurrentAssembly);
CurrentType = CurrentDoc;
reflectedType = reflectedAssembly.GetType(memberName.Substring(2));
break;
case 'P':
case 'F':
if (CurrentType == null || !memberName.Contains(CurrentType.Name.Substring(2))) {
string typename = memberName.GetTypeName();
CurrentType = new TypeDoc(CurrentAssembly) { Name = typename };
reflectedType = reflectedAssembly.GetType(typename);
}
// if type == p => add property documentation else => add field doc.
CurrentDoc = type == 'P' ? (new PropertyDoc(CurrentType as TypeDoc) {
Name = memberName.Substring(2),
Type = (from pinfo in reflectedType.GetProperties()
where pinfo.Name == memberName.GetMemberName()
select pinfo.PropertyType).FirstOrDefault()
} as BaseDoc)
: (new FieldDoc(CurrentType as TypeDoc) {
Name = memberName.Substring(2),
Type = (from finfo in reflectedType.GetFields()
where finfo.Name == memberName.GetMemberName()
select finfo.FieldType).FirstOrDefault()
} as BaseDoc);
break;
case 'M':
// check if the method is a method of the _current_ type, if it's not then it means the type of this method is not _documented_
// so we add an empty doc for it here
if (CurrentType == null || !memberName.Contains(CurrentType.Name.Substring(2))) {
string typename = memberName.GetTypeName();
CurrentType = new TypeDoc(CurrentAssembly) { Name = typename };
reflectedType = reflectedAssembly.GetType(typename);
}
reflectedMethodParams = new List<Type>();
string methodname = memberName.GetMemberName();
string paramtypes = Regex.Match(memberName, "\\(([\\.,A-z0-9])*\\)").Groups[1].Captures.Join();
foreach (var paramtype in paramtypes.Split(',')) {
Type typ = null;
if (!TryFindType(paramtype, out typ)) {
var paramtype2 = Regex.Replace(paramtype, "(.*)\\.([A-z]*)$", "$1+$2");
TryFindType(paramtype2, out typ);
}
if (typ != null)
reflectedMethodParams.Add(typ);
}
if (!methodname.Contains('#'))
reflectedMethod = reflectedType.GetMethod(methodname, reflectedMethodParams.ToArray());
else
reflectedMethod = reflectedType.GetConstructor(reflectedMethodParams.ToArray());
// the method's name for constructors is #ctor, lets change that to the name of the Type
if (memberName.Contains("#ctor")) {
CurrentDoc = new ConstructorDoc(CurrentType as TypeDoc);
CurrentDoc.Name = memberName.Replace("#ctor", Regex.Match(memberName, ".([A-z]+).#").Groups[1].Value).Substring(2);
CurrentDoc.Name = CurrentDoc.Name.Substring(0, CurrentDoc.Name.IndexOf("("));
} else {
CurrentDoc = new MethodDoc(CurrentType as TypeDoc);
}
CurrentMethod = CurrentDoc;
currentParam = 0;
if (reflectedMethod is MethodInfo)
CurrentMethod["ReturnType"] = (reflectedMethod as MethodInfo).ReturnType;
else
CurrentMethod["ReturnType"] = reflectedType;
break;
}
if (CurrentDoc.Name == null) {
if (memberName.Contains("("))
CurrentDoc.Name = memberName.Substring(2, memberName.IndexOf("(") - 2);
else
CurrentDoc.Name = memberName.Substring(2);
}
break;
case "summary":
xml.Read();
if (PreviousDoc != null)
PreviousDoc["Summary"] = xml.Value.Trim();
break;
case "param":
string name = xml["name"];
xml.Read();
CurrentDoc = new ParameterDoc(CurrentMethod) {
Name = name,
Summary = xml.Value,
Type = reflectedMethodParams[currentParam]
};
currentParam++;
break;
case "csharp":
case "javascript":
case "python":
case "lua":
if (PreviousDoc is TypeDoc) {
var cdoc = PreviousDoc as TypeDoc;
var lang = xml.Name;
xml.Read();
cdoc.CodeExamples.Add(new CodeExample {
Code = xml.Value,
Language = (Language)Enum.Parse(typeof(Language), lang)
});
} else if (PreviousDoc is MethodDoc) {
var cdoc = PreviousDoc as MethodDoc;
var lang = xml.Name;
xml.Read();
cdoc.CodeExamples.Add(new CodeExample {
Code = xml.Value,
Language = (Language)Enum.Parse(typeof(Language), lang)
});
}
break;
case "value":
xml.Read();
PreviousDoc["defaultValue"] = xml.Value;
break;
}
if (CurrentDoc != null)
PreviousDoc = CurrentDoc;
}
}
}
#if DEBUG
Console.WriteLine("CurrentAssembly is: " + Environment.NewLine + CurrentAssembly);
#endif
CurrentAssembly.ToFile();
}
}
}
} catch(Exception ex) {
Console.WriteLine(ex.ToString());
Console.WriteLine(ex.StackTrace);
}
}
public static readonly Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
public static bool TryFindType(string typeName, out Type t)
{
lock (typeCache) {
if (!typeCache.TryGetValue(typeName, out t)) {
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
t = assembly.GetType(typeName);
if (t != null) {
break;
}
}
typeCache[typeName] = t;
}
}
return (t != null);
}
}
public enum Language
{
csharp,
javascript,
python,
lua
}
public struct CodeExample
{
public Language Language;
public string Code;
public override string ToString()
{
return $"```{Language}{Environment.NewLine}{Code}{Environment.NewLine}```";
}
}
}