private List <LocalPackageInfo> GetPackages(string id)
        {
            var packages = new List <LocalPackageInfo>();

            var packageIdRoot = PathResolver.GetVersionListPath(id);

            if (!Directory.Exists(packageIdRoot))
            {
                return(packages);
            }

            foreach (var fullVersionDir in Directory.EnumerateDirectories(packageIdRoot))
            {
                LocalPackageInfo package;
                if (!_packageCache.TryGetValue(fullVersionDir, out package))
                {
                    var versionPart = fullVersionDir.Substring(packageIdRoot.Length).TrimStart(Path.DirectorySeparatorChar);

                    // Get the version part and parse it
                    NuGetVersion version;
                    if (!NuGetVersion.TryParse(versionPart, out version))
                    {
                        continue;
                    }

                    var hashPath = PathResolver.GetHashPath(id, version);

                    // The hash file is written last. If this file does not exist then the package is
                    // incomplete and should not be used.
                    if (_packageFileCache.Sha512Exists(hashPath))
                    {
                        var manifestPath = PathResolver.GetManifestFilePath(id, version);
                        var zipPath      = PathResolver.GetPackageFilePath(id, version);
                        var sha512Path   = PathResolver.GetHashPath(id, version);

                        var nuspec       = _packageFileCache.GetOrAddNuspec(manifestPath, fullVersionDir);
                        var files        = _packageFileCache.GetOrAddFiles(fullVersionDir);
                        var sha512       = _packageFileCache.GetOrAddSha512(hashPath);
                        var runtimeGraph = _packageFileCache.GetOrAddRuntimeGraph(fullVersionDir);

                        package = new LocalPackageInfo(id, version, fullVersionDir, manifestPath, zipPath, sha512Path, nuspec, files, sha512, runtimeGraph);

                        // Cache the package, if it is valid it will not change
                        // for the life of this restore.
                        // Locking is done at a higher level around the id
                        _packageCache.TryAdd(fullVersionDir, package);
                    }
                }

                // Add the package if it is valid
                if (package != null)
                {
                    packages.Add(package);
                }
            }

            return(packages);
        }
        public IEnumerable <LocalPackageInfo> FindPackagesById(string packageId)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentNullException(nameof(packageId));
            }

            // packages\{packageId}\{version}\{packageId}.nuspec
            return(_cache.GetOrAdd(packageId, id =>
            {
                var packages = new List <LocalPackageInfo>();

                var packageIdRoot = PathResolver.GetVersionListPath(id);

                if (!Directory.Exists(packageIdRoot))
                {
                    return packages;
                }

                foreach (var fullVersionDir in Directory.EnumerateDirectories(packageIdRoot))
                {
                    var versionPart = fullVersionDir.Substring(packageIdRoot.Length).TrimStart(Path.DirectorySeparatorChar);

                    // Get the version part and parse it
                    NuGetVersion version;
                    if (!NuGetVersion.TryParse(versionPart, out version))
                    {
                        continue;
                    }

                    var hashPath = PathResolver.GetHashPath(id, version);

                    // The hash file is written last. If this file does not exist then the package is
                    // incomplete and should not be used.
                    if (File.Exists(hashPath))
                    {
                        var manifestPath = PathResolver.GetManifestFilePath(id, version);
                        var zipPath = PathResolver.GetPackageFilePath(id, version);

                        packages.Add(new LocalPackageInfo(id, version, fullVersionDir, manifestPath, zipPath));
                    }
                }

                return packages;
            }));
        }
        private List <LocalPackageInfo> GetPackages(string id)
        {
            var packages = new List <LocalPackageInfo>();

            var packageIdRoot = PathResolver.GetVersionListPath(id);

            if (!Directory.Exists(packageIdRoot))
            {
                return(packages);
            }

            foreach (var fullVersionDir in Directory.EnumerateDirectories(packageIdRoot))
            {
                LocalPackageInfo package;
                if (!_packageCache.TryGetValue(fullVersionDir, out package))
                {
                    var versionPart = fullVersionDir.Substring(packageIdRoot.Length).TrimStart(Path.DirectorySeparatorChar);

                    // Get the version part and parse it
                    NuGetVersion version;
                    if (!NuGetVersion.TryParse(versionPart, out version))
                    {
                        continue;
                    }

                    package = GetPackage(id, version, fullVersionDir);
                }

                // Add the package if it is valid
                if (package != null)
                {
                    packages.Add(package);
                }
            }

            return(packages);
        }
        private List <LocalPackageInfo> GetPackages(string id)
        {
            var packages = new List <LocalPackageInfo>();

            var packageIdRoot = PathResolver.GetVersionListPath(id);

            if (!Directory.Exists(packageIdRoot))
            {
                return(packages);
            }

            foreach (var fullVersionDir in Directory.EnumerateDirectories(packageIdRoot))
            {
                LocalPackageInfo package;
                if (!_packageCache.TryGetValue(fullVersionDir, out package))
                {
                    var versionPart = fullVersionDir.Substring(packageIdRoot.Length).TrimStart(Path.DirectorySeparatorChar);

                    // Get the version part and parse it
                    NuGetVersion version;
                    if (!NuGetVersion.TryParse(versionPart, out version))
                    {
                        continue;
                    }

                    var nupkgMetadataPath = PathResolver.GetNupkgMetadataPath(id, version);
                    var hashPath          = PathResolver.GetHashPath(id, version);
                    var zipPath           = PathResolver.GetPackageFilePath(id, version);
                    var installPath       = PathResolver.GetInstallPath(id, version);

                    // The nupkg metadata file is written last. If this file does not exist then the package is
                    // incomplete and should not be used.
                    if (_packageFileCache.Sha512Exists(nupkgMetadataPath))
                    {
                        package = CreateLocalPackageInfo(id, version, fullVersionDir, nupkgMetadataPath, zipPath);

                        // Cache the package, if it is valid it will not change
                        // for the life of this restore.
                        // Locking is done at a higher level around the id
                        _packageCache.TryAdd(fullVersionDir, package);
                    }
                    // if hash file exists and it's not a fallback folder then we generate nupkg metadata file
                    else if (!_isFallbackFolder && _packageFileCache.Sha512Exists(hashPath))
                    {
                        LocalFolderUtility.GenerateNupkgMetadataFile(zipPath, installPath, hashPath, nupkgMetadataPath);

                        package = CreateLocalPackageInfo(id, version, fullVersionDir, nupkgMetadataPath, zipPath);

                        // Cache the package, if it is valid it will not change
                        // for the life of this restore.
                        // Locking is done at a higher level around the id
                        _packageCache.TryAdd(fullVersionDir, package);
                    }
                }

                // Add the package if it is valid
                if (package != null)
                {
                    packages.Add(package);
                }
            }

            return(packages);
        }