fromDir static method
This factory is guaranteed to return the same object for any given
dir.absolute.path. Multiple dir.absolute.paths will resolve to the
same object if they are part of the same package. Returns null if the
directory is not part of a known package.
Implementation
static PackageMeta? fromDir(
Folder folder,
ResourceProvider resourceProvider,
) {
var pathContext = resourceProvider.pathContext;
folder = resourceProvider.getFolder(pathContext.absolute(folder.path));
if (!folder.exists) {
throw PackageMetaFailure(
'fatal error: unable to locate the input directory at '
"'${folder.path}'.",
);
}
return _packageMetaCache.putIfAbsent(pathContext.absolute(folder.path), () {
// There are pubspec.yaml files inside the SDK. Ignore them.
var parentSdkDir = sdkDirParent(folder, resourceProvider);
if (parentSdkDir != null) {
return _SdkMeta(parentSdkDir, resourceProvider);
} else {
for (var dir in folder.withAncestors) {
var pubspec = resourceProvider.getFile(
pathContext.join(dir.path, 'pubspec.yaml'),
);
if (pubspec.exists) {
try {
return _FilePackageMeta(dir, resourceProvider);
} on YamlException {
// Skip scaffold/template pubspec files that are not valid YAML
// for the current package scan, and continue walking up to the
// nearest real package root.
continue;
} catch (error) {
if (error is TypeError) {
continue;
}
rethrow;
}
}
}
}
return null;
});
}