Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View NuGet package dependency hierarchy

Tags:

c#

nuget

Is there a way, either textual or graphical, to view the hierarchy of dependencies between NuGet packages?

like image 637
Neil Barnwell Avatar asked Jul 11 '11 17:07

Neil Barnwell


People also ask

How do I get NuGet dependencies?

To add a dependency, either add a package through the Package Manager UI or Console in Visual Studio, or modify packages. config and then run either install or restore . Open a command line and switch to the directory that contains your project file.

Do NuGet packages include dependencies?

Any time a package is installed or reinstalled, which includes being installed as part of a restore process, NuGet also installs any additional packages on which that first package depends. Those immediate dependencies might then also have dependencies on their own, which can continue to an arbitrary depth.

How do I show dependencies in Visual Studio?

On the Project menu, choose Project Dependencies. The Project Dependencies dialog box opens. On the Dependencies tab, select a project from the Project drop-down menu. In the Depends on field, select the check box of any other project that must build before this project does.


1 Answers

Like @neil-barnwell solution, but works with NuGet.Core 2.7+

Install-Package NuGet.Core 

Here is the code

using System; using System.Linq; using System.Runtime.Versioning; using System.IO; using NuGet;  public class Program {     public static void Main(string[] args)     {         var frameworkName = new FrameworkName(".NETFramework, Version=4.0");          // var packageSource = "https://www.nuget.org/api/v2/";         var packageSource = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "NuGet", "Cache");          var repository = PackageRepositoryFactory.Default.CreateRepository(packageSource);         const bool prerelease = false;          var packages = repository.GetPackages()             .Where(p => prerelease ? p.IsAbsoluteLatestVersion : p.IsLatestVersion)             .Where(p => VersionUtility.IsCompatible(frameworkName, p.GetSupportedFrameworks()));          foreach (IPackage package in packages)         {             GetValue(repository, frameworkName, package, prerelease, 0);         }          Console.WriteLine();         Console.WriteLine("Press Enter...");         Console.ReadLine();     }      private static void GetValue(IPackageRepository repository, FrameworkName frameworkName, IPackage package, bool prerelease, int level)     {          Console.WriteLine("{0}{1}", new string(' ', level * 3), package);         foreach (PackageDependency dependency in package.GetCompatiblePackageDependencies(frameworkName))         {             IPackage subPackage = repository.ResolveDependency(dependency, prerelease, true);             GetValue(repository, frameworkName, subPackage, prerelease, level + 1);         }     } } 
like image 62
Gian Marco Avatar answered Oct 21 '22 04:10

Gian Marco