private static package UpdatePackage(package package, packageMetadata packageMetadata)
        {
            package.metadata.id = string.IsNullOrEmpty(package.metadata.id)
                ? packageMetadata.id
                : package.metadata.id;

            package.metadata.version = string.IsNullOrEmpty(package.metadata.version)
                ? packageMetadata.version
                : package.metadata.version;

            package.metadata.authors = string.IsNullOrEmpty(package.metadata.authors)
                ? packageMetadata.authors
                : package.metadata.authors;

            package.metadata.owners = string.IsNullOrEmpty(package.metadata.owners)
                ? packageMetadata.owners
                : package.metadata.owners;

            package.metadata.title = string.IsNullOrEmpty(package.metadata.title)
                ? packageMetadata.title
                : package.metadata.title;

            package.metadata.description = string.IsNullOrEmpty(package.metadata.description)
                ? packageMetadata.description
                : package.metadata.description;

            package.metadata.copyright = string.IsNullOrEmpty(package.metadata.copyright)
                ? packageMetadata.copyright
                : package.metadata.copyright;

            package.metadata.dependencies =
                packageMetadata?.dependencies?.Items.Length == 0 ? null : packageMetadata?.dependencies;

            return(package);
        }
Exemplo n.º 2
0
        public bool ProcessPackage(Stream fileStream)
        {
            MemoryStream memoryStream = new MemoryStream();

            fileStream.CopyTo(memoryStream);
            ZipArchive archive = new ZipArchive(memoryStream);

            ZipArchiveEntry file = archive.Entries.FirstOrDefault(entry => entry.FullName.EndsWith(".nuspec"));

            if (file == null)
            {
                throw new InvalidOperationException("No nuspec in nupkg");
            }

            string nuspecContent = new StreamReader(file.Open()).ReadToEnd();

            XmlSerializer ser = new XmlSerializer(typeof(package));

            using (StringReader sr = new StringReader(nuspecContent))
            {
                package nuspec = (package)ser.Deserialize(new NamespaceIgnorantXmlTextReader(sr));

                if (nuspec == null || nuspec.metadata == null)
                {
                    throw new InvalidOperationException("Invalid nuspec");
                }

                packageMetadata metadata = nuspec.metadata;

                if (string.IsNullOrWhiteSpace(metadata.id))
                {
                    throw new InvalidOperationException("Invalid nuspec (missing id)");
                }
                if (string.IsNullOrWhiteSpace(metadata.version))
                {
                    throw new InvalidOperationException("Invalid nuspec (missing version)");
                }

                string title   = metadata.id;
                string version = metadata.version;

                Regex rgxId = new Regex(@"^[A-Za-z0-9\.\~\+_\-]+$");
                if (!rgxId.IsMatch(title))
                {
                    throw new InvalidOperationException("Invalid id specified in nuspec");
                }

                Regex rgxVersion = new Regex(@"^[0-9\.\-]+$");
                if (!rgxVersion.IsMatch(version))
                {
                    throw new InvalidOperationException("Invalid version specified in nuspec");
                }

                long size = memoryStream.Length;
                memoryStream.Position = 0;
                string checksum = FileStore.SaveFile(memoryStream, title + "." + version);

                Package package = new Package(nuspec, checksum, size);

                memoryStream.Close();
                archive.Dispose();
                fileStream.Close();
                return(Create(package).Result);
            }
        }