示例#1
0
        public static Package FromNupkg(string filePath)
        {
            var pkg = new Package();
            pkg.Path = filePath;

            pkg.PackageHash = SHAHelper.CalculateSHA512Hash(File.ReadAllText(filePath));
            pkg.PackageHashAlgorithm = "SHA512";

            var fInfo = new FileInfo(filePath);
            pkg.Updated = fInfo.LastWriteTimeUtc;
            pkg.Published = fInfo.LastWriteTimeUtc;
            pkg.PackageSize = fInfo.Length;

            using (var zipArchive = new ZipFile(File.OpenRead(filePath)))
            {
                var nuspecEntry = zipArchive.Entries().First(x => x.Name.Contains(".nuspec"));
                var reader = XmlReader.Create(zipArchive.GetInputStream(nuspecEntry));
                XElement element = XElement.Load(reader);
                XNamespace ns = element.GetDefaultNamespace();
                var nsm = new XmlNamespaceManager(reader.NameTable);
                nsm.AddNamespace("ns", ns.NamespaceName);

                pkg.Title = pkg.Id = element.XPathSelectElement("//ns:id", nsm).AsString();

                pkg.Version = new Version(element.XPathSelectElement("//ns:version", nsm).AsString());
                pkg.Author = element.XPathSelectElement("//ns:authors", nsm).AsString();
                pkg.LicenceUrl = element.XPathSelectElement("//ns:licenseUrl", nsm).AsString();
                pkg.ProjectUrl = element.XPathSelectElement("//ns:projectUrl", nsm).AsString();
                pkg.IconUrl = element.XPathSelectElement("//ns:iconUrl", nsm).AsString();
                pkg.RequireLicenseAcceptance = element.XPathSelectElement("//ns:requireLicenseAcceptance", nsm).As<bool>(false);
                pkg.Description = element.XPathSelectElement("//ns:description", nsm).AsString();
                pkg.Summary = element.XPathSelectElement("//ns:summary", nsm).AsString();
                pkg.Copyright = element.XPathSelectElement("//ns:copyright", nsm).AsString();
                pkg.Tags = element.XPathSelectElement("//ns:tags", nsm).AsString();
                pkg.Language = element.XPathSelectElement("//ns:language", nsm).AsString();

                var minClientVersionAttr = element.XPathSelectElement("//ns:metadata", nsm).Attribute("minClientVersion");

                pkg.MinClientVersion = minClientVersionAttr != null ? minClientVersionAttr.Value : null;

                var dependencyNodes = element.Descendants(ns + "dependency");
                if(dependencyNodes.Any()){
                    pkg.Dependencies = dependencyNodes.Select(x => x.Attribute("id").Value + ":" + x.Attribute("version").Value).Aggregate((x, y) => x + "|" + y);
                }
            }
            return pkg;
        }