Browse Source

[new]第一次提交

zk 10 months ago
commit
e61592c97d
9 changed files with 238 additions and 0 deletions
  1. 29 0
      .gitignore
  2. 10 0
      .metadata
  3. 3 0
      CHANGELOG.md
  4. 1 0
      LICENSE
  5. 39 0
      README.md
  6. 4 0
      analysis_options.yaml
  7. 14 0
      build.yaml
  8. 79 0
      lib/string_get_runner.dart
  9. 59 0
      pubspec.yaml

+ 29 - 0
.gitignore

@@ -0,0 +1,29 @@
+# Miscellaneous
+*.class
+*.log
+*.pyc
+*.swp
+.DS_Store
+.atom/
+.buildlog/
+.history
+.svn/
+migrate_working_dir/
+
+# IntelliJ related
+*.iml
+*.ipr
+*.iws
+.idea/
+
+# The .vscode folder contains launch configuration and tasks you configure in
+# VS Code which you may wish to be included in version control, so this line
+# is commented out by default.
+#.vscode/
+
+# Flutter/Dart/Pub related
+# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
+/pubspec.lock
+**/doc/api/
+.dart_tool/
+build/

+ 10 - 0
.metadata

@@ -0,0 +1,10 @@
+# This file tracks properties of this Flutter project.
+# Used by Flutter tool to assess capabilities and perform upgrades etc.
+#
+# This file should be version controlled and should not be manually edited.
+
+version:
+  revision: "80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819"
+  channel: "stable"
+
+project_type: package

+ 3 - 0
CHANGELOG.md

@@ -0,0 +1,3 @@
+## 0.0.1
+
+* TODO: Describe initial release.

+ 1 - 0
LICENSE

@@ -0,0 +1 @@
+TODO: Add your license here.

+ 39 - 0
README.md

@@ -0,0 +1,39 @@
+<!--
+This README describes the package. If you publish this package to pub.dev,
+this README's contents appear on the landing page for your package.
+
+For information about how to write a good package README, see the guide for
+[writing package pages](https://dart.dev/tools/pub/writing-package-pages).
+
+For general information about developing packages, see the Dart guide for
+[creating packages](https://dart.dev/guides/libraries/create-packages)
+and the Flutter guide for
+[developing packages and plugins](https://flutter.dev/to/develop-packages).
+-->
+
+TODO: Put a short description of the package here that helps potential users
+know whether this package might be useful for them.
+
+## Features
+
+TODO: List what your package can do. Maybe include images, gifs, or videos.
+
+## Getting started
+
+TODO: List prerequisites and provide or point to information on how to
+start using the package.
+
+## Usage
+
+TODO: Include short and useful examples for package users. Add longer examples
+to `/example` folder.
+
+```dart
+const like = 'sample';
+```
+
+## Additional information
+
+TODO: Tell users more about the package: where to find more information, how to
+contribute to the package, how to file issues, what response they can expect
+from the package authors, and more.

+ 4 - 0
analysis_options.yaml

@@ -0,0 +1,4 @@
+include: package:flutter_lints/flutter.yaml
+
+# Additional information about this file can be found at
+# https://dart.dev/guides/language/analysis-options

+ 14 - 0
build.yaml

@@ -0,0 +1,14 @@
+targets:
+  $default:
+    builders:
+      string_get_runner:
+        enabled: true
+
+builders:
+  string_get_runner:
+    import: 'package:string_get_runner/string_get_runner.dart'
+    builder_factories: ['stringXmlWatcherBuilder']
+    build_extensions:
+      "assets/string/base/*.xml": ["lib/resource/string.gen.dart"]
+    auto_apply: dependents
+    build_to: source

+ 79 - 0
lib/string_get_runner.dart

@@ -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'],
+  };
+}

+ 59 - 0
pubspec.yaml

@@ -0,0 +1,59 @@
+name: string_get_runner
+description: "getx插件 多语言字符串生成工具"
+version: 0.0.1
+homepage:
+
+environment:
+  sdk: ^3.5.0
+  flutter: ">=1.17.0"
+
+dependencies:
+  flutter:
+    sdk: flutter
+
+  build: ^2.0.0
+  yaml: ^3.0.0
+  xml: ^6.0.0
+
+
+dev_dependencies:
+  flutter_test:
+    sdk: flutter
+  flutter_lints: ^4.0.0
+
+# For information on the generic Dart part of this file, see the
+# following page: https://dart.dev/tools/pub/pubspec
+
+# The following section is specific to Flutter packages.
+flutter:
+
+  # To add assets to your package, add an assets section, like this:
+  # assets:
+  #   - images/a_dot_burr.jpeg
+  #   - images/a_dot_ham.jpeg
+  #
+  # For details regarding assets in packages, see
+  # https://flutter.dev/to/asset-from-package
+  #
+  # An image asset can refer to one or more resolution-specific "variants", see
+  # https://flutter.dev/to/resolution-aware-images
+
+  # To add custom fonts to your package, add a fonts section here,
+  # in this "flutter" section. Each entry in this list should have a
+  # "family" key with the font family name, and a "fonts" key with a
+  # list giving the asset and other descriptors for the font. For
+  # example:
+  # fonts:
+  #   - family: Schyler
+  #     fonts:
+  #       - asset: fonts/Schyler-Regular.ttf
+  #       - asset: fonts/Schyler-Italic.ttf
+  #         style: italic
+  #   - family: Trajan Pro
+  #     fonts:
+  #       - asset: fonts/TrajanPro.ttf
+  #       - asset: fonts/TrajanPro_Bold.ttf
+  #         weight: 700
+  #
+  # For details regarding fonts in packages, see
+  # https://flutter.dev/to/font-from-package