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
| import 'dart:convert'; import 'dart:io'; import 'package:path/path.dart' as path;
const TAG = "\$"; const SRC = "./json"; const DIST = "lib/models/";
void walk() { var src = new Directory(SRC); var list = src.listSync(); var template = new File("template/template.tmpl").readAsStringSync(); File file; list.forEach((f) { if (FileSystemEntity.isFileSync(f.path)) { file = new File(f.path); var paths = path.basename(f.path).split("."); String name = paths.first; if (paths.last.toLowerCase() != "json" || name.startsWith("_")) return; if (name.startsWith("_")) return; var map = json.decode(file.readAsStringSync()); var set = new Set<String>(); StringBuffer attrs = new StringBuffer(); List<String> params = []; (map as Map<String, dynamic>).forEach((key, v) { if (key.startsWith("_")) return; attrs.write(getType(v, set, name)); attrs.write(" "); attrs.write(key); attrs.writeln(";"); attrs.write(" "); params.add("required this.$key"); }); String conParams = "{${params.join(",")}}"; String className = name[0].toUpperCase() + name.substring(1); var dist = format(template, [ name, className, className, attrs.toString(), className, className, className ]); var _import = set.join(";\r\n"); _import += _import.isEmpty ? "" : ";"; dist = dist.replaceFirst("%t", _import); dist = dist.replaceFirst("%c", conParams); new File("$DIST$name.dart").writeAsStringSync(dist); } }); }
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(); if (v is bool) { return "bool"; } else if (v is int) { return "int"; } else if (v is num) { return "num"; } else if (v is Map) { return "Map<String,dynamic>"; } else if (v is List) { return "List"; } else if (v is String) { if (v.startsWith("$TAG") && v.endsWith("$TAG")) { return v.substring(1, v.length - 1); } 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 "String"; } }
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 new Exception("Missing parameter for string format"); } throw new Exception("Invalid format string: " + m[0].toString()); }
return fmt.replaceAllMapped("%s", replace); }
void main() { walk(); }
|