Exemplo n.º 1
0
        /// <summary>
        /// Retrieves the PackageReferences from the given XmlDocument. If a package is present multiple time, only the first version will be returned.
        /// </summary>
        /// <param name="document"></param>
        /// <returns>A Dictionary where the key is the id of a package and the value its version.</returns>
        public static PackageIdentity[] GetPackageReferences(this XmlDocument document)
        {
            var references = new List <PackageIdentity>();

            var packageReferences   = document.SelectElements("PackageReference");
            var dotnetCliReferences = document.SelectElements("DotNetCliToolReference");

            foreach (var packageReference in packageReferences.Concat(dotnetCliReferences))
            {
                var packageId = new[] { "Include", "Update", "Remove" }
                .Select(packageReference.GetAttribute)
                .FirstOrDefault(x => !string.IsNullOrEmpty(x));
                var packageVersion = packageReference.GetAttribute("Version");

                if (packageVersion.HasValue())
                {
                    references.Add(CreatePackageIdentity(packageId, packageReference.GetAttribute("Version")));
                }
                else
                {
                    var node = packageReference.SelectNode("Version");
                    if (node != null)
                    {
                        references.Add(CreatePackageIdentity(packageId, node.InnerText));
                    }
                }
            }

            return(references
                   .Trim()
                   .ToArray());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Retrieves the dependency elements from the given XmlDocument. If a package is present multiple time, only the first version will be returned.
 /// </summary>
 /// <param name="document"></param>
 /// <returns>A Dictionary where the key is the id of a package and the value its version.</returns>
 public static PackageIdentity[] GetDependencies(this XmlDocument document)
 => document
 .SelectElements("dependency")
 .Select(e => CreatePackageIdentity(e.GetAttribute("id"), e.GetAttribute("version")))
 .Trim()
 .ToArray();