internal IPackage FindPackage(Func<string, IPackage> openPackage, string packageId, SemanticVersion version)
{
var lookupPackageName = new PackageName(packageId, version);
string packagePath;
// If caching is enabled, check if we have a cached path. Additionally, verify that the file actually exists on disk since it might have moved.
if (_enableCaching &&
_packagePathLookup.TryGetValue(lookupPackageName, out packagePath) &&
FileSystem.FileExists(packagePath))
{
// When depending on the cached path, verify the file exists on disk.
return GetPackage(openPackage, packagePath);
}
// Lookup files which start with the name "<Id>." and attempt to match it with all possible version string combinations (e.g. 1.2.0, 1.2.0.0)
// before opening the package. To avoid creating file name strings, we attempt to specifically match everything after the last path separator
// which would be the file name and extension.
return (from path in GetPackageLookupPaths(packageId, version)
let package = GetPackage(openPackage, path)
where lookupPackageName.Equals(new PackageName(package.Id, package.Version))
select package).FirstOrDefault();
}