string_resource_builder.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:build/build.dart';
  4. import 'package:xml/xml.dart';
  5. const baseType = 'zh_CN';
  6. Builder builds(BuilderOptions options) => StringResourceBuilder();
  7. class StringResourceBuilder implements Builder {
  8. @override
  9. Future<void> build(BuildStep buildStep) async {
  10. generateStringResources();
  11. }
  12. @override
  13. Map<String, List<String>> get buildExtensions => {
  14. '.xml': ['.s.dart'],
  15. };
  16. }
  17. void main() {
  18. generateStringResources();
  19. }
  20. void generateStringResources() {
  21. print('generateStringResources...start');
  22. final directory = Directory('assets/string/');
  23. final buffer = StringBuffer();
  24. final multiBuffer = StringBuffer();
  25. buffer.writeln('import \'package:get/get.dart\';');
  26. buffer.writeln();
  27. buffer.writeln('class StringName {');
  28. buffer.writeln('StringName._();');
  29. multiBuffer.writeln('class StringMultiSource {');
  30. multiBuffer.writeln('StringMultiSource._();');
  31. multiBuffer.writeln(
  32. ' static const Map<String, Map<String, String>> values = {',
  33. );
  34. directory.listSync(recursive: true).forEach((element) {
  35. if (element is Directory) {
  36. final files = element.listSync().where(
  37. (file) => file.path.endsWith('.xml'),
  38. );
  39. bool isBase = element.path.endsWith('base');
  40. String node = isBase ? baseType : element.path.split('/').last;
  41. for (var file in files) {
  42. final content = File(file.path).readAsStringSync();
  43. final document = XmlDocument.parse(content);
  44. final strings = document.findAllElements('string');
  45. multiBuffer.writeln(' \'$node\': {');
  46. for (var string in strings) {
  47. final name = string.getAttribute('name');
  48. String value = processXmlText(string.text);
  49. if (isBase) {
  50. final camelCaseName = toCamelCase(name);
  51. buffer.writeln(
  52. ' static final String $camelCaseName = \'$name\'.tr;// $value',
  53. );
  54. }
  55. multiBuffer.writeln(' \'$name\': \'$value\',');
  56. }
  57. multiBuffer.writeln(' },');
  58. }
  59. }
  60. });
  61. multiBuffer.writeln(' };');
  62. multiBuffer.writeln('}');
  63. buffer.writeln('}');
  64. buffer.writeln();
  65. buffer.writeln(multiBuffer.toString());
  66. createDirectory('lib/resource');
  67. final outputFile = File('lib/resource/string.gen.dart');
  68. outputFile.writeAsStringSync(buffer.toString());
  69. print('Strings file generated successfully!');
  70. }
  71. String processXmlText(String original) {
  72. return original
  73. .replaceAll('\r\n', ' ')
  74. .replaceAll('\n', ' ')
  75. .replaceAll(RegExp(r'\s+'), ' ')
  76. .trim()
  77. .replaceAll("'", "\\'");
  78. }
  79. void createDirectory(String path) {
  80. final directory = Directory(path);
  81. if (!directory.existsSync()) {
  82. directory.createSync(recursive: true);
  83. print('Directory created: $path');
  84. }
  85. }
  86. String toCamelCase(String? snakeCase) {
  87. if (snakeCase == null) {
  88. return '';
  89. }
  90. return snakeCase
  91. .split('_')
  92. .map((word) {
  93. if (word == snakeCase.split('_').first) {
  94. return word;
  95. } else {
  96. return word[0].toUpperCase() + word.substring(1);
  97. }
  98. })
  99. .join('');
  100. }