-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.dart
208 lines (181 loc) · 6.25 KB
/
generator.dart
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
import 'dart:io';
class Generator {
static const String CSV_LIST_SEPARATOR = '|';
static const String ENUM_CLASS_NAME = "ErrorEnum";
static const String FILE_SUFFIX_NAME = "dart";
String inputFile;
String outputDir;
List<String> attributeList;
Generator(String inputFile, String outputDir) {
this.inputFile = inputFile;
this.outputDir = outputDir;
}
void generate() {
Map<String, Map<String, String>> dataMap = obtainDataMap();
generateConstantFile(dataMap);
generateEnumFile(dataMap);
}
void generateConstantFile(Map<String, Map<String, String>> dataMap) {
for (String attribute in attributeList) {
String className = toBigHump(attribute);
String filePath = obtainOutputFilePath(className);
String content = obtainConstantTemplate(className, attribute, dataMap);
generateFile(filePath, content);
}
}
void generateEnumFile(Map<String, Map<String, String>> dataMap) {
String className = ENUM_CLASS_NAME;
String filePath = obtainOutputFilePath(className);
String content = obtainEnumTemplate(className, dataMap);
generateFile(filePath, content);
}
Map<String, Map<String, String>> obtainDataMap() {
var dataList = List<List<String>>();
List<String> rowList = parseFile(inputFile);
for (String row in rowList) {
dataList.add(splitRowData(row));
}
attributeList = dataList.removeAt(0);
var dataMap = Map<String, Map<String, String>>();
var attributeSize = attributeList.length;
for (var rowDataList in dataList) {
fillList(rowDataList, attributeSize);
String name = obtainLabelName(rowDataList[0]);
var itemMap = Map<String, String>();
dataMap[name] = itemMap;
for (int index = 0; index < attributeSize; index++) {
String attribute = attributeList[index];
itemMap[attribute] = rowDataList[index];
}
}
return dataMap;
}
List<String> splitRowData(String row) {
List<String> list = List<String>();
var rowArr = row.split(CSV_LIST_SEPARATOR);
for (String data in rowArr) {
list.add(data.trim());
}
return list;
}
List<String> fillList(List<String> list, int tagSize) {
if (list.length < tagSize) {
for (int index = 0; index < tagSize - list.length; index++) {
list.add("");
}
}
return list;
}
String obtainLabelName(String name) {
String regex = "\\s+";
return name.trim().replaceAll(RegExp(regex), "_").toUpperCase();
}
String obtainPropertyName(String property) {
return toSmallHump(property);
}
String toBigHump(String str) {
return toHump(str, false);
}
String toSmallHump(String str) {
return toHump(str, true);
}
String toHump(String str, bool isSmallHump) {
var builder = StringBuffer();
var strArr = str.split("[ _-]");
String subStr = "";
for (int index = 0; index < strArr.length; index++) {
subStr = strArr[index];
if (index != 0 || !isSmallHump) {
subStr = capitalize(subStr);
}
builder.write(subStr);
}
return builder.toString();
}
String capitalize(String str) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
String obtainConstantTemplate(String className, String attribute, Map<String, Map<String, String>> dataMap) {
var builder = StringBuffer();
builder.write("class $className {\n");
for (MapEntry<String, Map<String, String>> data in dataMap.entries) {
String format = "\tstatic const ${data.key} = \"${data.value[attribute]}\";\n";
builder.write(format);
}
builder.write("}\r\n");
return builder.toString();
}
String obtainEnumTemplate(String className, Map<String, Map<String, String>> dataMap) {
return "class $className {\n" +
"${obtainEnumDataList(dataMap, className)}\n" +
"${obtainEnumProperty()}\n" +
"${obtainEnumConstructor(className)}\n" +
"}";
}
String obtainEnumDataList(Map<String, Map<String, String>> dataMap, String className) {
var builder = StringBuffer();
List<String> propertyList = obtainEnumPropertyList();
var nameList = dataMap.keys.toList();
for (int index = 0; index < nameList.length; index++) {
String name = nameList[index];
var itemMap = dataMap[name];
String propertyArrString = propertyList.map((property) => "\"${itemMap[property]}\"").join(", ");
builder.write("\tstatic const $name = ${obtainPrivateConstructorName(className)}($propertyArrString);\n");
}
return builder.toString();
}
String obtainEnumConstructor(String className) {
var builder = StringBuffer();
List<String> propertyList = obtainEnumPropertyList();
String propertyArrString = propertyList.map((property) => "this.${obtainPropertyName(property)}").join(", ");
builder.write("\tconst ${obtainPrivateConstructorName(className)}($propertyArrString);\n");
return builder.toString();
}
String obtainPrivateConstructorName(String className) {
return "$className._init";
}
String obtainEnumProperty() {
var builder = StringBuffer();
List<String> propertyList = obtainEnumPropertyList();
for (String property in propertyList) {
builder.write("\tfinal ${obtainPropertyName(property)};\n");
}
return builder.toString();
}
List<String> obtainEnumPropertyList() {
var list = List<String>();
for (int index = 1; index < attributeList.length; index++) {
String tag = attributeList[index];
list.add(tag);
}
return list;
}
String obtainOutputFilePath(String name) {
return "$outputDir${Platform.pathSeparator}$name.$FILE_SUFFIX_NAME";
}
void generateFile(String filePath, String content) {
try {
File(filePath).create(recursive: true).then((file) {
file.writeAsString(content);
});
} catch (e) {
print(e);
}
}
List<String> parseFile(String filePath) {
var list = List<String>();
try {
File file = File(filePath);
list = file.readAsLinesSync();
} catch (e) {
print(e);
}
return list;
}
}
void main() {
var currentPath = Directory.current.path;
String inputFile = currentPath + Platform.pathSeparator + "error-code.csv";
String outputDir = currentPath + Platform.pathSeparator + "output" + Platform.pathSeparator + "dart";
Generator(inputFile, outputDir).generate();
}