|
@@ -0,0 +1,79 @@
|
|
|
|
|
+import 'package:build/build.dart';
|
|
|
|
|
+import 'package:xml/xml.dart';
|
|
|
|
|
+import 'package:glob/glob.dart';
|
|
|
|
|
+
|
|
|
|
|
+Builder stringXmlWatcherBuilder(BuilderOptions options) {
|
|
|
|
|
+ return StringXmlWatcherBuilder();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class StringXmlWatcherBuilder implements Builder {
|
|
|
|
|
+ static const _baseType = 'zh_CN';
|
|
|
|
|
+ static final _targetXmlGlob = Glob('assets/string/base/*.xml');
|
|
|
|
|
+
|
|
|
|
|
+ @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',
|
|
|
|
|
+ );
|
|
|
|
|
+ await buildStep.writeAsString(outputId, buffer.toString() + multiBuffer.toString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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}\': {');
|
|
|
|
|
+
|
|
|
|
|
+ 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\',');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ multiBuffer.writeln(' },');
|
|
|
|
|
+ buffer.writeln('}');
|
|
|
|
|
+ multiBuffer.writeln(' };');
|
|
|
|
|
+ multiBuffer.writeln('}');
|
|
|
|
|
+
|
|
|
|
|
+ return (buffer, multiBuffer);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String _sanitizeValue(String value) => value.replaceAll('\r\n', '').trim();
|
|
|
|
|
+
|
|
|
|
|
+ String _toCamelCase(String snakeCase) {
|
|
|
|
|
+ return snakeCase.split('_').map((word) {
|
|
|
|
|
+ if (word == snakeCase.split('_').first) return word;
|
|
|
|
|
+ return word[0].toUpperCase() + word.substring(1);
|
|
|
|
|
+ }).join('');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ Map<String, List<String>> get buildExtensions => {
|
|
|
|
|
+ r'$package$': ['lib/resource/string.gen.dart'],
|
|
|
|
|
+ };
|
|
|
|
|
+}
|