-
Notifications
You must be signed in to change notification settings - Fork 0
/
Converter.cs
200 lines (171 loc) · 6.94 KB
/
Converter.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
using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Collections.Generic;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
namespace EfCoreTsGen
{
public static class Extensions
{
public static ClassAndLines GetClassAndLines(this string csFilePath)
{
List<string> allLines = File.ReadAllLines(csFilePath).ToList<string>();
int classNameIndex = allLines.FindIndex(l => l.Contains(" class "));
string className = allLines.Where(l => l.Contains(" class ")).Select(l => l.Substring(l.LastIndexOf(' ') + 1)).Single();
//Get lines inside class
List<string> filteredLines = allLines.GetRange(classNameIndex + 2, allLines.Count() - (classNameIndex + 3));
filteredLines = filteredLines.Where(l => l.Length > 0 && !l.Contains("//")).ToList(); //Remove blank lines and comments
return new ClassAndLines() { ClassName = className, Lines = filteredLines };
}
public static string BuildTsStr(this ClassAndLines classAndLines, List<string> allClassNames)
{
string tsClassStr = string.Empty;
tsClassStr += "export class " + classAndLines.ClassName + " {\r\n";
List<string> modifiedLines = new List<string>();
//Remove get and set
//Remove public
//Remove virtual
foreach (string l in classAndLines.Lines)
{
string lineToAdd = l;
List<string> removeMeList = new List<string>() { " { get; set; }", "virtual", "public", "{ get; }", " = new List<*" };
foreach (string removeMe in removeMeList)
{
lineToAdd = lineToAdd.RemoveThis(removeMe);
}
modifiedLines.Add(lineToAdd.Trim());
}
//Remove constructor if exists
int endOfConstructorIndex = modifiedLines.FindLastIndex(l => l.Contains("}"));
if (endOfConstructorIndex != -1)
{
modifiedLines = modifiedLines.GetRange(endOfConstructorIndex + 1, modifiedLines.Count() - (endOfConstructorIndex + 1));
}
List<string> importList = new List<string>();
foreach (string line in modifiedLines)
{
string typeStr = line.Substring(0, line.IndexOf(" "));
string propName = line.Substring(line.LastIndexOf(' ') + 1);
//Convert to camelcase
var cc = new CamelCasePropertyNamesContractResolver();
propName = cc.GetResolvedPropertyName(propName);
tsClassStr += $" public {propName}: {typeStr.TypeConversion(allClassNames, importList)};\r\n";
}
tsClassStr += "}";
//add imports
string importStr = "\r\n";
//Converting to hashset removes duplicates
foreach (string import in importList.ToHashSet<string>())
{
//don't import self
if (!(classAndLines.ClassName==import))
{
importStr += "import { " + import + " } from './index';\r\n";
}
}
tsClassStr = importStr + "\r\n" + tsClassStr;
return tsClassStr;
}
private static string TypeConversion(this string csTypeStr, List<string> allClassNames, List<string> importList)
{
//Default to number since lots of C# stuff rolls up to this in ts
string tsTypeStr = string.Empty;
switch (csTypeStr)
{
case "bool":
case "boolean":
tsTypeStr = "boolean = false";
break;
case "boolean?":
case "bool?":
tsTypeStr = "boolean = null";
break;
case "string":
tsTypeStr = "string = null";
break;
case "DateOnly":
case "DateTime":
case "DateTimeOffset":
tsTypeStr = "Date = new Date(0)";
break;
case "DateOnly?":
case "DateTime?":
case "DateTimeOffset?":
tsTypeStr = "Date = null";
break;
case "int?":
case "decimal?":
tsTypeStr = "number = null";
break;
case "byte[]":
tsTypeStr = "Array<any> = []";
break;
case "Guid":
tsTypeStr = "string = '00000000-0000-0000-0000-000000000000'";
break;
case "Guid?":
tsTypeStr = "string = null";
break;
default:
tsTypeStr = "number = 0";
if (csTypeStr.Contains("ICollection"))
{
int startIndex = csTypeStr.IndexOf('<')+1;
int lengthToGet = csTypeStr.IndexOf('>') - startIndex;
tsTypeStr = csTypeStr.Substring(startIndex,lengthToGet);
importList.Add(tsTypeStr);
tsTypeStr = tsTypeStr + "[] = []";
}
else if (allClassNames.Contains(csTypeStr))
{
importList.Add(csTypeStr);
tsTypeStr = csTypeStr + " = null";
}
break;
}
return tsTypeStr;
}
private static string RemoveThis(this string fullStr, string removeMe)
{
//if last char is "*" remove rest of line
string test = removeMe.EndsWith('*') ? removeMe.Remove(removeMe.Length-1) : removeMe;
int index = fullStr.IndexOf(test);
string retStr = fullStr;
if (index > 0)
{
if (removeMe.EndsWith('*'))
{
retStr = fullStr.Remove(index);
}
else
{
retStr = fullStr.Remove(index, removeMe.Length);
}
}
return retStr;
}
public static string GetStringBetweenCharacters(this string input, char charFrom, char charTo)
{
int posFrom = input.IndexOf(charFrom);
if (posFrom != -1) //if found char
{
int posTo = input.IndexOf(charTo, posFrom + 1);
if (posTo != -1) //if found char
{
return input.Substring(posFrom + 1, posTo - posFrom - 1);
}
}
return string.Empty;
}
}
}
public class ClassAndLines
{
public string ClassName { get; set; }
public List<string> Lines { get; set; }
}