コード例 #1
0
 private bool RemoveInvalidVersions(XElement x)
 {
     if (NuGetVersion.TryParse(x.AttributeIgnoreCase("Version")?.Value, out var version))
     {
         return(true);
     }
     Console.WriteLine(MsBuild.Log(Path, MsBuild.Category.Warning, ((IXmlLineInfo)x).LineNumber, ((IXmlLineInfo)x).LinePosition,
                                   version != null
             ? $"{version} is not a valid NuGetVersion and is being ignored. See 'https://docs.microsoft.com/en-us/nuget/concepts/package-versioning' for more info on valid versions"
             : "Unable to find a version for this package. It will be ignored."));
     return(false);
 }
コード例 #2
0
        /// <summary>
        ///     Loads NuGet packages in use form packages.config or PackageReferences in the project file
        /// </summary>
        /// <returns></returns>
        public Dictionary <string, NuGetPackage> LoadPackages(string targetFramework           = "",
                                                              bool checkTransitiveDependencies = true, bool checkReferencedProjects = true)
        {
            var pkgs = new Dictionary <string, NuGetPackage>();

            if (System.IO.Path.GetFileName(Path) == "packages.config")
            {
                pkgs = XElement.Load(Path, LoadOptions.SetLineInfo).DescendantsAndSelf("package")
                       .Where(x => RemoveInvalidVersions(x))
                       .Select(x => new NuGetPackage
                {
                    Id         = x.AttributeIgnoreCase("id").Value, Version = x.AttributeIgnoreCase("version").Value,
                    LineNumber = ((IXmlLineInfo)x).LineNumber, LinePosition = ((IXmlLineInfo)x).LinePosition
                }).ToDictionary(p => p.Id);
            }
            else
            {
                pkgs = XElement.Load(Path, LoadOptions.SetLineInfo).DescendantsAndSelf("PackageReference")
                       .Where(x => RemoveInvalidVersions(x))
                       .Select(
                    x => new NuGetPackage
                {
                    Id           = x.AttributeIgnoreCase("Include").Value,
                    Version      = x.AttributeIgnoreCase("Version").Value,
                    LineNumber   = ((IXmlLineInfo)x).LineNumber,
                    LinePosition = ((IXmlLineInfo)x).LinePosition
                }).ToDictionary(p => p.Id);
            }
            if (!PackagesConfig)
            {
                if (pkgs.Count > 0)
                {
                    Dictionary <string, NuGetPackage[]> projectectReferencePackages;
                    var resolvedPackages = dotnetListPackages(Path, targetFramework, out projectectReferencePackages);

                    if (checkTransitiveDependencies)
                    {
                        foreach (var pkg in pkgs.Where(
                                     package => resolvedPackages.ContainsKey(package.Key)))
                        {
                            resolvedPackages[pkg.Key].LineNumber   = pkg.Value.LineNumber;
                            resolvedPackages[pkg.Key].LinePosition = pkg.Value.LinePosition;
                        }

                        pkgs = resolvedPackages;
                    }
                    else
                    {
                        foreach (var pkg in pkgs.Where(p => resolvedPackages.ContainsKey(p.Key)))
                        {
                            pkgs[pkg.Key].Version = resolvedPackages[pkg.Key].Version;
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"Skipping dotnet list package. No Packages found for {Path}.");
                }
            }
            else if (checkTransitiveDependencies)
            {
                Console.WriteLine(MsBuild.Log(Path, MsBuild.Category.Warning,
                                              "Transitive dependency checking skipped. 'dotnet list package --include-transitive' only supports SDK style NuGet Package References"));
            }

            return(pkgs);
        }