src/NuGetGallery.Core/Packaging/PackageMetadata.cs
/// <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>
Gets package metadata from a the provided  instance.
Gets package metadata from a the provided instance .
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)));
                }
            }
            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))));
            }
            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));
                    }
                }
            }
            foreach (var packageType in nuspecReader.GetPackageTypes())
            {
                if (!NuGet.Packaging.PackageIdValidator.IsValidPackageId(packageType.Name))
                {
                    throw new PackagingException(string.Format(
                        CoreStrings.Manifest_InvalidPackageTypeName,
                        packageType.Name));
                }
            }
            return new PackageMetadata(
                nuspecReader.GetMetadata().ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                nuspecReader.GetDependencyGroups(useStrictVersionCheck: strict),
                nuspecReader.GetFrameworkAssemblyGroups(),
                nuspecReader.GetPackageTypes(),
                nuspecReader.GetMinClientVersion(),
                nuspecReader.GetRepositoryMetadata(),
                nuspecReader.GetLicenseMetadata());
        }
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 ) ) ) ; } } 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 ) ) ) ) ; } 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 ) ) ; } } } foreach ( var packageType in nuspecReader . GetPackageTypes ( ) ) { if ( ! NuGet . Packaging . PackageIdValidator . IsValidPackageId ( packageType . Name ) ) { throw new PackagingException ( string . Format ( CoreStrings . Manifest_InvalidPackageTypeName , packageType . Name ) ) ; } } return new PackageMetadata ( nuspecReader . GetMetadata ( ) . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) , nuspecReader . GetDependencyGroups ( useStrictVersionCheck : strict ) , nuspecReader . GetFrameworkAssemblyGroups ( ) , nuspecReader . GetPackageTypes ( ) , nuspecReader . GetMinClientVersion ( ) , nuspecReader . GetRepositoryMetadata ( ) , nuspecReader . GetLicenseMetadata ( ) ) ; }
Gets package metadata from a the provided  instance.
Gets package metadata from a the provided instance .
// Reject invalid metadata element names. Today this only rejects element names that collide with // properties generated downstream. // Reject non-boolean values for boolean metadata. // Reject invalid package types.
We default to use a strict version-check on dependency groups.
When an invalid dependency version range is detected, a  will be thrown.
We default to use a strict version - check on dependency groups . When an invalid dependency version range is detected a will be thrown .
nuspecReader
NuspecReader
The  instance used to read the
The instance used to read the
strict
bool
Whether or not to be strict when reading the . This should be true
Whether or not to be strict when reading the . This should be true