Last week I wrote about how my client uses LibGit2Sharp to clone all the Git repositories in their universe. Today is a follow up on how we use the NuGet libraries to find out our NuGet dependencies in code.
With the introduction of .net core and the new
project.json project system, NuGet now offer two APIs to interact with NuGet dependencies. One is for
packages.config projects, and the other
Unfortunately, neither API is well documented at the moment. If you're getting started I would recommend Dave Gilck's introduction. For anything more advanced, you'll need to dig through the source code on GitHub.
Below we'll look at the two APIs. Fortunately, it's quite straightforward once you have found the APIs you need.
packages.config projects, we need the v2
PM> Install-Package NuGet.Core
Then this code snippet is all you need to get all the NuGet dependencies in your solution:
var packageConfigFile = @"c:\projects\path-to-solution\packages.config"; var packages = new PackageReferenceFile(packageConfigFile).GetPackageReferences();
In the new .net core world, we need
NuGet.ProjectManagement from the new v3 NuGet libraries:
PM> Install-Package NuGet.ProjectManagement
Things are a little different for
project.json projects. You'll need to run this for every project in your solution:
var projectJsonFile = @"c:\projects\path-to-project\project.json"; var packages = JsonPackageSpecReader.GetPackageSpec("project-name", projectJsonFile).Dependencies;