Created
July 8, 2019 10:45
-
-
Save karanjitsingh/fba637158dc6f4c38573147f5723cfae to your computer and use it in GitHub Desktop.
Nuget package dependency tree.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using NuGet; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static Dictionary<string, IPackage> cache = new Dictionary<string, IPackage>(); | |
static void Main() | |
{ | |
var repo = getRepo(); | |
IQueryable<IPackage> packages = repo.GetPackages(); | |
OutputGraph(repo, packages, 0); | |
Console.WriteLine("done"); | |
Console.ReadLine(); | |
} | |
static IPackageRepository getRepo() | |
{ | |
return new LocalPackageRepository(@"D:\azure-pipelines-coveragepublisher\packages\microsoft.teamfoundation.publishtestresults\16.153.0-preview"); | |
} | |
static void OutputGraph(IPackageRepository repository, IEnumerable<IPackage> packages, int depth) | |
{ | |
var packageSource = "https://www.nuget.org/api/v2/"; | |
//var packageSource = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "NuGet", "Cache"); | |
var repo = PackageRepositoryFactory.Default.CreateRepository(packageSource); | |
foreach (IPackage package in packages) | |
{ | |
Console.WriteLine("{0}{1} v{2}", new string(' ', depth), package.Id, package.Version); | |
Dictionary<string, IPackage> dependentPackages = new Dictionary<string, IPackage>(); | |
foreach (var dependency in package.DependencySets.SelectMany(i => i.Dependencies)) | |
{ | |
if(dependency.Id.StartsWith("System")) | |
{ | |
continue; | |
} | |
var key = dependency.Id + dependency.VersionSpec.MinVersion; | |
if (cache.ContainsKey(key)) | |
{ | |
if (!dependentPackages.ContainsKey(key)) | |
{ | |
dependentPackages.Add(key, cache[key]); | |
} | |
continue; | |
} | |
var entry = repo.FindPackage(dependency.Id, dependency.VersionSpec, true, true); | |
if (!dependentPackages.ContainsKey(key)) | |
{ | |
dependentPackages.Add(key, entry); | |
} | |
cache.Add(key, entry); | |
} | |
OutputGraph(repository, dependentPackages.Values.ToList(), depth + 3); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment