removeImport static method
Drops importStatements from filePath if they are present.
Templates carry every import a generated file might need; the ones the
generated variant does not use have to come back out, or the project is
left with unused_import warnings it did not ask for.
Implementation
static Future<void> removeImport({
required String filePath,
required List<String> importStatements,
}) async {
final file = File(filePath);
if (!file.existsSync()) {
return;
}
final lines = await file.readAsLines();
final Set<String> unwanted =
importStatements.map((e) => collapseWhitespace(e)).toSet();
final List<String> kept = lines
.where((line) => !unwanted.contains(collapseWhitespace(line)))
.toList();
if (kept.length != lines.length) {
await file.writeAsString(kept.join('\n'));
}
}