private IEnumerable <IPackage> SearchImpl(string searchTerm, NuGetRequest nugetRequest)
        {
            if (nugetRequest == null)
            {
                yield break;
            }

            nugetRequest.Debug(Resources.Messages.SearchingRepository, "LocalPackageRepository", Source);

            var files = Directory.GetFiles(Source);
            IEnumerable <IPackage> packageItems = files.Select(nugetRequest.GetPackageByFilePath).Where(pkgItem => pkgItem != null).Select(pkg => pkg.Package);

            if (nugetRequest.FilterOnTag != null)
            {
                foreach (var package in PackageFilterUtility.FilterOnTags(packageItems, nugetRequest.FilterOnTag.Value))
                {
                    yield return(package);
                }
            }
            else
            {
                foreach (IPackage package in packageItems)
                {
                    yield return(package);
                }
            }

            // look in the package source location for directories that contain nupkg files.
            var subdirs = Directory.EnumerateDirectories(Source, "*", SearchOption.AllDirectories);

            foreach (var subdir in subdirs)
            {
                var nupkgs = Directory.EnumerateFileSystemEntries(subdir, "*.nupkg", SearchOption.TopDirectoryOnly);
                packageItems = nupkgs.Select(nugetRequest.GetPackageByFilePath).Where(pkgItem => pkgItem != null).Select(pkg => pkg.Package);
                if (nugetRequest.FilterOnTag != null)
                {
                    foreach (var package in PackageFilterUtility.FilterOnTags(packageItems, nugetRequest.FilterOnTag.Value))
                    {
                        yield return(package);
                    }
                }
                else
                {
                    foreach (IPackage package in packageItems)
                    {
                        yield return(package);
                    }
                }
            }
        }
        private IEnumerable <PackageBase> GetPackagesForPackageEntry(dynamic packageEntry, NuGetSearchContext searchContext, RequestWrapper request)
        {
            PackageEntryInfo packageEntryInfo = new PackageEntryInfo(packageEntry.id);

            if (!PackageFilterUtility.IsValidByName(packageEntryInfo, searchContext))
            {
                yield break;
            }

            // This will help us take packageEntryInfo.LatestVersion and packageEntryInfo.AbsoluteLatestVersion and get the matching package easily
            // We're not setting isLatestVersion here so we don't have to deal with "is it latest or absolute latest"
            Dictionary <SemanticVersion, PackageBase> versionToPackageTable = new Dictionary <SemanticVersion, PackageBase>();
            NuGetSearchContext individualPackageSearchContext = new NuGetSearchContext()
            {
                PackageInfo              = packageEntryInfo,
                AllVersions              = searchContext.AllVersions,
                AllowPrerelease          = searchContext.AllowPrerelease,
                RequiredVersion          = searchContext.RequiredVersion,
                MinimumVersion           = searchContext.MinimumVersion,
                MaximumVersion           = searchContext.MaximumVersion,
                EnableDeepMetadataBypass = searchContext.EnableDeepMetadataBypass
            };
            bool latestVersionRequired = !searchContext.AllVersions && searchContext.RequiredVersion == null && searchContext.MinimumVersion == null && searchContext.MaximumVersion == null;

            if (searchContext.EnableDeepMetadataBypass)
            {
                if (latestVersionRequired)
                {
                    // Use the search result page to get the metadata for the latest version
                    SemanticVersion individualPackageVersion = new SemanticVersion(packageEntry.version);
                    packageEntryInfo.AddVersion(individualPackageVersion);
                    PackageBase pb = this.ResourcesCollection.PackageConverter.Make(packageEntry);
                    if (pb != null)
                    {
                        yield return(pb);
                    }
                }
                else
                {
                    // Go to the registration index of this package first. This allows us to bypass "deep" (but required) metadata in certain cases.
                    NuGetPackageFeed3 packageFeed3 = (NuGetPackageFeed3)this.ResourcesCollection.PackagesFeed;
                    NuGetSearchResult result       = packageFeed3.Find(individualPackageSearchContext, request);
                    foreach (PackageBase pb in result.Result.Cast <PackageBase>())
                    {
                        yield return(pb);
                    }
                }
            }
            else
            {
                // Either we want a specific version or we want all metadata for any packages
                foreach (dynamic packageVersionEntry in packageEntry.versions)
                {
                    if (packageEntry.version.Equals(packageVersionEntry.version) || searchContext.AllVersions)
                    {
                        if (packageVersionEntry.Metadata.HasProperty("id"))
                        {
                            // Collect all versions from the search results so we can manually set isLatestVersion and isAbsoluteLatestVersion later
                            SemanticVersion individualPackageVersion = new SemanticVersion(packageVersionEntry.version);
                            packageEntryInfo.AddVersion(individualPackageVersion);

                            // Skip prerelease versions if AllowPrereleaseVersions is not specified
                            if (!String.IsNullOrEmpty(individualPackageVersion.SpecialVersion) && !searchContext.AllowPrerelease)
                            {
                                continue;
                            }

                            long?versionDownloadCount = null;
                            if (packageVersionEntry.HasProperty("downloads"))
                            {
                                versionDownloadCount = packageVersionEntry.downloads;
                            }

                            string registrationUrl = packageVersionEntry.Metadata.id;
                            // This should be PackageFeed3
                            // There should be a better way to reuse this function
                            NuGetPackageFeed3 packageFeed3          = (NuGetPackageFeed3)this.ResourcesCollection.PackagesFeed;
                            PackageBase       packageVersionPackage = packageFeed3.Find(registrationUrl, individualPackageSearchContext, request, true).FirstOrDefault();
                            if (packageVersionPackage != null)
                            {
                                if (versionDownloadCount.HasValue)
                                {
                                    packageVersionPackage.VersionDownloadCount = versionDownloadCount.Value;
                                }

                                // Reset these so we haven't collected all versions yet, so this is wrong
                                packageVersionPackage.IsLatestVersion         = false;
                                packageVersionPackage.IsAbsoluteLatestVersion = false;

                                versionToPackageTable[individualPackageVersion] = packageVersionPackage;
                            }
                        }
                    }
                }

                // Now manually set the latest versions
                if (packageEntryInfo.LatestVersion != null && versionToPackageTable.ContainsKey(packageEntryInfo.LatestVersion))
                {
                    versionToPackageTable[packageEntryInfo.LatestVersion].IsLatestVersion = true;
                }

                if (packageEntryInfo.AbsoluteLatestVersion != null && versionToPackageTable.ContainsKey(packageEntryInfo.AbsoluteLatestVersion))
                {
                    versionToPackageTable[packageEntryInfo.AbsoluteLatestVersion].IsAbsoluteLatestVersion = true;
                }

                // I think this is the best we can do for enumeration (reads all versions of a package before yielding anything)
                foreach (PackageBase package in versionToPackageTable.Values)
                {
                    yield return(package);
                }
            }
        }