-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAssemblyProcessor.cs
375 lines (336 loc) · 12.4 KB
/
AssemblyProcessor.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using nTsMapper.TypeScript;
namespace nTsMapper
{
public class AssemblyProcessor
{
private readonly CommandLineArgs mCommandLineArgs;
// Define other methods and classes here
private List<TypeMapping> mAllMappings;
private readonly Dictionary<Type, TsType> mTypeToTsTypeMap;
/// <summary>
/// Instantiates a new instance of the AssemblyProcessor with the parameters given.
/// </summary>
/// <param name="commandLineArgs">Use this to provide all the command line arguments needed to process and generate TypeScript.</param>
/// <param name="customMappings">Provide custom type mappings.</param>
public AssemblyProcessor(CommandLineArgs commandLineArgs, List<TypeMapping> customMappings)
{
mCommandLineArgs = commandLineArgs;
mTypeToTsTypeMap = new Dictionary<Type, TsType>();
FillAllMappings(customMappings);
}
public void ProcessAssembly()
{
var assembly = Assembly.LoadFrom(mCommandLineArgs.AssemblyPath);
AppDomain.CurrentDomain.AssemblyResolve += OnCurrentDomainOnAssemblyResolve;
var types = assembly.GetTypes();
var controllers = types.Where(IsApiController);
var commandParamtersAndDtoTypes = FindCommandParamtersAndDtoTypes(controllers);
// We've gathered a distinct list of all the not-directly-mapped top-level types that are used for input and output of the WebAPI action methods.
// Now with our defined mappings set up, we need to iterate the types we're going to generate proxies for and iterate their properties.
// We'll add the types of the properties to our working list according to the same logic as before: distinct and not-directly-mapped, skipping to the item type of enumerables
var typesToInspect = new List<Type>(commandParamtersAndDtoTypes);
foreach (var candidateType in typesToInspect)
{
MapToTsType(candidateType);
}
GenerateTypeScriptCode();
}
private void GenerateTypeScriptCode()
{
var typesToGenerate = mTypeToTsTypeMap
.Values.OfType<TsTypeWithProperties>()
.OrderBy(t => t.InheritanceHierarchyLevel)
.ThenBy(t => t.ModuleName)
.ToList();
// We are doing a distinct on the lists of enums in each grouping
// to prevent duplicate Enums from being generated.
var enumsToGenerate = mTypeToTsTypeMap
.Values.OfType<TsEnum>()
.Distinct(new EnumComparer())
.GroupBy(t => t.ModuleName)
.ToList();
var tsGenerator = new TypeScriptGenerator(typesToGenerate, enumsToGenerate, mCommandLineArgs.Debug);
var typeScriptText = tsGenerator.TransformText();
if (mCommandLineArgs.OutputToFile)
{
File.WriteAllText(mCommandLineArgs.OutputFilename, typeScriptText);
}
else
{
Console.Out.WriteLine(typeScriptText);
}
}
private static HashSet<Type> FindCommandParamtersAndDtoTypes(IEnumerable<Type> controllers)
{
var commandParamtersAndDtoTypes = new HashSet<Type>();
foreach (var controller in controllers)
{
foreach (
var method in
controller.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
.Where(IsActionMethod))
{
foreach (var type in method.CustomAttributes
.Where(cat => cat.AttributeType.FullName == "System.Web.Http.Description.ResponseTypeAttribute")
.Select(cat => ((Type) cat.ConstructorArguments[0].Value))
)
{
commandParamtersAndDtoTypes.Add(type);
}
foreach (var type in method.GetParameters()
.Select(p => p.ParameterType)
)
{
if (type.Name != "CancellationToken")
{
commandParamtersAndDtoTypes.Add(type);
}
}
}
}
return commandParamtersAndDtoTypes;
}
/*
mapping:
These type mappings need to correspond with the JSON serialization including any customization that might have been done.
custom mappings will be attempted first.
void <-
System.Void
number <-
System.Byte
System.SByte
System.Int16
System.UInt16
System.Int32
System.UInt32
System.Int64
System.UInt64
System.Double
System.Single
System.Decimal
(and all of their nullable counterparts)
string <-
System.Char (and nullable)
System.String
System.DateTime (and nullable)
System.DateTimeOffset (and nullable)
System.TimeSpan (and nullable)
System.Guid (and nullable)
System.Uri
boolean <-
System.Boolean
(and its nullable counterpart)
Enum (custom) <-
System.Enum
T[] <-
IEnumerable<T> (excluding the special-case of string which is IEnumerable<char>)
T[]
Custom class <-
Anything else
*
*/
private void FillAllMappings(List<TypeMapping> customMappings)
{
var mappings = new List<TypeMapping>
{
new TypeMapping
{
MatchesType = tr => (new[] {"System.String", "System.Char"}).Contains(tr.FullName),
DestinationType = "string",
DestinationAssignmentTemplate = "{0}"
},
new TypeMapping
{
MatchesType = tr => tr == typeof (char?),
DestinationType = "string",
DestinationAssignmentTemplate = "{0}"
},
new TypeMapping
{
MatchesType = tr => (new[]
{
"System.Byte", "System.SByte", "System.Int16", "System.UInt16", "System.Int32", "System.UInt32", "System.Int64",
"System.UInt64", "System.Double", "System.Single", "System.Decimal"
}).Contains(tr.FullName),
DestinationType = "number",
DestinationAssignmentTemplate = "{0}"
},
// System.Byte, System.SByte, System.Int16, System.UInt16, System.Int32, System.UInt32, System.Int64, System.UInt64, System.Double, System.Single, System.Decimal
new TypeMapping { MatchesType = tr => (new[]
{
typeof (Byte?), typeof (Byte?), typeof (SByte?), typeof (Int16?), typeof (UInt16?), typeof (Int32?), typeof (UInt32?), typeof (Int64?),
typeof (UInt64?), typeof (Double?), typeof (Single?), typeof (Decimal?)
}).Contains(tr), DestinationType = "number", DestinationAssignmentTemplate = "{0}" },
// Sytem.Void
// System.DateTime (and nullable), System.DateTimeOffset (and nullable)
// System.TimeSpan (and nullable)
new TypeMapping { MatchesType = tr => (new[] {typeof (DateTime), typeof(DateTime?)}).Contains(tr), DestinationType = "string", DestinationAssignmentTemplate = "{0}" },
new TypeMapping { MatchesType = tr => (new[] {typeof (DateTimeOffset), typeof(DateTimeOffset?)}).Contains(tr), DestinationType = "string", DestinationAssignmentTemplate = "{0}" },
new TypeMapping { MatchesType = tr => (new[] {typeof (TimeSpan), typeof(TimeSpan?)}).Contains(tr), DestinationType = "string", DestinationAssignmentTemplate = "{0}" },
// System.Guid (and nullable)
new TypeMapping { MatchesType = tr => tr == typeof (Guid), DestinationType = "string", DestinationAssignmentTemplate = "{0}" },
new TypeMapping { MatchesType = tr => tr == typeof (Guid?), DestinationType = "string", DestinationAssignmentTemplate = "{0}" },
// System.Uri
new TypeMapping { MatchesType = tr => tr == typeof (Uri), DestinationType = "string", DestinationAssignmentTemplate = "{0}" },
//
// System.Boolean (and nullable)
new TypeMapping { MatchesType = tr => tr == typeof (Boolean), DestinationType = "boolean", DestinationAssignmentTemplate = "{0}" },
new TypeMapping { MatchesType = tr => tr == typeof (Boolean?), DestinationType = "boolean", DestinationAssignmentTemplate = "{0}" },
};
mAllMappings = customMappings.Concat(mappings).ToList();
}
/// <summary>
/// This method doesn't seem to be necessary anymore now that we are NOT loading the dlls in Reflection Only mode.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
private Assembly OnCurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
try
{
var found = Assembly.Load(args.Name);
if (found != null)
return found;
}
catch (FileNotFoundException)
{
// TODO: write errors to standard error
//Console.Error.WriteLine();
}
catch (FileLoadException)
{
}
var dllPath = Path.Combine(mCommandLineArgs.AssemblyPath, assemblyName.Name + ".dll");
return Assembly.LoadFrom(dllPath);
}
private void MapToTsType(Type type)
{
if (mTypeToTsTypeMap.ContainsKey(type))
return;
var foundMapping = mAllMappings.FirstOrDefault(m => m.MatchesType.Invoke(type));
if (foundMapping != null)
{
mTypeToTsTypeMap[type] = new TsMappedType(foundMapping.DestinationType, foundMapping.DestinationAssignmentTemplate);
}
else
{
Type itemType;
Type keyItemType;
Type valueItemType;
if (IsDictionary(type, out keyItemType, out valueItemType))
{
MapToTsType(keyItemType);
MapToTsType(valueItemType);
mTypeToTsTypeMap[type] = new TsDictionary(mTypeToTsTypeMap[keyItemType], mTypeToTsTypeMap[valueItemType]);
}
else if (IsIEnumerableType(type, out itemType))
{
MapToTsType(itemType);
mTypeToTsTypeMap[type] = new TsCollection(mTypeToTsTypeMap[itemType]);
}
else if (type.IsEnum)
{
var enumType = new TsEnum(type.Namespace, type.Name);
var names = Enum.GetNames(type);
var valuesArray = Enum.GetValues(type);
var values = valuesArray.Cast<object>().Select(v => Convert.ToInt64(v)).ToArray();
for (int i = 0; i < names.Length; i++)
{
enumType.Members.Add(new KeyValuePair<string, long>(names[i], values[i]));
}
mTypeToTsTypeMap[type] = enumType;
}
// nullable enum
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
type.GetGenericArguments()[0].IsEnum)
{
var enumType = new TsEnum(type.GetGenericArguments()[0].Namespace, type.GetGenericArguments()[0].Name);
var names = Enum.GetNames(type.GetGenericArguments()[0]);
var valuesArray = Enum.GetValues(type.GetGenericArguments()[0]);
var values = valuesArray.Cast<object>().Select(v => Convert.ToInt64(v)).ToArray();
for (int i = 0; i < names.Length; i++)
{
enumType.Members.Add(new KeyValuePair<string, long>(names[i], values[i]));
}
mTypeToTsTypeMap[type] = enumType;
}
else
{
// Check if current TypeWithProperties has a base class that is not object
TsType baseTsType = null;
if (type.IsClass && type != typeof(Object) && type.BaseType != typeof(Object))
{
MapToTsType(type.BaseType);
baseTsType = mTypeToTsTypeMap[type.BaseType];
}
var tsType = new TsTypeWithProperties(type.Namespace, type.Name, baseTsType);
mTypeToTsTypeMap[type] = tsType;
var propertyInfos = type
.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.GetMethod.IsPublic && !p.GetMethod.GetParameters().Any());
// iterate properties.
foreach (var prop in propertyInfos)
{
MapToTsType(prop.PropertyType);
tsType.Properties.Add(new TsProperty { Name = prop.Name, TsType = mTypeToTsTypeMap[prop.PropertyType] });
}
}
}
}
private bool IsDictionary(Type type, out Type keyItemType, out Type valueItemType)
{
keyItemType = null;
valueItemType = null;
if (type == null)
return false;
var isDictionary = type.IsGenericType &&
(type.GetGenericTypeDefinition() == typeof(Dictionary<,>) || type.GetGenericTypeDefinition() == typeof(IDictionary<,>));
if (isDictionary)
{
keyItemType = type.GetGenericArguments()[0];
valueItemType = type.GetGenericArguments()[1];
}
return isDictionary;
}
private static bool IsIEnumerableType(Type type, out Type itemType)
{
itemType = null;
if (type == null)
return false;
var enumerableInterface = typeof(IEnumerable<>);
if (type.IsGenericType && type.GetGenericTypeDefinition() == enumerableInterface)
{
itemType = type.GenericTypeArguments[0];
return true;
}
var enumerable = type.GetInterface(enumerableInterface.FullName);
if (enumerable != null)
{
itemType = enumerable.GenericTypeArguments[0]; // type; //TODO: fill in
return true;
}
return false;
}
private static bool IsActionMethod(MethodInfo method)
{
if (!method.IsStatic && method.IsPublic && !method.IsSpecialName)
return true;
return false;
}
public bool IsApiController(Type type)
{
if (type.BaseType == null)
return false;
if (type.BaseType.Namespace == "System.Web.Http" && type.BaseType.Name == "ApiController")
return true;
return IsApiController(type.BaseType);
}
}
}