Example #1
0
        private async Task <PackageDocument> BuildDocumentAsync(string packageId)
        {
            if (packageId == null)
            {
                throw new ArgumentNullException(nameof(packageId));
            }

            var packages = await _packageService.FindAsync(packageId);

            if (packages.Count == 0)
            {
                _logger.LogError("Could not find package with id {PackageId}", packageId);

                throw new ArgumentException($"Invalid package id {packageId}", nameof(packageId));
            }

            var result = new PackageDocument();

            var latest       = packages.OrderByDescending(p => p.Version).First();
            var versions     = packages.OrderBy(p => p.Version).ToList();
            var dependencies = latest
                               .Dependencies
                               .Select(d => d.Id?.ToLowerInvariant())
                               .Where(d => d != null)
                               .Distinct()
                               .ToArray();

            result.Key                = EncodeKey(packageId.ToLowerInvariant());
            result.Id                 = latest.Id;
            result.Version            = latest.VersionString;
            result.Description        = latest.Description;
            result.Authors            = latest.Authors;
            result.IconUrl            = latest.IconUrlString;
            result.LicenseUrl         = latest.LicenseUrlString;
            result.ProjectUrl         = latest.ProjectUrlString;
            result.Published          = latest.Published;
            result.Summary            = latest.Summary;
            result.Tags               = latest.Tags;
            result.Title              = latest.Title;
            result.TotalDownloads     = versions.Sum(p => p.Downloads);
            result.DownloadsMagnitude = result.TotalDownloads.ToString().Length;
            result.Versions           = versions.Select(p => p.VersionString).ToArray();
            result.VersionDownloads   = versions.Select(p => p.Downloads.ToString()).ToArray();
            result.Dependencies       = dependencies;

            return(result);
        }
Example #2
0
        private async Task <IReadOnlyList <PackageDocument> > BuildDocumentsAsync(string packageId)
        {
            if (packageId == null)
            {
                throw new ArgumentNullException(nameof(packageId));
            }

            var packages = await _packageService.FindAsync(packageId, includeUnlisted : false);

            if (packages.Count == 0)
            {
                _logger.LogError("Could not find package with id {PackageId}", packageId);

                throw new ArgumentException($"Invalid package id {packageId}", nameof(packageId));
            }

            var result = new List <PackageDocument>();

            for (var i = 0; i < 4; i++)
            {
                var includePrerelease = (i & 1) != 0;
                var includeSemVer2    = (i & 2) != 0;
                var searchFilters     = (SearchFilters)i;

                IEnumerable <Package> filtered = packages;
                if (!includePrerelease)
                {
                    filtered = filtered.Where(p => !p.IsPrerelease);
                }

                if (!includeSemVer2)
                {
                    filtered = filtered.Where(p => p.SemVerLevel != SemVerLevel.SemVer2);
                }

                var versions = filtered.OrderBy(p => p.Version).ToList();
                if (versions.Count == 0)
                {
                    continue;
                }

                var latest       = versions.Last();
                var dependencies = latest
                                   .Dependencies
                                   .Select(d => d.Id?.ToLowerInvariant())
                                   .Where(d => d != null)
                                   .Distinct()
                                   .ToArray();

                var document  = new PackageDocument();
                var encodedId = EncodeKey(packageId.ToLowerInvariant());

                document.Key                = $"{encodedId}-{searchFilters}";
                document.Id                 = latest.Id;
                document.Version            = latest.Version.ToFullString();
                document.Description        = latest.Description;
                document.Authors            = latest.Authors;
                document.IconUrl            = latest.IconUrlString;
                document.LicenseUrl         = latest.LicenseUrlString;
                document.ProjectUrl         = latest.ProjectUrlString;
                document.Published          = latest.Published;
                document.Summary            = latest.Summary;
                document.Tags               = latest.Tags;
                document.Title              = latest.Title;
                document.TotalDownloads     = versions.Sum(p => p.Downloads);
                document.DownloadsMagnitude = document.TotalDownloads.ToString().Length;
                document.Versions           = versions.Select(p => p.Version.ToFullString()).ToArray();
                document.VersionDownloads   = versions.Select(p => p.Downloads.ToString()).ToArray();
                document.Dependencies       = dependencies;
                document.PackageTypes       = latest.PackageTypes.Select(t => t.Name).ToArray();
                document.Frameworks         = latest.TargetFrameworks.Select(f => f.Moniker.ToLowerInvariant()).ToArray();
                document.SearchFilters      = searchFilters.ToString();

                result.Add(document);
            }

            return(result);
        }
Example #3
0
        private IReadOnlyList <IndexAction <KeyedDocument> > AddOrUpdatePackage(
            PackageRegistration registration,
            bool isUpdate)
        {
            var encodedId = EncodePackageId(registration.PackageId.ToLowerInvariant());
            var result    = new List <IndexAction <KeyedDocument> >();

            for (var i = 0; i < 4; i++)
            {
                var includePrerelease = (i & 1) != 0;
                var includeSemVer2    = (i & 2) != 0;
                var searchFilters     = (SearchFilters)i;

                var documentKey = $"{encodedId}-{searchFilters}";
                var filtered    = registration.Packages.Where(p => p.Listed);

                if (!includePrerelease)
                {
                    filtered = filtered.Where(p => !p.IsPrerelease);
                }

                if (!includeSemVer2)
                {
                    filtered = filtered.Where(p => p.SemVerLevel != SemVerLevel.SemVer2);
                }

                var versions = filtered.OrderBy(p => p.Version).ToList();
                if (versions.Count == 0)
                {
                    if (isUpdate)
                    {
                        var action = IndexAction.Delete(
                            new KeyedDocument
                        {
                            Key = documentKey
                        });

                        result.Add(action);
                    }

                    continue;
                }

                var latest       = versions.Last();
                var dependencies = latest
                                   .Dependencies
                                   .Select(d => d.Id?.ToLowerInvariant())
                                   .Where(d => d != null)
                                   .Distinct()
                                   .ToArray();

                var document = new PackageDocument();

                document.Key                = $"{encodedId}-{searchFilters}";
                document.Id                 = latest.Id;
                document.Version            = latest.Version.ToFullString();
                document.Description        = latest.Description;
                document.Authors            = latest.Authors;
                document.IconUrl            = latest.IconUrlString;
                document.LicenseUrl         = latest.LicenseUrlString;
                document.ProjectUrl         = latest.ProjectUrlString;
                document.Published          = latest.Published;
                document.Summary            = latest.Summary;
                document.Tags               = latest.Tags;
                document.Title              = latest.Title;
                document.TotalDownloads     = versions.Sum(p => p.Downloads);
                document.DownloadsMagnitude = document.TotalDownloads.ToString().Length;
                document.Versions           = versions.Select(p => p.Version.ToFullString()).ToArray();
                document.VersionDownloads   = versions.Select(p => p.Downloads.ToString()).ToArray();
                document.Dependencies       = dependencies;
                document.PackageTypes       = latest.PackageTypes.Select(t => t.Name).ToArray();
                document.Frameworks         = latest.TargetFrameworks.Select(f => f.Moniker.ToLowerInvariant()).ToArray();
                document.SearchFilters      = searchFilters.ToString();

                result.Add(
                    isUpdate
                        ? IndexAction.MergeOrUpload <KeyedDocument>(document)
                        : IndexAction.Upload <KeyedDocument>(document));
            }

            return(result);
        }