getEdges function
Read each Dart file and get the import and export paths.
Implementation
List<Edge> getEdges(
Directory rootDir, String ignore, File pubspecYaml, List<String> nodes) {
var edges = <Edge>[];
var dartFiles = getDartFiles(rootDir, ignore);
for (var dartFile in dartFiles) {
var from =
dartFile.path.replaceFirst(rootDir.path, '').replaceAll('\\', '/');
// Grab the imports from the dart file
var lines = dartFile.readAsLinesSync();
for (var line in lines) {
line = line.trim();
if (line.startsWith('import') || line.startsWith('export')) {
var parsedImportLine = parseImportLine(line);
if (parsedImportLine == null) {
continue;
}
File? resolvedFile;
if (parsedImportLine.startsWith('package:')) {
resolvedFile =
resolvePackageFileFromPubspecYaml(pubspecYaml, parsedImportLine);
} else if (parsedImportLine.startsWith('dart:') ||
parsedImportLine.startsWith('dart-ext:')) {
continue; // Ignore dart: or dart-ext: imports
} else {
try {
resolvedFile = resolveFile(dartFile, parsedImportLine);
} catch (e) {
resolvedFile = null;
}
}
// Only add dart files that exist--account for imports inside strings, comments, etc.
if (resolvedFile != null &&
resolvedFile.existsSync() &&
isWithin(rootDir.path, resolvedFile.path) &&
resolvedFile.path.endsWith('.dart')) {
var to = resolvedFile.path
.replaceFirst(rootDir.path, '')
.replaceAll('\\', '/');
// No self loops
if (from != to) {
// Only add edges to nodes that exist
if (nodes.contains(to)) {
edges.add(Edge(from, to,
directive: line.startsWith('import')
? Directive.import
: Directive.export));
}
}
}
}
}
}
return edges;
}