generate method

  1. @override
FutureOr<String> generate(
  1. LibraryElement library,
  2. BuildStep buildStep
)
override

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.

@param library The library element to generate code for @param buildStep The build step providing context for generation @return Generated code as a string, or null if nothing to generate

Implementation

@override
FutureOr<String> generate(LibraryElement library, BuildStep buildStep) async {
  final TypeChecker typeChecker = getTypeChecker(buildStep);
  final Set<String> values = <String>{};
  final Iterable<AnnotatedElement> annotatedElements = library.annotatedWith(
    typeChecker,
  );
  if (annotatedElements.isEmpty && throwOnUnresolved) {
    throw ArgumentError(
      'No elements found with annotation $typeChecker in ${library.src.uri}. '
      'Please check your annotations.',
    );
  }
  for (AnnotatedElement annotatedElement in annotatedElements) {
    final dynamic rawValue = generateForAnnotatedElement(
      buildStep,
      annotatedElement.element,
      annotatedElement.annotation,
    );
    final Iterable<String> normalized = await normalizeGeneratorOutput(rawValue);
    for (final String value in normalized) {
      if (value.trim().isNotEmpty) {
        values.add(value);
      }
    }
  }
  return values.join('\n\n');
}