Exemplo n.º 1
0
        public static void FindReferences(string fileName, IList <PackageReference> references, IList <XDocument> documents)
        {
            var document = XDocument.Load(fileName);

            documents.Add(document);
            foreach (var reference in document.Descendants().Where(x => x.Name.LocalName == "PackageReference"))
            {
                var name             = reference.Attribute("Include").Value;
                var versionAttribute = reference.Attribute("Version");
                var version          = versionAttribute != null ? versionAttribute.Value : reference.Elements().First(x => x.Name.LocalName == "Version").Value;
                var pr = new PackageReference()
                {
                    Name             = name,
                    Version          = version,
                    FileName         = fileName,
                    Document         = document,
                    Reference        = reference,
                    VersionAttribute = versionAttribute
                };
                references.Add(pr);
            }
        }
Exemplo n.º 2
0
        public static void FindReferences(string fileName, IList <PackageReference> references, XmlDocument document)
        {
            XmlNodeList?packageReferences = default;

            if (document.DocumentElement.Attributes["xmlns"] != null)
            {
                string xmlns = document.DocumentElement.Attributes["xmlns"].Value;
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
                namespaceManager.AddNamespace("msbuild", xmlns);
                packageReferences = document.SelectNodes("//msbuild:Project//msbuild:ItemGroup//msbuild:PackageReference", namespaceManager);
            }
            else
            {
                packageReferences = document.SelectNodes("//Project//ItemGroup//PackageReference");
            }

            foreach (XmlNode packageReference in packageReferences)
            {
                var include = default(string);
                var version = default(string);

                var includeAttribute = packageReference.Attributes["Include"];
                if (includeAttribute != null)
                {
                    include = includeAttribute.Value;
                }
                else
                {
                    var includes = packageReference.SelectNodes("descendant::Include");
                    if (includes.Count > 0)
                    {
                        include = includes[0].InnerText;
                    }
                    else
                    {
                        Logger.Log($"Missing package reference Include, file: {fileName}");
                        continue;
                    }
                }

                var versionAttribute = packageReference.Attributes["Version"];
                if (versionAttribute != null)
                {
                    version = versionAttribute.Value;
                }
                else
                {
                    var versions = packageReference.SelectNodes("descendant::Version");
                    if (versions.Count > 0)
                    {
                        version = versions[0].InnerText;
                    }
                    else
                    {
                        Logger.Log($"Missing package reference Version for {include}, file: {fileName}");
                        continue;
                    }
                }

                var pr = new PackageReference()
                {
                    Name             = include,
                    Version          = version,
                    FileName         = fileName,
                    Document         = document,
                    Reference        = packageReference,
                    VersionAttribute = versionAttribute
                };

                references.Add(pr);
            }
        }