/// <summary> /// Loads NuGet packages in use form packages.config or PackageReferences in the project file /// </summary> /// <returns></returns> public static NuGetPackage[] LoadPackages(string packageSource, string framework) { IEnumerable <NuGetPackage> pkgs; if (Path.GetFileName(packageSource) == "packages.config") { pkgs = XElement.Load(packageSource, LoadOptions.SetLineInfo).DescendantsAndSelf("package").Select(x => new NuGetPackage { Id = x.Attribute("id").Value, Version = x.Attribute("version").Value, LineNumber = ((IXmlLineInfo)x).LineNumber, LinePosition = ((IXmlLineInfo)x).LinePosition }); } else { pkgs = XElement.Load(packageSource, LoadOptions.SetLineInfo).DescendantsAndSelf("PackageReference") .Select( x => new NuGetPackage { Id = x.Attribute("Include").Value, Version = x.Attribute("Version").Value, LineNumber = ((IXmlLineInfo)x).LineNumber, LinePosition = ((IXmlLineInfo)x).LinePosition }); } return(NuGetClient.GetAllPackageDependencies(pkgs.Where(p => p.Id != "NuGetDefense").ToList(), framework) .Result.ToArray()); }
/// <summary> /// Loads NuGet packages in use form packages.config or PackageReferences in the project file /// </summary> /// <returns></returns> public static NuGetPackage[] LoadPackages(string packageSource, string framework) { IEnumerable <NuGetPackage> pkgs; if (Path.GetFileName(packageSource) == "packages.config") { pkgs = XElement.Load(packageSource, LoadOptions.SetLineInfo).DescendantsAndSelf("package").Select(x => new NuGetPackage { Id = x.Attribute("id").Value, Version = x.Attribute("version").Value, LineNumber = ((IXmlLineInfo)x).LineNumber, LinePosition = ((IXmlLineInfo)x).LinePosition }); } else { pkgs = XElement.Load(packageSource, LoadOptions.SetLineInfo).DescendantsAndSelf("PackageReference") .Select( x => new NuGetPackage { Id = x.Attribute("Include").Value, Version = x.Attribute("Version").Value, LineNumber = ((IXmlLineInfo)x).LineNumber, LinePosition = ((IXmlLineInfo)x).LinePosition }); } if (_settings.ErrorSettings.IgnoredPackages.Length > 0) { pkgs = IgnorePackages(pkgs); } try { pkgs = NuGetClient .GetAllPackageDependencies(pkgs.Where(p => p.Id != "NuGetDefense").ToList(), framework) .Result.ToArray(); } catch (Exception e) { Console.WriteLine( $"{_nuGetFile} : Warning : Error getting package dependencies: {e}"); } return(pkgs as NuGetPackage[] ?? pkgs.ToArray()); }