-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathType.cs
177 lines (146 loc) · 5.74 KB
/
Type.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
/*
// Xml Documentation Generator (XDG)
// Copyright (C) 2012, Ilmar Kruis
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one
// at http://mozilla.org/MPL/2.0/.
//
*/
using System;
using System.Collections.Generic;
using Mono.Cecil;
using System.Xml;
using System.Text;
using Newtonsoft.Json;
using System.IO;
namespace XDG
{
class Type
{
public string Name { get; set; }
public string LinkName { get; set; }
public string Summary { get; set; }
public string Remarks { get; set; }
public string CSharpDecl { get; set; }
public string Title { get; set; }
public string Namespace { get; set; }
public List<MenuItem> Menu { get; set; }
public List<Method> Methods { get; private set; }
public List<Property> Properties { get; private set; }
public string Copyright { get; set; }
// for debugging
private TypeDefinition td;
public Type(string moduleName, TypeDefinition type)
{
td = type;
Name = GetName(type);
LinkName = GetName(type, false, true);
Title = moduleName + " Documentation - " + Name;
Namespace = GetNamespace(type);
Copyright = "This documentation was generated using <a href=\"https://github.com/seaeagle1/XDG\">XDG</a>.";
CSharpDecl = CSharpTypeDecl(type);
XmlNode doc = Xdg.FindXmlDoc(type);
if (doc != null)
{
Summary = doc.GetElementContent("summary");
Remarks = doc.GetElementContent("remarks");
}
Methods = new List<Method>();
foreach (MethodDefinition m in type.Methods)
{
if ((!m.IsPublic && !m.IsFamily) || m.IsSetter || m.IsGetter)
continue;
Methods.Add(new Method(m));
}
Methods.Sort( new Comparison<Method>((Method a, Method b) => { return a.Name.CompareTo(b.Name); }));
Properties = new List<Property>();
foreach (PropertyDefinition m in type.Properties)
{
bool isPublic = false;
if(m.GetMethod != null && (m.GetMethod.IsPublic || m.GetMethod.IsFamily))
isPublic = true;
if(m.SetMethod != null && (m.SetMethod.IsPublic || m.SetMethod.IsFamily))
isPublic = true;
if(!isPublic)
continue;
Properties.Add(new Property(m));
}
Properties.Sort(new Comparison<Property>((Property a, Property b) => { return a.Name.CompareTo(b.Name); }));
}
public void Write()
{
using (StreamWriter sw = new StreamWriter("../../example/"+LinkName+".js"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Newtonsoft.Json.Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, this);
}
}
static string GetNamespace(TypeReference tr)
{
while (tr.IsNested)
tr = tr.DeclaringType;
return tr.Namespace;
}
public static string GetName(TypeReference tr, bool use_generics = true, bool use_namespace = false)
{
string[] strSlices = tr.Name.Split(new char[] { '`' });
if (tr.HasGenericParameters && use_generics)
{
strSlices[0] += "<";
string comma = "";
foreach (GenericParameter p in tr.GenericParameters)
{
strSlices[0] += GetName(p) + comma;
comma = ",";
}
strSlices[0] += ">";
}
if (tr is GenericInstanceType && use_generics)
{
strSlices[0] += "<";
string comma = "";
foreach (TypeReference p in (tr as GenericInstanceType).GenericArguments)
{
strSlices[0] += GetName(p) + comma;
comma = ",";
}
strSlices[0] += ">";
}
string rv = "";
if (use_namespace)
rv += GetNamespace(tr) + ".";
if (tr.IsNested)
rv += GetName(tr.DeclaringType, use_generics) + "." + strSlices[0];
else
rv += strSlices[0];
return rv;
}
static string CSharpTypeDecl(TypeDefinition type)
{
StringBuilder s = new StringBuilder();
if (type.IsPublic)
s.AppendStyled("keyword", "public ");
if (type.IsEnum)
s.AppendStyled("keyword", "enum ");
else if (type.IsValueType)
s.AppendStyled("keyword", "struct ");
else if (type.IsClass)
s.AppendStyled("keyword", "class ");
s.Append(GetName(type));
if ((!type.IsEnum && !type.IsInterface) &&
(type.HasInterfaces || (type.BaseType.FullName != "System.Object" && type.BaseType.FullName != "System.ValueType")))
{
s.Append(" : ");
if (type.BaseType.FullName != "System.Object" && type.BaseType.FullName != "System.ValueType")
s.Append(type.BaseType.ToXdgUrl() + ", ");
foreach (TypeReference tr in type.Interfaces)
s.Append(tr.ToXdgUrl() + ", ");
s.Remove(s.Length - 2, 1);
}
return s.ToString();
}
}
}