Ejemplo n.º 1
0
        private Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, INupkg nugetPackage, User user)
        {
            var package = packageRegistration.Packages.SingleOrDefault(pv => pv.Version == nugetPackage.Metadata.Version.ToString());

            if (package != null)
            {
                throw new EntityException(
                          "A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
            }

            var now = DateTime.UtcNow;
            var packageFileStream = nugetPackage.GetStream();

            package = new Package
            {
                // Version must always be the exact string from the nuspec, which ToString will return to us.
                // However, we do also store a normalized copy for looking up later.
                Version           = nugetPackage.Metadata.Version.ToString(),
                NormalizedVersion = nugetPackage.Metadata.Version.ToNormalizedString(),

                Description               = nugetPackage.Metadata.Description,
                ReleaseNotes              = nugetPackage.Metadata.ReleaseNotes,
                HashAlgorithm             = Constants.Sha512HashAlgorithmId,
                Hash                      = Crypto.GenerateHash(packageFileStream.ReadAllBytes()),
                PackageFileSize           = packageFileStream.Length,
                Created                   = now,
                Language                  = nugetPackage.Metadata.Language,
                LastUpdated               = now,
                Published                 = now,
                Copyright                 = nugetPackage.Metadata.Copyright,
                FlattenedAuthors          = nugetPackage.Metadata.Authors.Flatten(),
                IsPrerelease              = !nugetPackage.Metadata.IsReleaseVersion(),
                Listed                    = true,
                PackageRegistration       = packageRegistration,
                RequiresLicenseAcceptance = nugetPackage.Metadata.RequireLicenseAcceptance,
                Summary                   = nugetPackage.Metadata.Summary,
                Tags                      = PackageHelper.ParseTags(nugetPackage.Metadata.Tags),
                Title                     = nugetPackage.Metadata.Title,
                User                      = user,
            };

            package.IconUrl          = nugetPackage.Metadata.IconUrl.ToEncodedUrlStringOrNull();
            package.LicenseUrl       = nugetPackage.Metadata.LicenseUrl.ToEncodedUrlStringOrNull();
            package.ProjectUrl       = nugetPackage.Metadata.ProjectUrl.ToEncodedUrlStringOrNull();
            package.MinClientVersion = nugetPackage.Metadata.MinClientVersion.ToStringOrNull();

#pragma warning disable 618 // TODO: remove Package.Authors completely once prodution services definitely no longer need it
            foreach (var author in nugetPackage.Metadata.Authors)
            {
                package.Authors.Add(new PackageAuthor {
                    Name = author
                });
            }
#pragma warning restore 618

            var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray();
            if (!supportedFrameworks.AnySafe(sf => sf == null))
            {
                foreach (var supportedFramework in supportedFrameworks)
                {
                    package.SupportedFrameworks.Add(new PackageFramework {
                        TargetFramework = supportedFramework
                    });
                }
            }

            foreach (var dependencySet in nugetPackage.Metadata.DependencySets)
            {
                if (dependencySet.Dependencies.Count == 0)
                {
                    package.Dependencies.Add(
                        new PackageDependency
                    {
                        Id              = null,
                        VersionSpec     = null,
                        TargetFramework = dependencySet.TargetFramework.ToShortNameOrNull()
                    });
                }
                else
                {
                    foreach (var dependency in dependencySet.Dependencies.Select(d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework }))
                    {
                        package.Dependencies.Add(
                            new PackageDependency
                        {
                            Id              = dependency.Id,
                            VersionSpec     = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(),
                            TargetFramework = dependency.TargetFramework.ToShortNameOrNull()
                        });
                    }
                }
            }

            package.FlattenedDependencies = package.Dependencies.Flatten();

            return(package);
        }
Ejemplo n.º 2
0
        private Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, INupkg nugetPackage)
        {
            var package = packageRegistration.Packages.SingleOrDefault(pv => pv.Version == nugetPackage.Metadata.Version.ToString());

            if (package != null)
            {
                throw new EntityException(
                          "A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
            }

            var now = DateTime.UtcNow;
            var packageFileStream = nugetPackage.GetStream();

            package = new Package
            {
                Version      = nugetPackage.Metadata.Version.ToString(),
                Description  = nugetPackage.Metadata.Description,
                ReleaseNotes = nugetPackage.Metadata.ReleaseNotes,
                RequiresLicenseAcceptance = nugetPackage.Metadata.RequireLicenseAcceptance,
                HashAlgorithm             = Constants.Sha512HashAlgorithmId,
                Hash                = Crypto.GenerateHash(packageFileStream.ReadAllBytes()),
                PackageFileSize     = packageFileStream.Length,
                Created             = now,
                Language            = nugetPackage.Metadata.Language,
                LastUpdated         = now,
                Published           = now,
                Copyright           = nugetPackage.Metadata.Copyright,
                IsPrerelease        = !nugetPackage.Metadata.IsReleaseVersion(),
                Listed              = true,
                PackageRegistration = packageRegistration,
                Summary             = nugetPackage.Metadata.Summary,
                Tags                = nugetPackage.Metadata.Tags,
                Title               = nugetPackage.Metadata.Title,
            };

            package.IconUrl          = nugetPackage.Metadata.IconUrl.ToStringOrNull();
            package.LicenseUrl       = nugetPackage.Metadata.LicenseUrl.ToStringOrNull();
            package.ProjectUrl       = nugetPackage.Metadata.ProjectUrl.ToStringOrNull();
            package.MinClientVersion = nugetPackage.Metadata.MinClientVersion.ToStringOrNull();

            foreach (var author in nugetPackage.Metadata.Authors)
            {
                package.Authors.Add(new PackageAuthor {
                    Name = author
                });
            }

            var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray();

            if (!supportedFrameworks.AnySafe(sf => sf == null))
            {
                foreach (var supportedFramework in supportedFrameworks)
                {
                    package.SupportedFrameworks.Add(new PackageFramework {
                        TargetFramework = supportedFramework
                    });
                }
            }

            foreach (var dependencySet in nugetPackage.Metadata.DependencySets)
            {
                if (dependencySet.Dependencies.Count == 0)
                {
                    package.Dependencies.Add(
                        new PackageDependency
                    {
                        Id              = null,
                        VersionSpec     = null,
                        TargetFramework = dependencySet.TargetFramework.ToShortNameOrNull()
                    });
                }
                else
                {
                    foreach (var dependency in dependencySet.Dependencies.Select(d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework }))
                    {
                        package.Dependencies.Add(
                            new PackageDependency
                        {
                            Id              = dependency.Id,
                            VersionSpec     = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(),
                            TargetFramework = dependency.TargetFramework.ToShortNameOrNull()
                        });
                    }
                }
            }

            package.FlattenedAuthors      = package.Authors.Flatten();
            package.FlattenedDependencies = package.Dependencies.Flatten();

            return(package);
        }