private static TransitivePackageReferenceContextInfo Create(IPackageReferenceContextInfo packageReference)
        {
            if (packageReference == null)
            {
                throw new ArgumentNullException(nameof(packageReference));
            }

            var tranPkgRefCtxInfo = new TransitivePackageReferenceContextInfo(packageReference.Identity, packageReference.Framework)
            {
                IsAutoReferenced        = packageReference.IsAutoReferenced,
                AllowedVersions         = packageReference.AllowedVersions,
                IsUserInstalled         = packageReference.IsUserInstalled,
                IsDevelopmentDependency = packageReference.IsDevelopmentDependency,
            };

            return(tranPkgRefCtxInfo);
        }
Пример #2
0
        protected override ITransitivePackageReferenceContextInfo?DeserializeCore(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            PackageIdentity?identity                = null;
            NuGetFramework? framework               = null;
            bool            isUserInstalled         = false;
            bool            isAutoReferenced        = false;
            bool            isDevelopmentDependency = false;
            string?         allowedVersions         = null;

            var transitiveOrigins = new List <IPackageReferenceContextInfo>();

            int propertyCount = reader.ReadMapHeader();

            for (int propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++)
            {
                switch (reader.ReadString())
                {
                case IdentityPropertyName:
                    identity = PackageIdentityFormatter.Instance.Deserialize(ref reader, options);
                    break;

                case FrameworkPropertyName:
                    framework = NuGetFrameworkFormatter.Instance.Deserialize(ref reader, options);
                    break;

                case IsUserInstalledPropertyName:
                    isUserInstalled = reader.ReadBoolean();
                    break;

                case IsAutoReferencedPropertyName:
                    isAutoReferenced = reader.ReadBoolean();
                    break;

                case IsDevelopmentDependencyPropertyName:
                    isDevelopmentDependency = reader.ReadBoolean();
                    break;

                case AllowedVersionsPropertyName:
                    if (!reader.TryReadNil())     // Advances beyond the current value if the current value is nil and returns true; otherwise leaves the reader's position unchanged and returns false.
                    {
                        allowedVersions = reader.ReadString();
                    }
                    break;

                case TransitiveOriginsPropertyName:
                    var elems = reader.ReadArrayHeader();
                    for (int i = 0; i < elems; i++)
                    {
                        var result = IPackageReferenceContextInfoFormatter.Instance.Deserialize(ref reader, options);
                        if (result != null)
                        {
                            transitiveOrigins.Add(result);
                        }
                    }
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            Assumes.NotNull(identity);

            var packageReferenceContextInfo = new TransitivePackageReferenceContextInfo(identity, framework)
            {
                IsUserInstalled         = isUserInstalled,
                IsAutoReferenced        = isAutoReferenced,
                IsDevelopmentDependency = isDevelopmentDependency
            };

            if (!string.IsNullOrWhiteSpace(allowedVersions) && VersionRange.TryParse(allowedVersions, out VersionRange versionRange))
            {
                packageReferenceContextInfo.AllowedVersions = versionRange;
            }
            packageReferenceContextInfo.TransitiveOrigins = transitiveOrigins;

            return(packageReferenceContextInfo);
        }