1. 程式人生 > 其它 >C# 舊模式(packages.config)讀取全部依賴關係,同時獲得頂級依賴程式碼

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 }