Esempio n. 1
0
        private async Task <IEnumerable <string> > GetPackageIdsFromLocalPackageRepository(
            string searchFilter,
            bool includePrerelease,
            ILogger log,
            CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            return(await Task.Run(() =>
            {
                var packages = _localResource.GetPackages(log, token);

                if (!string.IsNullOrEmpty(searchFilter))
                {
                    packages = packages.Where(p =>
                                              p.Identity.Id.StartsWith(searchFilter, StringComparison.OrdinalIgnoreCase));
                }

                if (!includePrerelease)
                {
                    packages = packages.Where(p => !p.Identity.Version.IsPrerelease);
                }

                return packages.Select(p => p.Identity.Id)
                .Distinct(StringComparer.OrdinalIgnoreCase)
                .Take(30)
                .ToList();
            },
                                  token));
        }
Esempio n. 2
0
        public async override Task <IEnumerable <IPackageSearchMetadata> > SearchAsync(
            string searchTerm,
            SearchFilter filters,
            int skip,
            int take,
            ILogger log,
            CancellationToken token)
        {
            return(await Task.Factory.StartNew(() =>
            {
                // Check if source is available.
                if (!IsLocalOrUNC(_localResource.Root))
                {
                    throw new InvalidOperationException(string.Format(
                                                            Strings.Protocol_Search_LocalSourceNotFound,
                                                            _localResource.Root));
                }

                var query = _localResource.GetPackages(log, token);

                // Filter on prerelease
                query = query.Where(package => filters.IncludePrerelease || !package.Identity.Version.IsPrerelease);

                // Filter on search terms
                if (!string.IsNullOrEmpty(searchTerm))
                {
                    var terms = searchTerm.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    query = query.Where(package => ContainsAnyTerm(terms, package));
                }

                // Collapse to the highest version per id, if necessary
                var collapsedQuery = filters?.Filter == SearchFilterType.IsLatestVersion ||
                                     filters?.Filter == SearchFilterType.IsAbsoluteLatestVersion
                                     ? CollapseToHighestVersion(query) : query;

                // execute the query
                var packages = collapsedQuery
                               .Skip(skip)
                               .Take(take)
                               .ToArray();

                // Create final results and retrieve all versions for each package.
                return packages
                .Select(package => CreatePackageSearchResult(package, filters, log, token))
                .ToArray();
            }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
        }