-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_generator.dart
More file actions
93 lines (86 loc) · 2.93 KB
/
model_generator.dart
File metadata and controls
93 lines (86 loc) · 2.93 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
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as path;
const tag = '\$';
const src = './lib/json';
const dist = './lib/model/';
void walk() {
var srcDir = Directory(src);
var fileList = srcDir.listSync();
var template = File('./template.dart').readAsStringSync();
File file;
for (var f in fileList) {
if (FileSystemEntity.isFileSync(f.path)) {
file = File(f.path);
var paths = path.basename(f.path).split('.');
String name = paths.first;
if (name.startsWith('_')) return;
var map = json.decode(file.readAsStringSync());
// 避免导入重复的包使用Set
var set = Set<String>();
StringBuffer attr = StringBuffer();
(map as Map<String, dynamic>).forEach((key, value) {
if (key.startsWith('_')) return;
attr.write(getType(value, set, name) + '?');
attr.write(' ');
attr.write(key);
attr.write(';');
attr.write('\r\n ');
});
String className = name[0].toUpperCase() + name.substring(1);
var formatString = format(template, [name, className, className, attr.toString(), className, className, className]);
var _import = set.join(';\r\n');
_import = _import.isEmpty ? '' : ';';
formatString = formatString.replaceFirst('%t', _import);
File('$dist$name.dart').writeAsStringSync(formatString);
}
}
}
String changeFirstChar(String str, [bool upper = true]) {
return (upper ? str[0].toUpperCase() : str[0].toLowerCase()) + str.substring(1);
}
String getType(v, Set<String> set, String current) {
current = current.toLowerCase();
var verifyList = [v is bool, v is num, v is Map, v is List, v is String];
var types = ['bool', 'num', 'Map<String, dynamic>', 'List', 'String'];
var index = verifyList.indexWhere((element) => element);
if (index < 0 || index >= types.length) return 'String';
if (index == verifyList.length - 1) {
// 如果是字符串类型
v = v as String;
if (v.startsWith('$tag[]')) {
var className = changeFirstChar(v.substring(3), false);
if (className.toLowerCase() != current) {
set.add('import $className.dart');
}
return 'List<${changeFirstChar(className)}>';
} else if (v.startsWith(tag)) {
var fileName = changeFirstChar(v.substring(1), false);
if (fileName.toLowerCase() != current) {
set.add('import $fileName.dart');
}
return changeFirstChar(fileName);
}
return 'String';
} else {
return types[index];
}
}
String format(String fmt, List<Object> params) {
int matchIndex = 0;
String replace(Match m) {
if (matchIndex < params.length) {
switch (m[0]) {
case '%s':
return params[matchIndex++].toString();
}
} else {
throw Exception('Missing parameter for string format');
}
throw Exception("Invalid format string: " + m[0].toString());
}
return fmt.replaceAllMapped("%s", replace);;
}
void main() {
walk();
}