Beispiel #1
0
        public static void AssertVersion(this PackageArchiveReader package, string expectedVersion)
        {
            var nuspecReader  = new NuspecReader(package.GetNuspec());
            var actualVersion = nuspecReader.GetMetadata().Single(x => x.Key == "version").Value;

            Assert.That(actualVersion, Is.EqualTo(expectedVersion));
        }
Beispiel #2
0
 private static string GetEntry(NuspecReader reader, string property)
 {
     return(reader.GetMetadata()
            .Where(pair => StringComparer.OrdinalIgnoreCase.Equals(pair.Key, property))
            .FirstOrDefault()
            .Value ?? string.Empty);
 }
Beispiel #3
0
        public static void AssertReleaseNotes(this PackageArchiveReader package, string expectedReleaseNotes)
        {
            var nuspecReader       = new NuspecReader(package.GetNuspec());
            var actualReleaseNotes = nuspecReader.GetMetadata().Single(x => x.Key == "releaseNotes").Value;

            Assert.That(actualReleaseNotes, Is.EqualTo(expectedReleaseNotes));
        }
        private void LoadNuSpecFile(string filePath)
        {
            var nuSpecFilePath = Directory.GetFiles(System.IO.Path.GetDirectoryName(filePath), "*.nuspec", SearchOption.AllDirectories).FirstOrDefault();

            if (nuSpecFilePath != null && !nuSpecFilePath.StartsWith(NuGetPackagesPath))
            {
                NuSpecFilePath = nuSpecFilePath;
                using (var stream = File.Open(NuSpecFilePath, FileMode.Open))
                {
                    var reader = new NuspecReader(stream);

                    var packageId = reader.GetId();
                    if (packageId != null && packageId.ToLower() == "$id$")
                    {
                        packageId = Name;
                    }

                    NuGetPackageId    = packageId;
                    NuGetPackageTitle = Name;

                    var metadata = reader.GetMetadata().ToArray();
                    if (metadata.Any(p => p.Key == "title"))
                    {
                        var titlePair = metadata.SingleOrDefault(p => p.Key == "title");
                        NuGetPackageTitle = titlePair.Value;
                    }
                }
            }
        }
Beispiel #5
0
        public static void AssertTitle(this PackageArchiveReader package, string expectedTitle)
        {
            var nuspecReader = new NuspecReader(package.GetNuspec());
            var actualTitle  = nuspecReader.GetMetadata().Single(x => x.Key == "title").Value;

            Assert.That(actualTitle, Is.EqualTo(expectedTitle));
        }
 public static PackageMetadata FromNuspecReader(NuspecReader nuspecReader)
 {
     return(new PackageMetadata(
                nuspecReader.GetMetadata().ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                nuspecReader.GetDependencyGroups(),
                nuspecReader.GetFrameworkReferenceGroups()
                ));
 }
Beispiel #7
0
 /// <summary>
 /// Gets package metadata from a the provided <see cref="NuspecReader"/> instance.
 /// </summary>
 /// <param name="nuspecReader">The <see cref="NuspecReader"/> instance used to read the <see cref="PackageMetadata"/></param>
 /// <param name="strict">
 /// Whether or not to be strict when reading the <see cref="NuspecReader"/>. This should be <code>true</code>
 /// on initial ingestion but false when a package that has already been accepted is being processed.</param>
 /// <exception cref="PackagingException">
 /// We default to use a strict version-check on dependency groups.
 /// When an invalid dependency version range is detected, a <see cref="PackagingException"/> will be thrown.
 /// </exception>
 public static PackageMetadata FromNuspecReader(NuspecReader nuspecReader, bool strict)
 {
     return(new PackageMetadata(
                nuspecReader.GetMetadata().ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                nuspecReader.GetDependencyGroups(useStrictVersionCheck: strict),
                nuspecReader.GetFrameworkReferenceGroups(),
                nuspecReader.GetPackageTypes(),
                nuspecReader.GetMinClientVersion()
                ));
 }
Beispiel #8
0
        /// <summary>
        /// Gets package metadata from a the provided <see cref="NuspecReader"/> instance.
        /// </summary>
        /// <param name="nuspecReader">The <see cref="NuspecReader"/> instance used to read the <see cref="PackageMetadata"/></param>
        /// <param name="strict">
        /// Whether or not to be strict when reading the <see cref="NuspecReader"/>. This should be <code>true</code>
        /// on initial ingestion but false when a package that has already been accepted is being processed.</param>
        /// <exception cref="PackagingException">
        /// We default to use a strict version-check on dependency groups.
        /// When an invalid dependency version range is detected, a <see cref="PackagingException"/> will be thrown.
        /// </exception>
        public static PackageMetadata FromNuspecReader(NuspecReader nuspecReader, bool strict)
        {
            var strictNuspecReader = new StrictNuspecReader(nuspecReader.Xml);
            var metadataLookup     = strictNuspecReader.GetMetadataLookup();

            if (strict)
            {
                var duplicates = metadataLookup
                                 .Where(g => g.Count() > 1)
                                 .Select(g => g.Key)
                                 .ToList();

                if (duplicates.Any())
                {
                    throw new PackagingException(string.Format(
                                                     CoreStrings.Manifest_DuplicateMetadataElements,
                                                     string.Join("', '", duplicates)));
                }
            }

            // Reject invalid metadata element names. Today this only rejects element names that collide with
            // properties generated downstream.
            var metadataKeys = new HashSet <string>(metadataLookup.Select(g => g.Key));

            metadataKeys.IntersectWith(RestrictedMetadataElements);
            if (metadataKeys.Any())
            {
                throw new PackagingException(string.Format(
                                                 CoreStrings.Manifest_InvalidMetadataElements,
                                                 string.Join("', '", metadataKeys.OrderBy(x => x))));
            }

            // Reject non-boolean values for boolean metadata.
            foreach (var booleanName in BooleanMetadataElements)
            {
                foreach (var unparsedBoolean in metadataLookup[booleanName])
                {
                    if (!bool.TryParse(unparsedBoolean, out var parsedBoolean))
                    {
                        throw new PackagingException(string.Format(
                                                         CoreStrings.Manifest_InvalidBooleanMetadata,
                                                         booleanName));
                    }
                }
            }

            return(new PackageMetadata(
                       nuspecReader.GetMetadata().ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                       nuspecReader.GetDependencyGroups(useStrictVersionCheck: strict),
                       nuspecReader.GetFrameworkReferenceGroups(),
                       nuspecReader.GetPackageTypes(),
                       nuspecReader.GetMinClientVersion(),
                       nuspecReader.GetRepositoryMetadata(),
                       nuspecReader.GetLicenseMetadata()));
        }
Beispiel #9
0
        private static PackageMetadata GetPackageMetadata(string configPath)
        {
            var nuspecReader = new NuspecReader(configPath);

            var rawMetadata = nuspecReader.GetMetadata();

            if (rawMetadata == null || !rawMetadata.Any())
            {
                return(null);
            }

            return(PackageMetadata.FromNuspecReader(nuspecReader));
        }
Beispiel #10
0
        private static PackageMetadata GetPackageMetadata(string nuspecPath)
        {
            var nuspecReader = new NuspecReader(nuspecPath);

            var rawMetadata = nuspecReader.GetMetadata();

            if (rawMetadata == null || !rawMetadata.Any())
            {
                return(null);
            }

            var metadata = PackageMetadata.FromNuspecReader(nuspecReader);

            return(metadata);
        }
        public static IEnumerable <ValidationResult> Validate(Stream nuspecStream, out NuspecReader nuspecReader)
        {
            try
            {
                nuspecReader = new NuspecReader(nuspecStream);
                var rawMetadata = nuspecReader.GetMetadata();
                if (rawMetadata != null && rawMetadata.Any())
                {
                    return(ValidateCore(PackageMetadata.FromNuspecReader(nuspecReader)));
                }
            }
            catch (Exception ex)
            {
                nuspecReader = null;
                return(new [] { new ValidationResult(ex.Message) });
            }

            return(Enumerable.Empty <ValidationResult>());
        }
        public void GivenABunchOfPackageReferences()
        {
            var artifacts = Directory.GetFiles(TestPackagesDirectory, "*.nupkg");

            foreach (var artifact in artifacts)
            {
                using (var zip = ZipFile.OpenRead(artifact))
                {
                    var nuspecFileStream = zip.Entries.First(e => e.Name.EndsWith(".nuspec")).Open();
                    var nuspecReader     = new NuspecReader(nuspecFileStream);
                    var metadata         = nuspecReader.GetMetadata();
                    packageReferences.Add(new BuildPackageReference
                    {
                        Name        = metadata.Where(kvp => kvp.Key == "id").Select(i => i.Value).First(),
                        Version     = metadata.Where(kvp => kvp.Key == "version").Select(i => i.Value).First(),
                        PackagePath = artifact
                    });
                }
            }
        }
Beispiel #13
0
        public static IVsPackageMetadata CreateMetadata(string nupkgPath, PackageIdentity package)
        {
            IEnumerable <string> authors = Enumerable.Empty <string>();
            string description           = string.Empty;
            string title       = package.Id;
            string installPath = string.Empty;

            try
            {
                // installPath is the nupkg path
                FileInfo file = new FileInfo(nupkgPath);
                installPath = file.Directory.FullName;
                PackageReader reader = new PackageReader(file.OpenRead());

                using (var nuspecStream = reader.GetNuspec())
                {
                    NuspecReader nuspec = new NuspecReader(nuspecStream);

                    var metadata = nuspec.GetMetadata();

                    authors     = GetNuspecValue(metadata, "authors").Split(',').ToArray();
                    title       = GetNuspecValue(metadata, "title");
                    description = GetNuspecValue(metadata, "description");
                }
            }
            catch (Exception ex)
            {
                // ignore errors from reading the extra fields
                Debug.Fail(ex.ToString());
            }

            if (String.IsNullOrEmpty(title))
            {
                title = package.Id;
            }

            return(new VsPackageMetadata(package, title, authors, description, installPath));
        }
        private void LoadNuSpecFile(string filePath)
        {
            var nuSpecFilePath = Directory.GetFiles(System.IO.Path.GetDirectoryName(filePath), "*.nuspec", SearchOption.AllDirectories).FirstOrDefault();

            if (nuSpecFilePath != null && !nuSpecFilePath.StartsWith(NuGetPackagesPath) && !nuSpecFilePath.Contains("node_modules"))
            {
                NuSpecFilePath = nuSpecFilePath;
                using (var stream = File.Open(NuSpecFilePath, FileMode.Open))
                {
                    var reader = new NuspecReader(stream);

                    var packageId = reader.GetId();
                    if (packageId != null && packageId.ToLower() == "$id$")
                    {
                        packageId = Name;
                    }

                    NuGetPackageId    = packageId;
                    NuGetPackageTitle = Name;

                    var metadata = reader.GetMetadata().ToArray();
                    if (metadata.Any(p => p.Key == "title"))
                    {
                        var titlePair = metadata.SingleOrDefault(p => p.Key == "title");
                        NuGetPackageTitle = titlePair.Value;
                    }
                }
            }
            else if (Project.GeneratesPackage() ||
                     Project.HasVersion() ||
                     !string.IsNullOrEmpty(Project.GetProperty("PackageProjectUrl")?.EvaluatedValue) ||
                     !string.IsNullOrEmpty(Project.GetProperty("PackageTags")?.EvaluatedValue))
            {
                NuGetPackageId    = Project.GetProperty("PackageId")?.EvaluatedValue;
                NuGetPackageTitle = Project.GetProperty("PackageTitle")?.EvaluatedValue ?? Name;
            }
        }