C# 调用NuGet 实现获取包的顶级依赖
代码:
1 using System; 2 using static NuGet.Frameworks.FrameworkConstants; 3 using static NuGet.Protocol.Core.Types.Repository; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Threading; 7 using System.Threading.Tasks; 8 using NuGet.Common; 9 using NuGet.Packaging.Core; 10 using NuGet.Protocol; 11 using NuGet.Protocol.Core.Types; 12 //using NuGet.Test.Utility; 13 using NuGet.Versioning; 14 using NuGet.PackageManagement; 15 namespace NuGet_Remote_develop 16 { 17 internal class Program 18 { 19 static void Main(string[] args) 20 { 21 Console.WriteLine("Hello World!"); 22 Console.WriteLine("!!!!"); 23 // Set up 24 var packageIdentityA100 = new PackageIdentity("a", NuGetVersion.Parse("1.0.0")); 25 Console.WriteLine("%%%%%"); 26 var packageIdentityB100 = new PackageIdentity("b", NuGetVersion.Parse("1.0.0")); 27 var packageIdentityC100 = new PackageIdentity("c", NuGetVersion.Parse("1.0.0")); 28 var packageIdentityC110 = new PackageIdentity("c", NuGetVersion.Parse("1.1.0")); 29 var packageIdentityD100 = new PackageIdentity("d", NuGetVersion.Parse("1.0.0")); 30 31 32 var packageDependencyInfos = new List<PackageDependencyInfo>(); 33 //制造关于A的依赖图 34 var packageDependencyInfoA = new PackageDependencyInfo(packageIdentityA100, 35 new PackageDependency[] { 36 new PackageDependency(packageIdentityB100.Id, VersionRange.Parse(packageIdentityB100.Version.OriginalVersion)), 37 new PackageDependency(packageIdentityC100.Id, VersionRange.Parse(packageIdentityC100.Version.OriginalVersion)), 38 }); 39 var packageDependencyInfoB = new PackageDependencyInfo(packageIdentityB100, 40 new PackageDependency[] { 41 new PackageDependency(packageIdentityD100.Id, VersionRange.Parse(packageIdentityD100.Version.OriginalVersion)), 42 }); 43 var packageDependencyInfoC = new PackageDependencyInfo(packageIdentityC110, Enumerable.Empty<PackageDependency>()); 44 var packageDependencyInfoD = new PackageDependencyInfo(packageIdentityD100, Enumerable.Empty<PackageDependency>()); 45 46 //增加所有的依赖关系图 47 packageDependencyInfos.Add(packageDependencyInfoA); 48 packageDependencyInfos.Add(packageDependencyInfoB); 49 packageDependencyInfos.Add(packageDependencyInfoC); 50 packageDependencyInfos.Add(packageDependencyInfoD); 51 52 // Act————》》获取依赖图 53 var packageWithDependants = PackageGraphAnalysisUtilities.GetPackagesWithDependants(packageDependencyInfos); 54 Console.WriteLine(packageWithDependants); 55 Console.WriteLine("*****"); 56 foreach (var package in packageWithDependants) 57 { 58 switch (package.Identity.Id) 59 { 60 case "a": 61 { 62 //看他是不是没有其他的依赖项 63 Console.WriteLine(package.DependantPackages.Count); 64 Console.WriteLine(package.IsTopLevelPackage); 65 break; 66 } 67 case "b": 68 { 69 //看他是不是只有一个依赖项,而且这个依赖项是A。并且他不是直接依赖 70 Console.WriteLine( package.DependantPackages.Count); 71 Console.WriteLine(package.DependantPackages.Single().Id); 72 Console.WriteLine(package.IsTopLevelPackage); 73 break; 74 } 75 case "c": 76 { 77 Console.WriteLine(package.DependantPackages.Count); 78 Console.WriteLine(package.IsTopLevelPackage); 79 break; 80 } 81 case "d": 82 { 83 Console.WriteLine( package.DependantPackages.Count); 84 Console.WriteLine( package.DependantPackages.Single().Id); 85 Console.WriteLine(package.IsTopLevelPackage); 86 break; 87 } 88 default: 89 { 90 Console.WriteLine($"Unexpected package {package.Identity}"); 91 break; 92 } 93 } 94 } 95 Console.ReadKey(); 96 } 97 } 98 }