82 lines
2.4 KiB
Dart
82 lines
2.4 KiB
Dart
import 'dart:io';
|
|
|
|
void main() {
|
|
final files = <String, String>{
|
|
't_settings_performance.dart': 'TSettingsPerformance',
|
|
't_settings_privacy.dart': 'TSettingsPrivacy',
|
|
't_settings_advanced.dart': 'TSettingsAdvanced',
|
|
't_settings_cache.dart': 'TSettingsCache',
|
|
't_settings_permission.dart': 'TSettingsPermission',
|
|
't_settings_data_collection.dart': 'TSettingsDataCollection',
|
|
};
|
|
|
|
const baseDir = 'e:\\project\\flutter\\f\\xianyan\\lib\\l10n\\types';
|
|
|
|
for (final entry in files.entries) {
|
|
final file = File('$baseDir\\${entry.key}');
|
|
final className = entry.value;
|
|
if (!file.existsSync()) {
|
|
print('NOT FOUND: ${entry.key}');
|
|
continue;
|
|
}
|
|
|
|
var content = file.readAsStringSync();
|
|
|
|
// Find the fromMap method
|
|
final startPattern = 'static $className fromMap(Map<String, String> map)';
|
|
final startIdx = content.indexOf(startPattern);
|
|
if (startIdx == -1) {
|
|
print('NO fromMap found: ${entry.key}');
|
|
continue;
|
|
}
|
|
|
|
// Find the end of the method (matching closing );
|
|
var depth = 0;
|
|
var endIdx = startIdx;
|
|
var foundOpen = false;
|
|
for (var i = startIdx; i < content.length; i++) {
|
|
if (content[i] == '(') {
|
|
depth++;
|
|
foundOpen = true;
|
|
} else if (content[i] == ')') {
|
|
depth--;
|
|
}
|
|
if (foundOpen && depth == 0) {
|
|
// Find the semicolon
|
|
endIdx = content.indexOf(';', i);
|
|
if (endIdx == -1) endIdx = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
final oldMethod = content.substring(startIdx, endIdx + 1);
|
|
|
|
// Parse all field: map['key'] ?? '' patterns
|
|
final fieldPattern = RegExp(r"(\w+):\s*map\['([^']+)'\]\s*\?\?\s*''");
|
|
final matches = fieldPattern.allMatches(oldMethod);
|
|
|
|
final newLines = <String>[];
|
|
for (final m in matches) {
|
|
final fieldName = m.group(1)!;
|
|
final mapKey = m.group(2)!;
|
|
newLines.add(' $fieldName: map[\'$mapKey\']?.isNotEmpty == true');
|
|
newLines.add(' ? map[\'$mapKey\']!');
|
|
newLines.add(' : (fallback?.$fieldName ?? \'\'),');
|
|
}
|
|
|
|
final newMethod =
|
|
'static $className fromMap(Map<String, String> map,\n'
|
|
' {$className? fallback}) =>\n'
|
|
' $className(\n'
|
|
'${newLines.join('\n')}\n'
|
|
' );';
|
|
|
|
content =
|
|
content.substring(0, startIdx) +
|
|
newMethod +
|
|
content.substring(endIdx + 1);
|
|
file.writeAsStringSync(content);
|
|
print('OK: ${entry.key} (${matches.length} fields)');
|
|
}
|
|
}
|