buildIt method

Future<void> buildIt(
  1. AssetId inputId,
  2. Future<String> readAsString(
    1. AssetId id, {
    2. Encoding encoding,
    }),
  3. Future<void> writeAsString(
    1. AssetId id,
    2. FutureOr<String> contents, {
    3. Encoding encoding,
    }),
  4. Future<LibraryElem> libraryFor(
    1. AssetId assetId, {
    2. bool allowSyntaxErrors,
    }),
)

Implementation

Future<void> buildIt(
    AssetId inputId,
    Future<String> Function(AssetId id, {Encoding encoding}) readAsString,
    Future<void> Function(AssetId id, FutureOr<String> contents,
            {Encoding encoding})
        writeAsString,
    Future<LibraryElem> Function(AssetId assetId, {bool allowSyntaxErrors})
        libraryFor) async {
  // Only process .dart files, skip generated files
  if (!inputId.path.endsWith('.dart') ||
      inputId.path.contains('.g.dart') ||
      inputId.path.contains('.rdf_mapper.g.dart')) {
    return;
  }

  try {
    final sourceContent = await readAsString(inputId);

    // Parse the source file using the analyzer
    final parseResult = _analyzerWrapperService.parseString(
      content: sourceContent,
      path: inputId.path,
    );

    if (parseResult.errors.isNotEmpty) {
      log.warning(
        'Parse errors in ${inputId.path}: ${parseResult.errors}',
      );
      return;
    }

    // Get the library element for the parsed file
    final library = await _resolveLibrary(libraryFor, inputId);
    if (library == null) {
      return;
    }

    final classes = library.classes;

    final enums = library.enums;

    final generatedTemplateData = (await _builderHelper.buildTemplateData(
            inputId.path,
            inputId.package,
            classes,
            enums,
            BroaderImports.create(library)))
        ?.toMap();

    // Only create output file if we generated code
    if (generatedTemplateData != null) {
      final outputId = inputId.changeExtension('.rdf_mapper.cache.json');
      await writeAsString(outputId, jsonEncode(generatedTemplateData));

      log.info('Generated RDF mapper cache for ${inputId.path}');
    }
  } catch (e, stackTrace) {
    log.severe(
      'Error processing ${inputId.path}: $e',
      e,
      stackTrace,
    );
    // Re-throw to ensure build fails on errors
    rethrow;
  }
}