C# 旧模式(packages.config)读取全部依赖关系,同时获得顶级依赖代码

代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using NuGet.ProjectModel;
  5 using NuGet.PackageManagement;
  6 using NuGet.Common;
  7 using NuGet.Packaging.Core;
  8 using NuGet.Protocol;
  9 using NuGet.Protocol.Core.Types;
 10 using NuGet.Versioning;
 11 using static NuGet.Frameworks.FrameworkConstants;
 12 using static NuGet.Protocol.Core.Types.Repository;
 13 using System.Threading;
 14 using System.Threading.Tasks;
 15 using NuGet.Test.Utility;
 16 using Xunit;
 17 //using Test_Tools;
 18 namespace Test_Tools
 19 {
 20 
 21     class Program 
 22     {
 23         static public void Main(string[] args)
 24         {
 25             PackageGraphAnalysisUtilities_GetDependencyInfoForPackageIdentitiesAsync_SucceedsAsync();
 26             // Console.WriteLine();
 27             //Top_level();
 28             Console.ReadKey();  
 29             
 30         }
 31 
 32         public static void Top_level()
 33         {
 34             var packageIdentityA100 = new PackageIdentity("a", NuGetVersion.Parse("2.2.0"));
 35             var packageIdentityB100 = new PackageIdentity("b", NuGetVersion.Parse("13.0.1"));
 36             var packageDependencyInfos = new List<PackageDependencyInfo>();
 37             //制造关于A的依赖图
 38             var packageDependencyInfoA = new PackageDependencyInfo(packageIdentityA100,
 39                 new PackageDependency[] {
 40                     new PackageDependency(packageIdentityB100.Id, VersionRange.Parse("[9.0.1,)")),
 41                 });
 42             var packageDependencyInfoB = new PackageDependencyInfo(packageIdentityB100, Enumerable.Empty<PackageDependency>());
 43 
 44             //增加所有的依赖关系图
 45             packageDependencyInfos.Add(packageDependencyInfoA);
 46             packageDependencyInfos.Add(packageDependencyInfoB);
 47             // Act————》》获取依赖图___>>>用的是package的获取整个依赖图
 48             var packageWithDependants = PackageGraphAnalysisUtilities.GetPackagesWithDependants(packageDependencyInfos);
 49             foreach(var package in packageWithDependants)
 50             {
 51                 Console.WriteLine(package.Identity.Id);
 52                 Console.WriteLine(package.IsTopLevelPackage);
 53             }
 54         }
 55         public static async Task PackageGraphAnalysisUtilities_GetDependencyInfoForPackageIdentitiesAsync_SucceedsAsync()
 56         {
 57             using (var pathContext = new SimpleTestPathContext())
 58             {
 59                 string Path_Resorece =@"D:\VS2017\WorkSpace\Nufix\Test_Tools\Test_Tools\WorkPlace\PackageResource";
 60                 //直接获取nupkg包这样的环境
 61                 var sourceReposistory = Factory.GetCoreV3(Path_Resorece);
 62                 var installedList = new PackageIdentity[]
 63                 {
 64                     new PackageIdentity("LibraryA", NuGetVersion.Parse("1.0.0")),
 65                     //new PackageIdentity("Newtonsoft.Json", NuGetVersion.Parse("13.0.1")),
 66                     new PackageIdentity("Newtonsoft.Json", NuGetVersion.Parse("12.0.1")),
 67                 };
 68                 //工作的流程——》把所有的依赖项都放到一起,制作出来一个环境,然后根据这些环境构建之间的依赖关系
 69                 // Act
 70                 var packageDependencyInfos = await PackageGraphAnalysisUtilities.GetDependencyInfoForPackageIdentitiesAsync(
 71                     packageIdentities: installedList,
 72                     nuGetFramework: CommonFrameworks.NetStandard20,//甚至可以判断出来平台的兼容性!!!!!!!
 73                     dependencyInfoResource: await sourceReposistory.GetResourceAsync<DependencyInfoResource>(),//从Resourse中返回资源
 74                     sourceCacheContext: new SourceCacheContext(),
 75                     includeUnresolved: true,
 76                     logger: NullLogger.Instance,
 77                     cancellationToken: CancellationToken.None
 78                     );
 79                 // Assert
 80                 Console.WriteLine(packageDependencyInfos.Count());
 81                 var Tops_List = new List<PackageDependencyInfo>();
 82                 foreach (var package in packageDependencyInfos)
 83                 {
 84                     Console.WriteLine(package.Id);
 85                     Console.WriteLine(package.Dependencies.Count());
 86                     var packageIdentity = new PackageIdentity(package.Id, package.Version);
 87                     var packageDependencyInfo = new PackageDependencyInfo(packageIdentity, Enumerable.Empty<PackageDependency>());
 88                     if (package.Dependencies.Count() > 0)
 89                     {
 90                         var PackageDependency_ = package.Dependencies;
 91                         packageDependencyInfo = new PackageDependencyInfo(packageIdentity, PackageDependency_);
 92 
 93                     }
 94                     Tops_List.Add(packageDependencyInfo);
 95                 }
 96                 Console.WriteLine("RTRTRTRTR");
 97                 //通过依赖图构建全局关系
 98                 var packageWithDependants = PackageGraphAnalysisUtilities.GetPackagesWithDependants(Tops_List);
 99 
100                 foreach (var package in packageWithDependants)
101                 {
102                     Console.WriteLine(package.Identity.Id);
103                     Console.WriteLine(package.IsTopLevelPackage);
104                 }
105             }
106 
107         }
108 
109     }
110 }

 

posted @ 2022-03-26 21:28  博二爷  阅读(668)  评论(0编辑  收藏  举报