-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathgenIntlKeys.dart
62 lines (54 loc) · 1.62 KB
/
genIntlKeys.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
import 'dart:io';
import 'dart:isolate';
import 'package:glob/glob.dart';
import 'package:glob/list_local_fs.dart';
import 'utils.dart';
// This file for generating keys for message files
main(List<String> args) async {
print("GENERATE CLASS KEYS FOR YOUR LOCALIZE MESSAGES");
final dartFile = new Glob("lib/**/**.dart");
List<FileSystemEntity> files = dartFile.listSync();
for (var entity in files) {
if (entity.path.contains(normalize("/messages/strings.dart"))) {
final keysFile = new File(entity.path.replaceAll('strings', 'keys'));
ReceivePort port = new ReceivePort();
final raw = await getMessagesViaIsolate(entity, port);
final scope = raw["scope"];
final Map messages = raw["strings"];
final keys = messages.keys
.map(
(str) {
final key = str.toString();
final parsedKey = key.split('/').last;
return "\tstatic String $parsedKey = '\$scope\/$parsedKey';";
},
)
.toList()
.join('\n');
final classInString = classTemplate(scope, keys);
await keysFile.writeAsString(classInString);
}
}
}
Future<Map> getMessagesViaIsolate(
FileSystemEntity entity,
ReceivePort port,
) async {
await Isolate.spawnUri(
Uri.parse(entity.resolveSymbolicLinksSync().replaceAll("\\", "/")),
[],
port.sendPort,
);
final Map messages = await port.first;
port.close();
return messages;
}
String classTemplate(scope, keys) {
return '''
// This is an auto generated file. Do not make any change on this.
const scope = '$scope';
abstract class ${scope}Messages {
$keys
}
''';
}