|
|
@@ -1,57 +1,82 @@
|
|
|
import 'package:build/build.dart';
|
|
|
import 'package:xml/xml.dart';
|
|
|
import 'package:glob/glob.dart';
|
|
|
+import 'package:path/path.dart' as path;
|
|
|
+
|
|
|
+// 新增配置导入
|
|
|
+import 'flutter_string_get_config.dart';
|
|
|
|
|
|
Builder stringXmlWatcherBuilder(BuilderOptions options) {
|
|
|
return StringXmlWatcherBuilder();
|
|
|
}
|
|
|
|
|
|
class StringXmlWatcherBuilder implements Builder {
|
|
|
- static const _baseType = 'zh_CN';
|
|
|
- static final _targetXmlGlob = Glob('assets/string/base/*.xml');
|
|
|
+ // 从配置读取参数
|
|
|
+ late final FlutterStringGetConfig _config;
|
|
|
+ late final Glob _targetXmlGlob;
|
|
|
+
|
|
|
+ StringXmlWatcherBuilder() {
|
|
|
+ // 初始化时加载用户配置
|
|
|
+ _config = FlutterStringGetConfig.fromProject();
|
|
|
+
|
|
|
+ print('string_get_runner inputDir ${_config.inputDir}');
|
|
|
+ print('string_get_runner outputFile ${_config.outputFile}');
|
|
|
+ // 动态生成Glob监听模式
|
|
|
+ _targetXmlGlob = Glob(_config.inputDir);
|
|
|
+ }
|
|
|
|
|
|
@override
|
|
|
Future<void> build(BuildStep buildStep) async {
|
|
|
final xmlFiles = await buildStep.findAssets(_targetXmlGlob).toList();
|
|
|
final (buffer, multiBuffer) = await _processXmlFiles(buildStep, xmlFiles);
|
|
|
|
|
|
+ // 使用配置中的输出路径
|
|
|
final outputId = AssetId(
|
|
|
buildStep.inputId.package,
|
|
|
- 'lib/resource/string.gen.dart',
|
|
|
+ _config.outputFile,
|
|
|
);
|
|
|
- await buildStep.writeAsString(outputId, buffer.toString() + multiBuffer.toString());
|
|
|
+ await buildStep.writeAsString(
|
|
|
+ outputId, buffer.toString() + multiBuffer.toString());
|
|
|
}
|
|
|
|
|
|
- Future<(StringBuffer, StringBuffer)> _processXmlFiles(
|
|
|
- BuildStep buildStep,
|
|
|
- List<AssetId> xmlFiles,
|
|
|
- ) async {
|
|
|
+ Future<(StringBuffer, StringBuffer)> _processXmlFiles(BuildStep buildStep,
|
|
|
+ List<AssetId> xmlFiles,) async {
|
|
|
final buffer = StringBuffer();
|
|
|
final multiBuffer = StringBuffer();
|
|
|
+
|
|
|
buffer.writeln('import \'package:get/get.dart\';');
|
|
|
buffer.writeln();
|
|
|
buffer.writeln('class StringName {');
|
|
|
buffer.writeln(' StringName._();');
|
|
|
+
|
|
|
multiBuffer.writeln('class StringMultiSource {');
|
|
|
multiBuffer.writeln(' StringMultiSource._();');
|
|
|
- multiBuffer.writeln(' static const Map<String, Map<String, String>> values = {');
|
|
|
-
|
|
|
- multiBuffer.writeln(' \'${_baseType}\': {');
|
|
|
+ multiBuffer
|
|
|
+ .writeln(' static const Map<String, Map<String, String>> values = {');
|
|
|
+ multiBuffer.writeln(' \'zh_CN\': {'); // 默认基准语言
|
|
|
|
|
|
+ print('zkzkzk xmlFiles ${xmlFiles.length}');
|
|
|
+ // 遍历所有 XML 文件
|
|
|
for (final file in xmlFiles) {
|
|
|
- final content = await buildStep.readAsString(file);
|
|
|
- final document = XmlDocument.parse(content);
|
|
|
-
|
|
|
- for (final element in document.findAllElements('string')) {
|
|
|
- final name = element.getAttribute('name');
|
|
|
- final value = _sanitizeValue(element.text);
|
|
|
-
|
|
|
- if (name != null) {
|
|
|
- final camelCaseName = _toCamelCase(name);
|
|
|
- buffer.writeln(
|
|
|
- ' static final String $camelCaseName = \'$name\'.tr;// $value');
|
|
|
- multiBuffer.writeln(' \'$name\': \'$value\',');
|
|
|
+ print('zkzkzk file ${file.path}');
|
|
|
+ try {
|
|
|
+ final content = await buildStep.readAsString(file);
|
|
|
+ final document = XmlDocument.parse(content);
|
|
|
+ print('zkzkzk document ${document.toString()}');
|
|
|
+ // 遍历所有 <string> 元素
|
|
|
+ for (final element in document.findAllElements('string')) {
|
|
|
+ final name = element.getAttribute('name');
|
|
|
+ final value = _sanitizeValue(element.text);
|
|
|
+
|
|
|
+ if (name != null) {
|
|
|
+ final camelCaseName = _toCamelCase(name);
|
|
|
+ buffer.writeln(
|
|
|
+ ' static final String $camelCaseName = \'$name\'.tr; // $value');
|
|
|
+ multiBuffer.writeln(' \'$name\': \'$value\',');
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (e) {
|
|
|
+ print('❌ Error processing file ${file.path}: $e');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -74,6 +99,7 @@ class StringXmlWatcherBuilder implements Builder {
|
|
|
|
|
|
@override
|
|
|
Map<String, List<String>> get buildExtensions => {
|
|
|
- r'$package$': ['lib/resource/string.gen.dart'],
|
|
|
- };
|
|
|
-}
|
|
|
+ // 动态生成扩展映射
|
|
|
+ r'$package$': [path.normalize(_config.outputFile)],
|
|
|
+ };
|
|
|
+}
|