ImportGraph.build constructor
ImportGraph.build(})
Builds a graph from directivesByFile, a map of project-relative file
path to the raw URIs that file declares.
packageName resolves package:<packageName>/x.dart to lib/x.dart.
packages does the same for sub-packages of a monorepo: a directory
(packages/core) mapped to the name its own pubspec declares, so
package:core/x.dart resolves to packages/core/lib/x.dart.
A URI that does not resolve to a file in directivesByFile is dropped:
it points outside the project, or at something that is not there.
Implementation
factory ImportGraph.build(
Map<String, List<String>> directivesByFile,
String packageName, {
Map<String, String> packages = const {},
}) {
final files = directivesByFile.keys.toSet();
final edges = <String, Set<String>>{};
for (final entry in directivesByFile.entries) {
final from = entry.key;
final targets = <String>{};
for (final uri in entry.value) {
final resolved = resolveUri(
uri,
from: from,
packageName: packageName,
packages: packages,
);
// A self-edge is a file parting or importing itself: not a dependency,
// and it would show up as a one-file cycle.
if (resolved != null && resolved != from && files.contains(resolved)) {
targets.add(resolved);
}
}
edges[from] = targets;
}
return ImportGraph(edges, packages: {'': packageName, ...packages});
}