Пример #1
0
        public async Task <JObject> CreatePackageBlob(PackageInput packageInput)
        {
            var rootUri = GetPackageUri(packageInput.Identity);

            var json = JsonUtility.Create(rootUri, new string[] { "Package", "http://schema.nuget.org/catalog#Permalink" });

            var packageDetailsFile = _context.Source.Get(packageInput.PackageDetailsUri);

            if (!await packageDetailsFile.Exists(_context.Log, _context.Token))
            {
                throw new FileNotFoundException($"Unable to find {packageDetailsFile.EntityUri.AbsoluteUri}");
            }

            var detailsJson = await packageDetailsFile.GetJson(_context.Log, _context.Token);

            json.Add("catalogEntry", packageInput.PackageDetailsUri.AbsoluteUri);
            json.Add("packageContent", detailsJson["packageContent"].ToString());
            json.Add("registration", GetIndexUri(packageInput.Identity));

            var copyProperties = new List <string>()
            {
                "listed",
                "published",
            };

            JsonUtility.CopyProperties(detailsJson, json, copyProperties, skipEmpty: true);

            var context = JsonUtility.GetContext("Package");

            json.Add("@context", context);

            return(JsonLDTokenComparer.Format(json));
        }
Пример #2
0
        public JObject CreateIndex(Uri indexUri, List <JObject> packageDetails)
        {
            var json = JsonUtility.Create(indexUri,
                                          new string[] {
                "catalog:CatalogRoot",
                "PackageRegistration",
                "catalog:Permalink"
            });

            json.Add("commitId", _context.CommitId.ToString().ToLowerInvariant());
            json.Add("commitTimeStamp", DateTimeOffset.UtcNow.GetDateString());

            var itemsArray = new JArray();

            json.Add("items", itemsArray);
            json.Add("count", 1);

            // Add everything to a single page
            var pageJson = CreatePage(indexUri, packageDetails);

            itemsArray.Add(pageJson);

            var context = JsonUtility.GetContext("Registration");

            json.Add("@context", context);

            return(JsonLDTokenComparer.Format(json));
        }
Пример #3
0
        /// <summary>
        /// Create PackageDetails for a delete
        /// </summary>
        public JObject CreateDeleteDetails(PackageIdentity package, string reason)
        {
            var now    = DateTimeOffset.UtcNow;
            var pageId = Guid.NewGuid().ToString().ToLowerInvariant();

            var rootUri = UriUtility.CreateUri($"{_context.Source.BaseURI}catalog/data/{pageId}.json");

            var json = JsonUtility.Create(rootUri, new List <string>()
            {
                "PackageDetails", "catalog:Permalink"
            });

            json.Add("commitId", _context.CommitId.ToString().ToLowerInvariant());
            json.Add("commitTimeStamp", now.GetDateString());
            json.Add("sleet:operation", "remove");

            var context = JsonUtility.GetContext("Catalog");

            json.Add("@context", context);

            json.Add("id", package.Id);
            json.Add("version", package.Version.ToFullVersionString());

            json.Add("created", DateTimeOffset.UtcNow.GetDateString());
            json.Add("sleet:removeReason", reason);

            json.Add("sleet:toolVersion", Constants.SleetVersion.ToFullVersionString());

            return(JsonLDTokenComparer.Format(json));
        }
Пример #4
0
        /// <summary>
        /// Catalog index page.
        /// </summary>
        public JObject CreateCatalogPage(Uri indexUri, Uri rootUri, List <JObject> packageDetails)
        {
            var json = JsonUtility.Create(rootUri, "CatalogPage");

            json.Add("commitId", _context.CommitId.ToString().ToLowerInvariant());
            json.Add("commitTimeStamp", DateTimeOffset.UtcNow.GetDateString());
            json.Add("count", packageDetails.Count);
            json.Add("parent", indexUri.AbsoluteUri);

            var itemArray = new JArray();

            json.Add("items", itemArray);

            foreach (var entry in packageDetails
                     .OrderBy(e => e["commitTimeStamp"].ToObject <DateTimeOffset>())
                     .ThenBy(e => e["@id"].ToString()))
            {
                itemArray.Add(entry);
            }

            var context = JsonUtility.GetContext("CatalogPage");

            json.Add("@context", context);

            return(JsonLDTokenComparer.Format(json));
        }
Пример #5
0
        /// <summary>
        /// Create a PackageDetails page that contains all the package information.
        /// </summary>
        public JObject CreatePackageDetails(PackageInput packageInput)
        {
            var now          = DateTimeOffset.UtcNow;
            var package      = packageInput.Package;
            var nuspec       = XDocument.Load(package.GetNuspec());
            var nuspecReader = new NuspecReader(nuspec);

            var pageId = Guid.NewGuid().ToString().ToLowerInvariant();

            var rootUri = UriUtility.CreateUri($"{_context.Source.BaseURI}catalog/data/{pageId}.json");

            packageInput.PackageDetailsUri = rootUri;

            var json = JsonUtility.Create(rootUri, new List <string>()
            {
                "PackageDetails", "catalog:Permalink"
            });

            json.Add("commitId", _context.CommitId.ToString().ToLowerInvariant());
            json.Add("commitTimeStamp", DateTimeOffset.UtcNow.GetDateString());
            json.Add("sleet:operation", "add");

            var context = JsonUtility.GetContext("Catalog");

            json.Add("@context", context);

            json.Add("id", packageInput.Identity.Id);
            json.Add("version", packageInput.Identity.Version.ToFullVersionString());

            json.Add("created", now.GetDateString());
            json.Add("lastEdited", "0001-01-01T00:00:00Z");

            var copyProperties = new List <string>()
            {
                "authors",
                "copyright",
                "description",
                "iconUrl",
                "projectUrl",
                "licenseUrl",
                "language",
                "summary",
                "owners",
                "releaseNotes"
            };

            foreach (var propertyName in copyProperties)
            {
                json.Add(CreateProperty(propertyName, propertyName, nuspecReader));
            }

            json.Add("isPrerelease", packageInput.Identity.Version.IsPrerelease);

            // Unused?
            json.Add("licenseNames", string.Empty);
            json.Add("licenseReportUrl", string.Empty);

            // All packages are listed
            json.Add("listed", true);

            var titleValue = GetEntry(nuspecReader, "title");

            if (!string.IsNullOrEmpty(titleValue))
            {
                json.Add("title", titleValue);
            }

            using (var stream = File.OpenRead(packageInput.PackagePath))
            {
                using (var sha512 = SHA512.Create())
                {
                    var packageHash = Convert.ToBase64String(sha512.ComputeHash(stream));

                    json.Add("packageHash", packageHash);
                    json.Add("packageHashAlgorithm", "SHA512");
                }

                json.Add("packageSize", stream.Length);
            }

            json.Add("published", now.GetDateString());
            json.Add("requireLicenseAcceptance", GetEntry(nuspecReader, "requireLicenseAcceptance").Equals("true", StringComparison.OrdinalIgnoreCase));

            var minVersion = nuspecReader.GetMinClientVersion();

            if (minVersion != null)
            {
                json.Add("minClientVersion", minVersion.ToIdentityString());
            }

            // Tags
            var tagSet = new HashSet <string>(GetEntry(nuspecReader, "tags").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), StringComparer.OrdinalIgnoreCase);

            tagSet.Remove(string.Empty);
            var tagArray = new JArray(tagSet);

            json.Add("tags", tagArray);

            // Framework assemblies
            var fwrGroups = nuspecReader.GetFrameworkReferenceGroups();
            var fwrArray  = new JArray();

            json.Add("frameworkAssemblyGroup", fwrArray);

            foreach (var group in fwrGroups.OrderBy(e => e.TargetFramework.GetShortFolderName(), StringComparer.OrdinalIgnoreCase))
            {
                var groupTFM  = group.TargetFramework.GetShortFolderName().ToLowerInvariant();
                var groupNode = JsonUtility.Create(rootUri, $"frameworkassemblygroup/{groupTFM}".ToLowerInvariant(), "FrameworkAssemblyGroup");

                // Leave the framework property out for the 'any' group
                if (!group.TargetFramework.IsAny)
                {
                    groupNode.Add("targetFramework", groupTFM);
                }

                fwrArray.Add(groupNode);

                if (group.Items.Any())
                {
                    var assemblyArray = new JArray();
                    groupNode.Add("assembly", assemblyArray);

                    foreach (var fwAssembly in group.Items.Distinct().OrderBy(e => e, StringComparer.OrdinalIgnoreCase))
                    {
                        assemblyArray.Add(fwAssembly);
                    }
                }
            }

            // Dependencies
            var dependencyGroups = nuspecReader.GetDependencyGroups();

            var depArray = new JArray();

            json.Add("dependencyGroups", depArray);

            foreach (var group in dependencyGroups.OrderBy(e => e.TargetFramework.GetShortFolderName(), StringComparer.OrdinalIgnoreCase))
            {
                var groupTFM  = group.TargetFramework.GetShortFolderName().ToLowerInvariant();
                var groupNode = JsonUtility.Create(rootUri, $"dependencygroup/{groupTFM}".ToLowerInvariant(), "PackageDependencyGroup");

                // Leave the framework property out for the 'any' group
                if (!group.TargetFramework.IsAny)
                {
                    groupNode.Add("targetFramework", groupTFM);
                }

                depArray.Add(groupNode);

                if (group.Packages.Any())
                {
                    var packageArray = new JArray();
                    groupNode.Add("dependencies", packageArray);

                    foreach (var depPackage in group.Packages.Distinct().OrderBy(e => e.Id, StringComparer.OrdinalIgnoreCase))
                    {
                        var packageNode = JsonUtility.Create(rootUri, $"dependencygroup/{groupTFM}/{depPackage.Id}".ToLowerInvariant(), "PackageDependency");
                        packageNode.Add("id", depPackage.Id);
                        packageNode.Add("range", depPackage.VersionRange.ToNormalizedString());

                        packageArray.Add(packageNode);
                    }
                }
            }

            json.Add("packageContent", packageInput.NupkgUri.AbsoluteUri);

            // add flatcontainer files
            var packageEntriesArray = new JArray();

            json.Add("packageEntries", packageEntriesArray);
            var packageEntryIndex = 0;

            foreach (var entry in packageInput.Zip.Entries.OrderBy(e => e.FullName, StringComparer.OrdinalIgnoreCase))
            {
                var fileEntry = JsonUtility.Create(rootUri, $"packageEntry/{packageEntryIndex}", "packageEntry");
                fileEntry.Add("fullName", entry.FullName);
                fileEntry.Add("length", entry.Length);
                fileEntry.Add("lastWriteTime", entry.LastWriteTime.GetDateString());

                packageEntriesArray.Add(fileEntry);
                packageEntryIndex++;
            }

            json.Add("sleet:toolVersion", Constants.SleetVersion.ToFullVersionString());

            return(JsonLDTokenComparer.Format(json));
        }