generate method

  1. @override
Future<String> generate(
  1. LibraryReader library,
  2. BuildStep _
)

Generates Dart code for an input Dart library.

May create additional outputs through the buildStep, but the 'primary' output is Dart code returned through the Future. If there is nothing to generate for this library may return null, or a Future that resolves to null or the empty string.

Implementation

@override
Future<String> generate(LibraryReader library, _) async {
  final output = <String>[];
  if (forLibrary) {
    var name = library.element.name;
    if (name.isEmpty ?? false) {
      name = library.element.source.uri.pathSegments.last;
    }
    output.add('// Code for "$name"');
  }
  if (forClasses) {
    for (var classElement in library.allElements.whereType<ClassElement>()) {
      if (isRemoteConfig(classElement)) {
        final e = classElement.allSupertypes.firstWhere(
          (element) {
            return element.getDisplayString(withNullability: false) ==
              '$BuiltConfig';
          },
        );
        for (final a in e.accessors) {
          output.add('// ' + a.displayName);
        }
        if (classElement.displayName.contains('GoodError')) {
          throw InvalidGenerationSourceError(
            "Don't use classes with the word 'Error' in the name",
            todo: 'Rename ${classElement.displayName} to something else.',
            element: classElement,
          );
        }
        output.add(_RemoteConfigBuilder(classElement).generateConfig());
      }
    }
  }
  return output.join('\n');
}