This repository was archived by the owner on Jun 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCodeDomUtils.cs
More file actions
456 lines (400 loc) · 17.8 KB
/
CodeDomUtils.cs
File metadata and controls
456 lines (400 loc) · 17.8 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.CSharp;
using Microsoft.JScript;
using Microsoft.VisualBasic;
namespace ServiceStack.Translators.Generator
{
public enum CodeLang
{
CSharp,
JScript,
Vb,
Python,
Boo,
FSharp,
//Java //JSharp,
}
public static class CodeDomUtils
{
public static ICodeGenerator CreateGenerator(CodeLang langType)
{
//Need to comment out the languages that require external dependencies otherwise the program can't run without them
switch (langType)
{
case CodeLang.CSharp:
return new CSharpCodeProvider().CreateGenerator();
//case CodeLang.FSharp:
// return new FSharpCodeProvider().CreateGenerator();
case CodeLang.JScript:
return new JScriptCodeProvider().CreateGenerator();
case CodeLang.Vb:
return new VBCodeProvider().CreateGenerator();
//case CodeLang.Boo:
// return new BooCodeProvider().CreateGenerator();
default:
throw new NotSupportedException(string.Format("{0} is not currently supported", langType));
}
}
/// <summary>
/// type.New() == new T()
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public static CodeObjectCreateExpression New(this Type type)
{
return new CodeObjectCreateExpression(new CodeTypeReference(type));
}
public static CodeObjectCreateExpression New(this string type)
{
return new CodeObjectCreateExpression(new CodeTypeReference(type));
}
/// <summary>
/// Declares the method.
///
/// methodName.DeclareMethod(typeof(string), MemberAttributes.Public):
/// public virtual string methodName()
///
/// methodName.DeclareMethod(typeof(string), MemberAttributes.Public, typeof(int).Param("argument")):
/// public virtual string methodName(int argument)
///
/// </summary>
/// <param name="methodName">Name of the method.</param>
/// <param name="returnType">Type of the return.</param>
/// <param name="attributes">The attributes.</param>
/// <param name="methodParams">The method params.</param>
/// <returns></returns>
public static CodeMemberMethod DeclareMethod(this string methodName, Type returnType, MemberAttributes attributes,
params CodeParameterDeclarationExpression[] methodParams)
{
return DeclareMethod(methodName, new CodeTypeReference(returnType.FullName), attributes, methodParams);
}
public static CodeMemberMethod DeclareMethod(this string methodName, CodeTypeReference returnType, MemberAttributes attributes,
params CodeParameterDeclarationExpression[] methodParams)
{
var method = new CodeMemberMethod {
Name = methodName,
ReturnType = returnType,
Attributes = attributes,
};
foreach (var methodParam in methodParams)
{
method.Parameters.Add(methodParam);
}
return method;
}
public static CodeVariableDeclarationStatement DeclareVar(this Type type, string variableName)
{
return new CodeVariableDeclarationStatement(type, variableName, New(type));
}
public static CodeVariableDeclarationStatement DeclareVar(this Type type, string variableName, CodeExpression initExpression)
{
return new CodeVariableDeclarationStatement(type, variableName, initExpression);
}
public static CodeMethodReturnStatement Return(this CodeVariableDeclarationStatement expression)
{
return new CodeMethodReturnStatement(new CodeArgumentReferenceExpression(expression.Name));
}
public static CodeMethodReturnStatement Return(this CodeParameterDeclarationExpression expression)
{
return new CodeMethodReturnStatement(new CodeArgumentReferenceExpression(expression.Name));
}
public static CodeMethodReturnStatement Return(this CodeMethodInvokeExpression expression)
{
return new CodeMethodReturnStatement(expression);
}
public static CodeParameterDeclarationExpression Param(this Type type, string paramName)
{
return new CodeParameterDeclarationExpression(type, paramName);
}
public static CodeConditionStatement ReturnNullIfNull(this CodeParameterDeclarationExpression expression)
{
return new CodeConditionStatement(
new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression(expression.Name), CodeBinaryOperatorType.IdentityEquality,
new CodePrimitiveExpression(null)),
new[] { new CodeMethodReturnStatement(new CodePrimitiveExpression(null)) });
}
public static CodeConditionStatement IfIsNotNull(this CodeVariableDeclarationStatement expression,
params CodeExpression[] trueExpressions)
{
var trueStatements = trueExpressions.ToList().ConvertAll(x => new CodeExpressionStatement(x)).ToArray();
return new CodeConditionStatement(
new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression(expression.Name), CodeBinaryOperatorType.IdentityInequality,
new CodePrimitiveExpression(null)),
trueStatements);
}
public static CodeConditionStatement IfIsNotNull(this CodePropertyReferenceExpression expression,
params CodeStatement[] trueStatements)
{
return new CodeConditionStatement(
new CodeBinaryOperatorExpression(
expression, CodeBinaryOperatorType.IdentityInequality,
new CodePrimitiveExpression(null)),
trueStatements);
}
/// <summary>
/// type.Param("paramName", typeof(List>T<)):
/// [Method(..] List>T< paramName
/// </summary>
/// <param name="type">The type.</param>
/// <param name="paramName">Name of the param.</param>
/// <param name="genericTypeDefinitionName">Name of the generic type definition.</param>
/// <returns></returns>
public static CodeParameterDeclarationExpression Param(this Type type, string paramName, Type genericTypeDefinitionName)
{
return new CodeParameterDeclarationExpression(
new CodeTypeReference(genericTypeDefinitionName.FullName, new CodeTypeReference(type)), paramName);
}
public static CodeAssignStatement Assign(this CodeParameterDeclarationExpression property, string assignTo, string assignFrom)
{
return new CodeAssignStatement(
new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(property.Name), assignTo),
new CodeArgumentReferenceExpression(assignFrom));
}
public static CodeAssignStatement Assign(this CodeParameterDeclarationExpression property, string assignTo, CodeVariableReferenceExpression assignFrom)
{
return new CodeAssignStatement(
new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(property.Name), assignTo),
assignFrom);
}
public static CodeAssignStatement Assign(this CodeParameterDeclarationExpression property, string assignTo, CodePropertyReferenceExpression assignFrom)
{
return new CodeAssignStatement(
new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(property.Name), assignTo), assignFrom);
}
public static CodeAssignStatement Assign(this CodeParameterDeclarationExpression property, CodePropertyReferenceExpression assignTo, CodeExpression assignFrom)
{
return new CodeAssignStatement(
new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(property.Name), assignTo.PropertyName), assignFrom);
}
//UpdateModelMethod: model.Assign(property.Name, property.Name.ThisProperty().Call("ToModel"))
public static CodeAssignStatement Assign(this CodeParameterDeclarationExpression property, string assignTo, CodeMethodInvokeExpression methodResult)
{
return new CodeAssignStatement(
new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(property.Name), assignTo),
methodResult);
}
public static CodeAssignStatement Assign(this CodeVariableDeclarationStatement property, string assignTo, string assignFrom)
{
return new CodeAssignStatement(
new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(property.Name), assignTo),
new CodeArgumentReferenceExpression(assignFrom));
}
//to.Assign(property.Name, from.RefProperty(property.Name).Call("ToModel"))
public static CodeAssignStatement Assign(this CodeVariableDeclarationStatement property, string assignTo, CodeExpression assignExpression)
{
return new CodeAssignStatement(
new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(property.Name), assignTo),
assignExpression);
}
public static CodeAssignStatement Assign(this CodeVariableDeclarationStatement property, CodeExpression assignTo, CodeExpression expression)
{
return new CodeAssignStatement(assignTo, expression);
}
public static CodeAssignStatement Assign(this CodePropertyReferenceExpression property, CodeExpression expression)
{
return new CodeAssignStatement(property, expression);
}
public static CodePropertyReferenceExpression ThisProperty(this string propertyName)
{
return new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), propertyName);
}
public static CodePropertyReferenceExpression RefProperty(this string propertyName, CodeExpression field)
{
return new CodePropertyReferenceExpression(field, propertyName);
}
public static CodePropertyReferenceExpression RefProperty(this CodeExpression field, string propertyName)
{
return new CodePropertyReferenceExpression(field, propertyName);
}
public static CodeTypeOfExpression RefType(this CodeParameterDeclarationExpression property)
{
return new CodeTypeOfExpression(property.Type);
}
public static CodeVariableReferenceExpression RefArg(this CodeParameterDeclarationExpression property)
{
return new CodeVariableReferenceExpression(property.Name);
}
/// <summary>
/// Adds the 'Extension' Attribute to the parameter.
///
/// Warning: this modfies and returns the param provided.
/// </summary>
/// <param name="property">The property.</param>
/// <returns></returns>
public static CodeParameterDeclarationExpression ExtensionVar(this CodeParameterDeclarationExpression property)
{
property.CustomAttributes.Add(new CodeAttributeDeclaration("Extension"));
return property;
}
/// <summary>
/// Refs the argument.
///
/// "arg".RefArgument() == arg.
/// </summary>
/// <param name="argumentName">Name of the argument.</param>
/// <returns></returns>
public static CodeArgumentReferenceExpression RefArgument(this string argumentName)
{
return new CodeArgumentReferenceExpression(argumentName);
}
/// <summary>
/// Refs the property.
///
/// typeof(int).Param("from").RefProperty("propertyName"):
/// [void Method(...] int from
/// from.propertyName
/// </summary>
/// <param name="param">The param.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns></returns>
public static CodePropertyReferenceExpression RefProperty(this CodeParameterDeclarationExpression param, string propertyName)
{
return new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression(param.Name), propertyName);
}
public static CodePropertyReferenceExpression RefProperty(this CodePropertyReferenceExpression param, string propertyName)
{
return new CodePropertyReferenceExpression(param, propertyName);
}
/// <summary>
/// Calls the specified member.
///
/// "propertyName".ThisProperty().Call("ToModel") == this.propertyName.ToModel()
///
/// </summary>
/// <param name="member">The member.</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="methodParams">The method params.</param>
/// <returns></returns>
public static CodeMethodInvokeExpression Call(this CodePropertyReferenceExpression member, string methodName, params CodeExpression[] methodParams)
{
return new CodeMethodInvokeExpression(member, methodName, methodParams);
}
public static CodeMethodInvokeExpression Call(this string methodName, params CodeExpression[] methodParams)
{
return new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), methodName, methodParams);
}
public static CodeMethodInvokeExpression Call(this CodeVariableDeclarationStatement var, string methodName, params CodeExpression[] methodParams)
{
return new CodeMethodInvokeExpression(var.Name.RefArgument(), methodName, methodParams);
}
public static CodeMethodInvokeExpression Call(this CodeParameterDeclarationExpression var, string methodName, params CodeExpression[] methodParams)
{
return new CodeMethodInvokeExpression(var.Name.RefArgument(), methodName, methodParams);
}
/// <summary>
/// Calls the specified method name.
///
/// var variable = type.DeclareVar("argument");
/// "methodName".Call(variable) == this.UpdateModel(argument);
/// </summary>
/// <param name="methodName">Name of the method.</param>
/// <param name="varMethodParams">The var method params.</param>
/// <returns></returns>
public static CodeMethodInvokeExpression Call(this string methodName, params CodeVariableDeclarationStatement[] varMethodParams)
{
var methodParams = varMethodParams.ToList().ConvertAll(x => x.Name.RefArgument()).ToArray();
return new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), methodName, methodParams);
}
public static CodeMethodInvokeExpression Call(this Type type, string methodName, params CodeExpression[] methodParams)
{
return new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(type), methodName, methodParams);
}
public static CodeMethodInvokeExpression CallStatic(this string typeName, string methodName, params CodeExpression[] methodParams)
{
return new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeName), methodName, methodParams);
}
public static CodeMethodInvokeExpression Call(this Type type, string methodName, params CodeVariableDeclarationStatement[] varMethodParams)
{
var methodParams = varMethodParams.ToList().ConvertAll(x => x.Name.RefArgument()).ToArray();
return new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(type), methodName, methodParams);
}
public static CodeMethodInvokeExpression CallStatic(this string methodName, params CodeVariableDeclarationStatement[] varMethodParams)
{
var methodParams = varMethodParams.ToList().ConvertAll(x => x.Name.RefArgument()).ToArray();
return new CodeMethodInvokeExpression(null, methodName, methodParams);
}
public static CodeTypeReference RefGeneric(this Type type, Type genericTypeDefinition)
{
return new CodeTypeReference(genericTypeDefinition.FullName, new CodeTypeReference(type));
}
public static CodeMethodInvokeExpression CallGeneric(this Type type, string methodName, CodeTypeReference[] genericMethodDefinitions, params CodeExpression[] methodParams)
{
return new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression(type), methodName, genericMethodDefinitions),
methodParams);
}
public static CodeTypeReference GenericDefinition(this Type type)
{
return new CodeTypeReference(type);
}
//"to".DeclareGenericVar(type, typeof(List<>)):
//
//List<type> to = new ServiceStack.Translators.Generator.Tests.Support.DataContract.Customer();
public static CodeVariableDeclarationStatement DeclareGenericVar(this string variableName, Type type, Type genericTypeDefinition)
{
return new CodeVariableDeclarationStatement(
type.RefGeneric(genericTypeDefinition), variableName, type.NewGeneric(genericTypeDefinition));
}
/// <summary>
/// Declares the generic var.
///
/// IEnumerable<itemType> iter = enumerableVar.GetEnumerator()
/// NOTE: Always leave the genericTypeDefinition at the end so we can use params[] to support more complex generic types.
/// </summary>
/// <param name="variableName">Name of the variable.</param>
/// <param name="type">The type.</param>
/// <param name="initExpression">The init expression.</param>
/// <param name="genericTypeDefinition">The generic type definition.</param>
/// <returns></returns>
public static CodeVariableDeclarationStatement DeclareGenericVar(this string variableName, Type type, CodeExpression initExpression, Type genericTypeDefinition)
{
return new CodeVariableDeclarationStatement(
type.RefGeneric(genericTypeDefinition), variableName, initExpression);
}
public static CodeObjectCreateExpression NewGeneric(this Type type, Type genericTypeDefinition)
{
return new CodeObjectCreateExpression(type.RefGeneric(genericTypeDefinition));
}
public static CodePropertyReferenceExpression RefProperty(this CodeVariableDeclarationStatement varDeclaration, string propertyName)
{
return new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression(varDeclaration.Name), propertyName);
}
public static CodeVariableReferenceExpression RefVar(this CodeVariableDeclarationStatement varDeclaration)
{
return new CodeVariableReferenceExpression(varDeclaration.Name);
}
public static CodeIterationStatement ForEach(this CodeParameterDeclarationExpression enumerableVar, Type itemType, out CodeVariableDeclarationStatement item)
{
//IEnumerator<itemType> iter = enumerableVar.GetEnumerator()
var iter = "iter".DeclareGenericVar(itemType, enumerableVar.Call("GetEnumerator"), typeof(IEnumerator<>));
var iterStatement = new CodeIterationStatement {
InitStatement = iter,
TestExpression = iter.Call("MoveNext"),
IncrementStatement = new CodeSnippetStatement(""),
};
item = itemType.DeclareVar("item", iter.RefProperty("Current"));
iterStatement.Statements.Add(item);
return iterStatement;
}
/// <summary>
/// Calls the specified method name.
///
/// "methodName".Call(type.New()) == this.UpdateModel(new T());
/// </summary>
/// <param name="methodName">Name of the method.</param>
/// <param name="methodParams">The method params.</param>
/// <returns></returns>
public static CodeMethodInvokeExpression Call(this string methodName, params CodeObjectCreateExpression[] methodParams)
{
return new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), methodName, methodParams);
}
}
}