forked from Aviacode/nTsMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeScriptGenerator.cs
740 lines (583 loc) · 24.7 KB
/
TypeScriptGenerator.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 12.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace nTsMapper
{
using System.Globalization;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using nTsMapper.TypeScript;
using System;
/// <summary>
/// Class to produce the template output
/// </summary>
#line 1 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "12.0.0.0")]
public partial class TypeScriptGenerator : TypeScriptGeneratorBase
{
#line hidden
/// <summary>
/// Create the template output
/// </summary>
public virtual string TransformText()
{
this.Write("// ReSharper disable RedundantQualifier\r\n");
#line 9 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
// Enums
foreach (var module in mEnumsToGenerate) {
#line default
#line hidden
this.Write("module ");
#line 13 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(module.Key));
#line default
#line hidden
this.Write(" {\r\n");
#line 14 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
foreach (var tsType in module) {
#line default
#line hidden
this.Write("\texport enum ");
#line 17 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.TsTypeName));
#line default
#line hidden
this.Write(" {\r\n");
#line 18 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
foreach (var enumMember in tsType.Members) {
#line default
#line hidden
this.Write("\t\t");
#line 21 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(enumMember.Key));
#line default
#line hidden
this.Write(" = ");
#line 21 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(enumMember.Value));
#line default
#line hidden
this.Write(",\r\n");
#line 22 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\t}\r\n\r\n");
#line 27 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("}\r\n");
#line 31 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\r\n");
#line 35 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
foreach (var tsType in mTypesToGenerate) {
if (mDebugMode) {
#line default
#line hidden
this.Write("\r\n");
#line 40 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("module ");
#line 43 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.ModuleName));
#line default
#line hidden
this.Write(" {\r\n\texport interface I");
#line 44 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.TsTypeName));
#line default
#line hidden
this.Write(" ");
#line 44 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.BaseTsType != null ? "extends " + string.Format("{0}.I{1}", ((TsTypeWithProperties)tsType.BaseTsType).ModuleName, tsType.BaseTsType.TsTypeName) : ""));
#line default
#line hidden
this.Write(" {\r\n");
#line 45 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
foreach (var prop in tsType.Properties) {
#line default
#line hidden
this.Write("\t\t");
#line 48 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(CamelCase(prop.Name)));
#line default
#line hidden
this.Write(": ");
#line 48 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(prop.TsType.TsTypeReferenceName));
#line default
#line hidden
this.Write(";\r\n");
#line 49 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\t}\r\n\r\n");
#line 54 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
if (mDebugMode) {
#line default
#line hidden
this.Write("\t// InheritanceHierarchyLevel: ");
#line 57 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture((mDebugMode) ? ((TsTypeWithProperties)tsType).InheritanceHierarchyLevel.ToString() : ""));
#line default
#line hidden
this.Write("\r\n");
#line 58 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\texport class ");
#line 61 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.TsTypeName));
#line default
#line hidden
this.Write(" ");
#line 61 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.BaseTsType != null ? "extends " + tsType.BaseTsType.TsTypeReferenceName : ""));
#line default
#line hidden
this.Write(" implements I");
#line 61 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.TsTypeName));
#line default
#line hidden
this.Write(" {\r\n");
#line 62 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
foreach (var prop in tsType.Properties) {
#line default
#line hidden
this.Write("\t\t");
#line 65 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(CamelCase(prop.Name)));
#line default
#line hidden
this.Write(": ");
#line 65 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(prop.TsType.TsTypeReferenceName));
#line default
#line hidden
this.Write(";\r\n");
#line 66 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\t\tpublic static fromJSON(json: any) : ");
#line 69 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.TsTypeName));
#line default
#line hidden
this.Write(" {\r\n\t\t\tif (json === undefined)\r\n\t\t\t\treturn undefined;\r\n\t\t\tif (json === null)\r\n\t\t\t" +
"\treturn null;\r\n\r\n");
#line 75 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
if (tsType.BaseTsType == null) {
#line default
#line hidden
this.Write("\t\t\treturn {\r\n");
#line 79 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
for (var i = 0; i < tsType.Properties.Count; i++) {
var prop = tsType.Properties[i];
#line default
#line hidden
this.Write("\t\t\t\t");
#line 83 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(CamelCase(prop.Name)));
#line default
#line hidden
this.Write(": ");
#line 83 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(GetJsonAssignment(prop)));
#line default
#line hidden
#line 83 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
if (i < tsType.Properties.Count - 1) {
#line default
#line hidden
this.Write(",\r\n");
#line 85 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
}
#line default
#line hidden
this.Write("\r\n\t\t\t};\r\n");
#line 90 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
} else {
#line default
#line hidden
this.Write("\t\t\tvar baseObj: ");
#line 93 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.TsTypeName));
#line default
#line hidden
this.Write(" = <");
#line 93 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.TsTypeName));
#line default
#line hidden
this.Write("> ");
#line 93 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(tsType.BaseTsType.TsTypeReferenceName));
#line default
#line hidden
this.Write(".fromJSON(json);\r\n");
#line 94 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
foreach (var prop in tsType.Properties) {
#line default
#line hidden
this.Write("\t\t\tbaseObj.");
#line 97 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(CamelCase(prop.Name)));
#line default
#line hidden
this.Write(" = ");
#line 97 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(GetJsonAssignment(prop)));
#line default
#line hidden
this.Write(";\r\n");
#line 98 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\t\t\treturn baseObj;\r\n");
#line 102 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\t\t}\r\n\t}\r\n}\r\n");
#line 108 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
}
#line default
#line hidden
this.Write("\r\n");
#line 112 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
#line default
#line hidden
this.Write("// ReSharper restore RedundantQualifier\r\n");
return this.GenerationEnvironment.ToString();
}
#line 116 "C:\Development\ProCoder10\nTsMapper\TypeScriptGenerator.tt"
// camel case converter code borrowed from JSON.NET
string CamelCase(string identifier) {
if (string.IsNullOrEmpty(identifier))
return identifier;
if (!char.IsUpper(identifier[0]))
return identifier;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < identifier.Length; i++)
{
bool hasNext = (i + 1 < identifier.Length);
if ((i == 0 || !hasNext) || char.IsUpper(identifier[i + 1]))
{
char lowerCase;
lowerCase = char.ToLower(identifier[i], CultureInfo.InvariantCulture);
sb.Append(lowerCase);
}
else
{
sb.Append(identifier.Substring(i));
break;
}
}
return sb.ToString();
}
string GetJsonAssignment(TsProperty prop) {
return GetAssignmentExpression(prop.TsType, "json." + CamelCase(prop.Name));
}
string GetAssignmentExpression(TsType type, string sourceExpression) {
if(type is TsMappedType) return string.Format(((TsMappedType)type).AssignmentTemplate, sourceExpression);
if(type is TsTypeWithProperties) return type.TsTypeReferenceName + ".fromJSON(" + sourceExpression + ")";
if(type is TsCollection) return sourceExpression + " === null ? null : " + sourceExpression + ".map(o => " + GetAssignmentExpression(((TsCollection)type).TsItemType, "o") + ")";
return sourceExpression;
}
#line default
#line hidden
}
#line default
#line hidden
#region Base class
/// <summary>
/// Base class for this transformation
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "12.0.0.0")]
public class TypeScriptGeneratorBase
{
#region Fields
private global::System.Text.StringBuilder generationEnvironmentField;
private global::System.CodeDom.Compiler.CompilerErrorCollection errorsField;
private global::System.Collections.Generic.List<int> indentLengthsField;
private string currentIndentField = "";
private bool endsWithNewline;
private global::System.Collections.Generic.IDictionary<string, object> sessionField;
#endregion
#region Properties
/// <summary>
/// The string builder that generation-time code is using to assemble generated output
/// </summary>
protected System.Text.StringBuilder GenerationEnvironment
{
get
{
if ((this.generationEnvironmentField == null))
{
this.generationEnvironmentField = new global::System.Text.StringBuilder();
}
return this.generationEnvironmentField;
}
set
{
this.generationEnvironmentField = value;
}
}
/// <summary>
/// The error collection for the generation process
/// </summary>
public System.CodeDom.Compiler.CompilerErrorCollection Errors
{
get
{
if ((this.errorsField == null))
{
this.errorsField = new global::System.CodeDom.Compiler.CompilerErrorCollection();
}
return this.errorsField;
}
}
/// <summary>
/// A list of the lengths of each indent that was added with PushIndent
/// </summary>
private System.Collections.Generic.List<int> indentLengths
{
get
{
if ((this.indentLengthsField == null))
{
this.indentLengthsField = new global::System.Collections.Generic.List<int>();
}
return this.indentLengthsField;
}
}
/// <summary>
/// Gets the current indent we use when adding lines to the output
/// </summary>
public string CurrentIndent
{
get
{
return this.currentIndentField;
}
}
/// <summary>
/// Current transformation session
/// </summary>
public virtual global::System.Collections.Generic.IDictionary<string, object> Session
{
get
{
return this.sessionField;
}
set
{
this.sessionField = value;
}
}
#endregion
#region Transform-time helpers
/// <summary>
/// Write text directly into the generated output
/// </summary>
public void Write(string textToAppend)
{
if (string.IsNullOrEmpty(textToAppend))
{
return;
}
// If we're starting off, or if the previous text ended with a newline,
// we have to append the current indent first.
if (((this.GenerationEnvironment.Length == 0)
|| this.endsWithNewline))
{
this.GenerationEnvironment.Append(this.currentIndentField);
this.endsWithNewline = false;
}
// Check if the current text ends with a newline
if (textToAppend.EndsWith(global::System.Environment.NewLine, global::System.StringComparison.CurrentCulture))
{
this.endsWithNewline = true;
}
// This is an optimization. If the current indent is "", then we don't have to do any
// of the more complex stuff further down.
if ((this.currentIndentField.Length == 0))
{
this.GenerationEnvironment.Append(textToAppend);
return;
}
// Everywhere there is a newline in the text, add an indent after it
textToAppend = textToAppend.Replace(global::System.Environment.NewLine, (global::System.Environment.NewLine + this.currentIndentField));
// If the text ends with a newline, then we should strip off the indent added at the very end
// because the appropriate indent will be added when the next time Write() is called
if (this.endsWithNewline)
{
this.GenerationEnvironment.Append(textToAppend, 0, (textToAppend.Length - this.currentIndentField.Length));
}
else
{
this.GenerationEnvironment.Append(textToAppend);
}
}
/// <summary>
/// Write text directly into the generated output
/// </summary>
public void WriteLine(string textToAppend)
{
this.Write(textToAppend);
this.GenerationEnvironment.AppendLine();
this.endsWithNewline = true;
}
/// <summary>
/// Write formatted text directly into the generated output
/// </summary>
public void Write(string format, params object[] args)
{
this.Write(string.Format(global::System.Globalization.CultureInfo.CurrentCulture, format, args));
}
/// <summary>
/// Write formatted text directly into the generated output
/// </summary>
public void WriteLine(string format, params object[] args)
{
this.WriteLine(string.Format(global::System.Globalization.CultureInfo.CurrentCulture, format, args));
}
/// <summary>
/// Raise an error
/// </summary>
public void Error(string message)
{
System.CodeDom.Compiler.CompilerError error = new global::System.CodeDom.Compiler.CompilerError();
error.ErrorText = message;
this.Errors.Add(error);
}
/// <summary>
/// Raise a warning
/// </summary>
public void Warning(string message)
{
System.CodeDom.Compiler.CompilerError error = new global::System.CodeDom.Compiler.CompilerError();
error.ErrorText = message;
error.IsWarning = true;
this.Errors.Add(error);
}
/// <summary>
/// Increase the indent
/// </summary>
public void PushIndent(string indent)
{
if ((indent == null))
{
throw new global::System.ArgumentNullException("indent");
}
this.currentIndentField = (this.currentIndentField + indent);
this.indentLengths.Add(indent.Length);
}
/// <summary>
/// Remove the last indent that was added with PushIndent
/// </summary>
public string PopIndent()
{
string returnValue = "";
if ((this.indentLengths.Count > 0))
{
int indentLength = this.indentLengths[(this.indentLengths.Count - 1)];
this.indentLengths.RemoveAt((this.indentLengths.Count - 1));
if ((indentLength > 0))
{
returnValue = this.currentIndentField.Substring((this.currentIndentField.Length - indentLength));
this.currentIndentField = this.currentIndentField.Remove((this.currentIndentField.Length - indentLength));
}
}
return returnValue;
}
/// <summary>
/// Remove any indentation
/// </summary>
public void ClearIndent()
{
this.indentLengths.Clear();
this.currentIndentField = "";
}
#endregion
#region ToString Helpers
/// <summary>
/// Utility class to produce culture-oriented representation of an object as a string.
/// </summary>
public class ToStringInstanceHelper
{
private System.IFormatProvider formatProviderField = global::System.Globalization.CultureInfo.InvariantCulture;
/// <summary>
/// Gets or sets format provider to be used by ToStringWithCulture method.
/// </summary>
public System.IFormatProvider FormatProvider
{
get
{
return this.formatProviderField ;
}
set
{
if ((value != null))
{
this.formatProviderField = value;
}
}
}
/// <summary>
/// This is called from the compile/run appdomain to convert objects within an expression block to a string
/// </summary>
public string ToStringWithCulture(object objectToConvert)
{
if ((objectToConvert == null))
{
throw new global::System.ArgumentNullException("objectToConvert");
}
System.Type t = objectToConvert.GetType();
System.Reflection.MethodInfo method = t.GetMethod("ToString", new System.Type[] {
typeof(System.IFormatProvider)});
if ((method == null))
{
return objectToConvert.ToString();
}
else
{
return ((string)(method.Invoke(objectToConvert, new object[] {
this.formatProviderField })));
}
}
}
private ToStringInstanceHelper toStringHelperField = new ToStringInstanceHelper();
/// <summary>
/// Helper to produce culture-oriented representation of an object as a string
/// </summary>
public ToStringInstanceHelper ToStringHelper
{
get
{
return this.toStringHelperField;
}
}
#endregion
}
#endregion
}