build method

  1. @override
FutureOr<void> build(
  1. BuildStep buildStep
)

Generates the outputs for a given BuildStep.

Implementation

@override
FutureOr<void> build(BuildStep buildStep) async {
  final library = await buildStep.inputLibrary;
  final Map<String, Set<String>> _locales = {};
  final Map<String, Map<String, String>> _usedKeys = {};
  final Map<String, Map<String, String>> _commonKeys = {};
  final Map<String, Map<String, String>> _langKeys = {};
  final Map<String, String> _outputDir = {};
  for (var u in library.units) {
    for (var t in u.classes) {
      //class level
      for (var m in t.metadata) {
        String an = m.element!.name!; // Map by annotation name
        if (!_usedKeys.containsKey(an)) {
          _usedKeys[an] = {};
        }
        if (!_commonKeys.containsKey(an)) {
          _commonKeys[an] = {};
        }
        if (!_langKeys.containsKey(an)) {
          _langKeys[an] = {};
        }
        if ('${m.element}'.startsWith(RegExp(r'L10nSettings*'))) {
          if (!_locales.containsKey(an)) {
            _locales[an] = {};
            final settings = m.computeConstantValue()!;
            if (settings.getField('path') != null &&
                !settings.getField('path')!.isNull) {
              _outputDir[an] = settings.getField('path')!.toStringValue()!;
            }
            for (var el in settings.getField('locales')!.toListValue()!) {
              log.fine('Locale: $el');
              if (el.getField('countryCode') != null &&
                  el.getField('countryCode')!.isNull) {
                _locales[an]
                    ?.add(el.getField('languageCode')!.toStringValue()!);
              } else {
                _locales[an]?.add(
                    el.getField('languageCode')!.toStringValue()! +
                        '_' +
                        el.getField('countryCode')!.toStringValue()!);
              }
            }
          }
          for (var f in t.fields) {
            final val = f.computeConstantValue()!;
            log.fine('${t.name}::${f.name} = $val');
            if (val.type.toString().startsWith(RegExp(r'L10nSet*'))) {
              findKeyAndvalue('${t.name}::${f.name}', val, _usedKeys[an],
                  _commonKeys[an], _langKeys[an]);
            } else if (val.type
                .toString()
                .startsWith(RegExp(r'Map<String*?, L10nSet*?>*'))) {
              val.toMapValue()!.forEach((key, val) {
                findKeyAndvalue(
                    '${t.name}::${f.name}::${key!.toStringValue()}',
                    val,
                    _usedKeys[an],
                    _commonKeys[an],
                    _langKeys[an]);
              });
            }
          }
        }
      }
    }
  }
  _locales.forEach((an, locales) {
    //generate report for each L10nSettings
    createReport(_outputDir[an], locales, _commonKeys[an], _langKeys[an]);
  });
}