findEscapingPaths function
Every escaping path literal in packageRoot's shipped script directories.
Implementation
List<EscapingPath> findEscapingPaths(Directory packageRoot) {
final out = <EscapingPath>[];
if (!packageRoot.existsSync()) return out;
final rootPath = packageRoot.uri.toFilePath();
final unshipped = readPubignoreDirs(packageRoot);
for (final dirName in shippedScriptDirs) {
if (unshipped.contains(dirName)) continue;
final dir = Directory('${packageRoot.path}/$dirName');
if (!dir.existsSync()) continue;
for (final entity in dir.listSync(recursive: true)) {
if (entity is! File || !entity.path.endsWith('.dart')) continue;
final lines = entity.readAsLinesSync();
for (var i = 0; i < lines.length; i++) {
if (_isCommentOrDirective(lines[i])) continue;
for (final m in _relativeLiteral.allMatches(lines[i])) {
final literal = m.group(1)!;
final fileDir = File(entity.path).parent.path;
// The two bases a Dart path literal can be resolved against.
final candidates = [
Uri.file('$fileDir/').resolve(literal).toFilePath(),
Uri.file('${packageRoot.path}/').resolve(literal).toFilePath(),
];
final ok = candidates.any((c) {
final normalised = c.endsWith('/')
? c.substring(0, c.length - 1)
: c;
final within =
normalised == rootPath.replaceAll(RegExp(r'/$'), '') ||
normalised.startsWith(rootPath);
return within &&
(File(normalised).existsSync() ||
Directory(normalised).existsSync());
});
if (!ok) {
final rel = entity.path.startsWith(rootPath)
? entity.path.substring(rootPath.length)
: entity.path;
out.add(EscapingPath(rel, i + 1, literal));
}
}
}
}
}
return out;
}