CAKE-272 | refactored script

OleksandrSobol committed Mar 24, 2021 at 19:55 UTC b009b2a6e4ae67c8109b0c5435172a7bdd4805ba
1 file changed +64 -61
tool/generate_localization.dart
+64 -61
@@ -28,85 +28,88 @@ Future<void> main(List<String> args) async {
28 : <String, dynamic> {srcDir : inputPath};
29
30 extraInfo.forEach((key, dynamic value) async {
31 - if (key == srcDir) {
32 - final dirPath = value as String;
33 - final dir = Directory(dirPath);
31 + if (key != srcDir) {
32 + print('Wrong key: $key');
33 + return;
34 + }
35
35 - if (await dir.exists()) {
36 - final localePath = <String, dynamic>{};
37 - await dir.list(recursive: false).forEach((element) {
38 - try {
39 - final shortLocale = element.path.split('_',)[1].split('.')[0];
40 - localePath[shortLocale] = element.path;
41 - } catch (e) {
42 - print('Wrong file: ${element.path}');
43 - }
44 - });
36 + final dirPath = value as String;
37 + final dir = Directory(dirPath);
38
46 - if (localePath.keys.contains(defaultLocale)) {
47 - try {
48 - var output = '';
49 - var locales = 'const locales = [';
39 + if (!await dir.exists()) {
40 + print('Wrong directory path: $dirPath');
41 + return;
42 + }
43
51 - output += part1;
52 - output += textDirectionDeclaration;
44 + final localePath = <String, dynamic>{};
45 + await dir.list(recursive: false).forEach((element) {
46 + try {
47 + final shortLocale = element.path.split('_',)[1].split('.')[0];
48 + localePath[shortLocale] = element.path;
49 + } catch (e) {
50 + print('Wrong file: ${element.path}');
51 + }
52 + });
53
54 - var inputContent =
55 - File(localePath[defaultLocale].toString()).readAsStringSync();
56 - var config = json.decode(inputContent) as Map<String, dynamic>;
54 + if (!localePath.keys.contains(defaultLocale)) {
55 + print("Locale list doesn't contain $defaultLocale");
56 + return;
57 + }
58
58 - output += localizedStrings(config: config, hasOverride: false);
59 - output += '}' + '\n\n';
59 + try {
60 + var output = '';
61 + var locales = 'const locales = [';
62
61 - localePath.forEach((key, dynamic value) {
62 - inputContent = File(localePath[key].toString()).readAsStringSync();
63 - config = json.decode(inputContent) as Map<String, dynamic>;
63 + output += part1;
64 + output += textDirectionDeclaration;
65
65 - locales += "'$key', ";
66 + var inputContent =
67 + File(localePath[defaultLocale].toString()).readAsStringSync();
68 + var config = json.decode(inputContent) as Map<String, dynamic>;
69
67 - output += 'class \$$key extends S {' + '\n';
68 - output += ' const \$$key();' + '\n';
70 + output += localizedStrings(config: config, hasOverride: false);
71 + output += '}' + '\n\n';
72
70 - if (key != defaultLocale) {
71 - output += textDirectionDeclaration;
72 - output += localizedStrings(config: config, hasOverride: true);
73 - }
73 + localePath.forEach((key, dynamic value) {
74 + inputContent = File(localePath[key].toString()).readAsStringSync();
75 + config = json.decode(inputContent) as Map<String, dynamic>;
76
75 - output += '}' + '\n\n';
76 - });
77 + locales += "'$key', ";
78
78 - output += classDeclaration;
79 + output += 'class \$$key extends S {' + '\n';
80 + output += ' const \$$key();' + '\n';
81
80 - localePath.keys.forEach((key) {
81 - output += ' Locale("$key", ""),' + '\n';
82 - });
82 + if (key != defaultLocale) {
83 + output += textDirectionDeclaration;
84 + output += localizedStrings(config: config, hasOverride: true);
85 + }
86
84 - output += part2;
87 + output += '}' + '\n\n';
88 + });
89
86 - localePath.keys.forEach((key) {
87 - output += ' case "$key":' + '\n';
88 - output += ' S.current = const \$$key();' + '\n';
89 - output += ' return SynchronousFuture<S>(S.current);' + '\n';
90 - });
90 + output += classDeclaration;
91
92 - output += part3;
92 + localePath.keys.forEach((key) {
93 + output += ' Locale("$key", ""),' + '\n';
94 + });
95
94 - await File(outputPath + localizationFileName).writeAsString(output);
96 + output += part2;
97
96 - locales += '];';
98 + localePath.keys.forEach((key) {
99 + output += ' case "$key":' + '\n';
100 + output += ' S.current = const \$$key();' + '\n';
101 + output += ' return SynchronousFuture<S>(S.current);' + '\n';
102 + });
103
98 - await File(outputPath + localeListFileName).writeAsString(locales);
99 - } catch (e) {
100 - print(e.toString());
101 - }
102 - } else {
103 - print("Locale list doesn't contain $defaultLocale");
104 - }
105 - } else {
106 - print('Wrong directory path: $dirPath');
107 - }
108 - } else {
109 - print('Wrong key: $key');
104 + output += part3;
105 +
106 + await File(outputPath + localizationFileName).writeAsString(output);
107 +
108 + locales += '];';
109 +
110 + await File(outputPath + localeListFileName).writeAsString(locales);
111 + } catch (e) {
112 + print(e.toString());
113 }
114 });
115 }