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 {
  BuilderStatus status = BuilderStatus();
  status.imports.add("package:xml_layout/xml_layout.dart");
  status.imports.add("package:xml_layout/register.dart");
  status.imports.add("dart:convert");

  var library = await buildStep.inputLibrary;
  String entryName = options.config["entry_name"];
  String collectionsName = options.config["collections_name"];
  String convertsName = options.config["coverts_name"];
  String importsName = options.config["imports_name"];
  String convertName = options.config["convert_types"];
  List<DartType> entries = [];
  for (var element in library.topLevelElements) {
    if (element.kind == ElementKind.TOP_LEVEL_VARIABLE) {
      var topLevelElement = element as TopLevelVariableElement;
      if (topLevelElement.type.isDartCoreList == true) {
        if (topLevelElement.name == entryName) {
          var types = topLevelElement.computeConstantValue();
          if (types != null) {
            for (var type in types.toListValue()!) {
              entries.add(type.toTypeValue()!);
            }
          }
        } else if (topLevelElement.name == collectionsName) {
          var types = topLevelElement.computeConstantValue();
          if (types != null) {
            for (var type in types.toListValue()!) {
              _processCollectionType(status, type);
            }
          }
        } else if (topLevelElement.name == importsName) {
          var importsList = topLevelElement.computeConstantValue();
          if (importsList != null) {
            for (var importUri in importsList.toListValue()!) {
              String str = importUri.toStringValue()!;
              if (!status.imports.contains(str)) {
                status.imports.add(str);
              }
            }
          }
        }
      } else if (topLevelElement.type.isDartCoreMap == true) {
        if (topLevelElement.name == convertName) {
          var types = topLevelElement.computeConstantValue();
          types!.toMapValue()!.forEach((key, value) {
            status._convertTypes[key!.toTypeValue()!] = value!.toTypeValue()!;
          });
        } else if (topLevelElement.name == convertsName) {
          var converts = topLevelElement.computeConstantValue();
          converts!.toMapValue()!.forEach((key, value) {
            status._inputConvert[key!.toStringValue()!] = value!.toStringValue()!;
          });
        }
      }
    }
  }
  for (var entry in entries) {
    _processDartType(status, entry);
  }

  List<String> codes = [];
  status.imports.forEach((element) {
    codes.add("import '$element';");
  });
  codes.add("Register register = Register(() {");
  codes.addAll(status._processed.values.expand<String>((element) => element));
  codes.add("});");

  String output = DartFormatter().format(codes.join('\n'));
  await buildStep.writeAsString(
      buildStep.inputId.changeExtension('.xml_layout.dart'),
      output
  );
}