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);
        }
        private LocalPackageInfo CreateLocalPackageInfo(string id, NuGetVersion version, string fullVersionDir, string newHashPath, string zipPath)
        {
            var manifestPath = PathResolver.GetManifestFilePath(id, version);
            var nuspec       = _packageFileCache.GetOrAddNuspec(manifestPath, fullVersionDir);
            var files        = _packageFileCache.GetOrAddFiles(fullVersionDir);
            var sha512       = _packageFileCache.GetOrAddSha512(newHashPath);
            var runtimeGraph = _packageFileCache.GetOrAddRuntimeGraph(fullVersionDir);

            return(new LocalPackageInfo(id, version, fullVersionDir, manifestPath, zipPath, newHashPath, nuspec, files, sha512, runtimeGraph));
        }
        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;
            }));
        }