processProviderFile static method

Future<void> processProviderFile(
  1. String contents,
  2. File file
)

Implementation

static Future<void> processProviderFile(String contents, File file) async {
  try {
    var effectiveContents = contents;
    if (_fileContentCache[file.path] == effectiveContents) {
      return; // Content hasn't changed
    }

    final fileName = file.uri.pathSegments.last;
    final partName = fileName.replaceAll('.dart', '.pg.dart');
    final generatedFilePath =
        '${file.parent.path.endsWith('/') ? file.parent.path : '${file.parent.path}/'}$partName';
    final generatedFile = File(generatedFilePath);

    // Fast path: if file clearly has no provider annotations, avoid analyzer parse.
    // Strip comments before checking to avoid false positives from commented code.
    final stripped = effectiveContents
        // Remove block comments
        .replaceAll(RegExp(r'/\*.*?\*/', dotAll: true), '')
        // Remove line comments
        .replaceAll(RegExp(r'//.*'), '');

    final maybeAnnotated =
        stripped.contains('@provider') ||
        stripped.contains('@providerValue') ||
        stripped.contains('@command') ||
        stripped.contains('@Command');

    if (!maybeAnnotated) {
      final hasPartLine =
          effectiveContents.contains("part '$partName';") ||
          effectiveContents.contains('part "$partName";');
      final generatedExists = await generatedFile.exists();

      if (hasPartLine || generatedExists) {
        await _cleanupPartLineSimple(file, partName);
        if (generatedExists) {
          await generatedFile.delete();
        }
        _fileContentCache[file.path] = await file.readAsString();
      } else {
        _fileContentCache[file.path] = effectiveContents;
      }
      return;
    }

    final parsedResult = parseString(content: effectiveContents);
    final parsedUnit = parsedResult.unit;

    final providerDataCollection = collectData(parsedResult);

    String generatedContent = '';

    final hasProvidersOrCommands =
        providerDataCollection.providers.isNotEmpty ||
        providerDataCollection.commands.isNotEmpty;

    generatedContent +=
        ProviderGeneratedFile(
          file: file,
          providerDataCollection: providerDataCollection,
        ).build() ??
        '';

    if (!hasProvidersOrCommands) {
      await _cleanupPartAndGenerated(file, parsedUnit);
      _fileContentCache[file.path] = await file.readAsString();
      return; // No providers/commands -> clean part + generated and exit
    }

    if (generatedContent.isEmpty) {
      // Defensive: nothing generated, still ensure cleanup
      await _cleanupPartAndGenerated(file, parsedUnit);
      _fileContentCache[file.path] = await file.readAsString();
      return;
    }

    effectiveContents = await _ensurePartDirective(
      file,
      effectiveContents,
      parsedUnit,
    );
    _fileContentCache[file.path] = effectiveContents;

    generatedContent = "part of '$fileName';\n\n$generatedContent";

    final formatter = DartFormatter(
      languageVersion: DartFormatter.latestLanguageVersion,
    );

    final formatted = formatter.format(generatedContent);
    await File(generatedFilePath).writeAsString(formatted);
  } on FileSystemException catch (e) {
    print('FileSystemException: ${e.message}, path = ${e.path}');
  } catch (e) {
    print('Error processing file "${file.path}": $e');
  }
}