generate method

Future<String> generate(
  1. Directory directory, {
  2. bool dryRun = false,
})

Implementation

Future<String> generate(Directory directory, {bool dryRun = false}) async {
  final output = <String>[];
  final Set<String> delegates = {
    '...AppLocalizations.localizationsDelegates,'
  };
  final Set<String> imports = {
    "import 'package:flutter/widgets.dart';",
    "import 'package:flutter_gen/gen_l10n/app_localizations.dart';"
  };
  var pubspec = Pubspec.parse(
      File('${directory.parent.path}/pubspec.yaml').readAsStringSync());
  var l10nYaml =
      loadYaml(File('${directory.parent.path}/l10n.yaml').readAsStringSync());
  String outputFile = l10nYaml['output-localization-file'];
  final delegateName =
      ReCase(outputFile.replaceFirst('.dart', '')).pascalCase;
  String destination = l10nYaml['arb-dir'];
  String templateArbFile = l10nYaml['template-arb-file'];

  final arbFile = {};

  directory.listSync(recursive: true).forEach((file) {
    if (!file.path.endsWith('.dart') || file.path.endsWith('.g.dart')) {
      return;
    }

    var compilationUnitFile = File(file.path);
    List info = parseContent(compilationUnitFile.readAsStringSync());

    String libraryName =
        compilationUnitFile.uri.pathSegments.last.replaceFirst('.dart', '');

    List<Map<String, String>> properties = [];
    for (var item in info) {
      (item["arb"] as Map).forEach((k, v) {
        String name = k;
        String aliasName;
        if (k.startsWith("@")) {
          name = name.substring(1);
          aliasName = ReCase(libraryName).camelCase + ReCase(name).pascalCase;
          properties.add({"name": name, "alias": aliasName});
          arbFile["@$aliasName"] = v;
        } else {
          aliasName = ReCase(libraryName).camelCase + ReCase(name).pascalCase;
          arbFile[aliasName] = v;
        }
      });

      String relativePath =
          file.path.replaceFirst(directory.path, '').substring(1);
      String importStatement =
          "import 'package:${pubspec.name}/$relativePath';";
      if (imports.add(importStatement)) {
        var code = generateDelegatedLocalization(
            name: item['name'],
            properties: properties,
            delegate: delegateName);
        output.add(code);

        delegates.add("""LocalizableObjectDelegate<${item['name']}>(
  _${item['name']}(), $delegateName.delegate),""");
      }
    }
  });

  var field = Field((b) => b
    ..modifier = FieldModifier.final$
    ..type = refer('List<LocalizationsDelegate<dynamic>>')
    ..name = 'localizationsDelegates'
    ..assignment = Code("[${delegates.join('\n')}]"));

  final emitter = DartEmitter();
  String localizationsDelegates =
      DartFormatter().format('${field.accept(emitter)}');

  final source = '$generatedCodeWarning${imports.join('\n')}'
      '$template${output.join('\n')}\n$localizationsDelegates';

  if( !dryRun ) {
    File('$destination/localizations.g.dart').writeAsStringSync(source);

    File('$destination/$templateArbFile')
        .writeAsStringSync(jsonEncode(arbFile));
  } else {
    stdout.writeln(source);
  }
  return source;
}