Example #1
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);
        }
Example #2
0
 public virtual IReadOnlyList <IndexAction <KeyedDocument> > UpdatePackage(
     PackageRegistration registration)
 {
     return(AddOrUpdatePackage(registration, isUpdate: true));
 }