processProviderFile static method
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', '.craft.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 plugin annotations, avoid analyzer parse.
// The check is driven by the union of all registered plugin annotation names.
final stripped = effectiveContents
// Remove block comments
.replaceAll(RegExp(r'/\*.*?\*/', dotAll: true), '')
// Remove line comments
.replaceAll(RegExp(r'//.*'), '');
final allAnnotations = _pluginRunner.allAnnotations;
final maybeAnnotated = allAnnotations.any(
(name) => stripped.contains('@$name'),
);
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;
// Run the plugin pipeline
final generatedContent = _pluginRunner.run(parsedResult);
if (generatedContent == null || generatedContent.isEmpty) {
await _cleanupPartAndGenerated(file, parsedUnit);
_fileContentCache[file.path] = await file.readAsString();
return;
}
// Check if file has paged providers
final hasPaged = generatedContent.contains('PagedDataNotifier');
final pagedPreamble = hasPaged ? _buildPagedPreamble() : '';
effectiveContents = await _ensurePartDirective(
file,
effectiveContents,
parsedUnit,
);
_fileContentCache[file.path] = effectiveContents;
final fullContent = "part of '$fileName';\n$pagedPreamble\n$generatedContent";
final formatter = DartFormatter(
languageVersion: DartFormatter.latestLanguageVersion,
);
final formatted = formatter.format(fullContent);
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');
}
}