47 lines
1.3 KiB
Dart
47 lines
1.3 KiB
Dart
import 'dart:io';
|
|
import 'dart:convert';
|
|
|
|
void main(List<String> args) async {
|
|
final dirPath = args.isNotEmpty ? args[0] : Directory.current.path;
|
|
|
|
print('扫描目录: $dirPath');
|
|
|
|
final directory = Directory(dirPath);
|
|
if (!directory.existsSync()) {
|
|
print('目录不存在');
|
|
return;
|
|
}
|
|
|
|
final dartFiles = <File>[];
|
|
await for (var entity
|
|
in directory.list(recursive: true, followLinks: false)) {
|
|
if (entity is File && entity.path.endsWith('.dart')) {
|
|
dartFiles.add(entity);
|
|
}
|
|
}
|
|
|
|
final chineseReg = RegExp(r'[\u4e00-\u9fa5]+');
|
|
final chineseSet = <String>{};
|
|
|
|
for (var file in dartFiles) {
|
|
final content = await file.readAsString();
|
|
final uncommented = removeComments(content);
|
|
final matches = chineseReg.allMatches(uncommented);
|
|
chineseSet.addAll(matches.map((m) => m.group(0)!));
|
|
}
|
|
|
|
final resultMap = {for (var text in chineseSet) text: text};
|
|
final jsonString = const JsonEncoder.withIndent(' ').convert(resultMap);
|
|
|
|
final outputFile = File('chinese_texts.json');
|
|
await outputFile.writeAsString(jsonString);
|
|
|
|
print('中文文本已保存到: ${outputFile.path}');
|
|
}
|
|
|
|
String removeComments(String source) {
|
|
source = source.replaceAll(RegExp(r'\/\*[\s\S]*?\*\/'), '');
|
|
source = source.replaceAll(RegExp(r'\/\/.*'), '');
|
|
return source;
|
|
}
|