Reputation: 499
I'm looking for a way to get a list of all used NuGet packages in every project in a solution (and specifically the version) using command-line script and not manually in Visual Studio.
Using the Package Manager Console with the command "Get-Package" gives me what I want, but it is unavailable outside of VS.
I'm using is a local NuGet feed. My default package management format is PackageReference.
Any idea would be helpful
Upvotes: 39
Views: 49072
Reputation: 1661
PackageReference as a package management format only works on a per project basis. So you would need to "analyze" each project individually.
Update:
In .NET SDK versions, 2.2.100 and newer, dotnet list package
.
Old version:
From the commandline, there "will" be a way to list all the packages. It's the "dotnet list package" command. I say will, because it's still in preview. You can download the 2.2.100 version from here. Related spec.
The simplest usage example is:
dotnet list YourSln.sln package
If you do not want to use a dotnet.exe preview, you can consider writing your own tool, by reading the assets files for each project, which is what the actual command does. For reference, see code here and here
Upvotes: 32
Reputation: 740
For .NET Core 2.2 and later projects where PowerShell is available, try
(dotnet list package) -match '^.*>' -replace '^ +> ','' | % { $_.split(" ")[0] } | Sort-Object -Unique
and
dotnet list package | grep '>' | sed 's/^ *> //g;s/ \+/ /g' | cut -f 1 -d ' ' | sort -u
in POSIX-like environments.
Upvotes: 4
Reputation: 1365
Run this command Get-Package | Select-Object Id, Version, LicenseUrl, ProjectName
in package manager console in Visual Studio. taken from this answer: https://softwareengineering.stackexchange.com/a/286981
Upvotes: 30
Reputation: 2263
Since packages dependencies have been moved into the package.config file, here is an updated version of the Powershell provided by https://stackoverflow.com/users/2385218/sellotape
Get-Content .\NP.sln | where { $_ -match "Project.+, ""(.+)\\([^\\]+).csproj"", " } | foreach { "$($matches[1])\packages.config" } | % { Get-Content $_ | Find "<package id" } | Sort-Object -Unique
Output be will be like this:
<package id="AutoMapper" version="8.1.1" targetFramework="net47" />
<package id="BouncyCastle" version="1.8.5" targetFramework="net47" />
<package id="CsvHelper" version="12.1.2" targetFramework="net47" />
<package id="DnsClient" version="1.2.0" targetFramework="net47" />
<package id="EntityFramework" version="6.2.0" targetFramework="net462" />
<package id="EntityFramework" version="6.2.0" targetFramework="net47" />
Upvotes: 6
Reputation:
I wrote a C# script for this purpose, but it doesn't directly use the SLN file. It searches instead within a given directory for any packages.config file. Duplicates will be visible within the column 'assemblies' (list of all assemblies that use the specified package). The output will be a CSV file to any given location.
You can just copy / download or adjust the code from here: Source Code
You could also compile the above written source code using CMD / Powershell: Compiling/Executing a C# Source File in Command Prompt
Upvotes: 0
Reputation: 1560
Writing a script that finds the occurrences of the tag PackageReference on each one of the csproj files is a great idea, as proposed by selotape.
This solution won't work if your csproj file has something like this:
<PackageReference Include"SomePackage">
<Version>1.0.42</Version>
</PackageReference>
Even though I recommend that you change the format to state the version in-line (as in the .NET Standard csproj format), maybe you don't have time to do it for all your projects.
Using a C# script, you could get all the items in your csproj files containing PackageReference as their tag-name. Then you could do one of two things:
(Version="*")
, which will allow you to find the version for each PackageReferenceI hope this helps.
Upvotes: 2
Reputation: 8325
I'm sure there are better ways to do this, but here's a round-the-houses PowerShell way when using PackageReferences:
Get-Content .\<solution>.sln | where { $_ -match "Project.+, ""(.+)""," } | foreach { $matches[1] } | % {Get-Content $_ | Find "<PackageReference Include" } | Sort-Object -Unique
Run it in the folder where the .sln lives.
It produces output like this:
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.5.0" /> <PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Http" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.5" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="StyleCop.Analyzers" Version="1.0.2">
I intentionally remove duplicates; you could omit that part if you prefer.
In my case, this matches the output from Get-Package
with the one exception being Microsoft.NETCore.App
, as that is not listed as a dependency anywhere, but is probably rather derived from <TargetFramework>netcoreapp2.1</TargetFramework>
.
Upvotes: 12