How do I update all NuGet packages at once with the dotnet CLI?
I'm trying to update all NuGet packages for a solution in VS Code (using Mac). Is there a way to achieve that in VS code or for a specific project.json file? At the moment I'm going one by one but I would have thought there is either an extension or a feature that does that for you?
For Update all packages in all projects nuget package manager gui extension can do it with one click.
How it works
- Open your project workspace in VSCode
- Open the Command Palette (Ctrl+Shift+P)
- Select > Nuget Package Manager GUI
- Click
Load Package Versions
- Click
Update All Packages
Based on Jon Canning's powershell solution. I fixed a small bug where only the first dependency was being updated and not all the dependencies for the project file.
$regex = 'PackageReference Include="([^"]*)" Version="([^"]*)"'
ForEach ($file in get-childitem . -recurse | where {$_.extension -like "*proj"})
{
$packages = Get-Content $file.FullName |
select-string -pattern $regex -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Groups[1].Value.ToString()}|
sort -Unique
ForEach ($package in $packages)
{
write-host "Update $file package :$package" -foreground 'magenta'
$fullName = $file.FullName
iex "dotnet add $fullName package $package"
}
}
Here's a shell script and a powershell script that will do this
#!/bin/bash
regex='PackageReference Include="([^"]*)" Version="([^"]*)"'
find . -name "*.*proj" | while read proj
do
while read line
do
if [[ $line =~ $regex ]]
then
name="${BASH_REMATCH[1]}"
version="${BASH_REMATCH[2]}"
if [[ $version != *-* ]]
then
dotnet add $proj package $name
fi
fi
done < $proj
done
$regex = [regex] 'PackageReference Include="([^"]*)" Version="([^"]*)"'
ForEach ($file in get-childitem . -recurse | where {$_.extension -like "*proj"})
{
$proj = $file.fullname
$content = Get-Content $proj
$match = $regex.Match($content)
if ($match.Success) {
$name = $match.Groups[1].Value
$version = $match.Groups[2].Value
if ($version -notin "-") {
iex "dotnet add $proj package $name"
}
}
}
Should also mention Paket as a fantastic alternative package manager that supports update:
https://fsprojects.github.io/Paket/index.html
dotnet tool install paket --tool-path .paket
Also have a look at dotnet outdated
:
https://github.com/dotnet-outdated/dotnet-outdated
This seems to work https://nukeeper.com/
dotnet tool install nukeeper --global
nukeeper update <SLN/PROJ>
UPDATE
The default settings on nukeeper
seem slightly odd to me as running nukeeper update
will only update a single package, and only if it is a major version that is more than 3 days old.
To update to the latest non-prerelease version of everything run:
nukeeper update -a 0 -m 1000
And for prerelease:
nukeeper update -a 0 -m 1000 --useprerelease Always
The -m 1000
flag is a synonym for everything, assuming that you have less than 1000 packages in your solution / project.
I wrote this powershell script to keep packages up to date on Githup.
To update all packages of the solution I use first dotnet sln list
.
The for each project I get the list of outdated package with dotnet list package --outdated
, it give the latest version of each outdated packages.
And for each packages I update the project with dotnet add package {package name} --version {new version}
.
Full code:
# Update one project packages
function UpdatePackages {
param (
$project
)
$return = $false
# Get outdated packages
$packageLineList = dotnet list $project package --outdated
foreach($line in $packageLineList) {
Write-Host $line
$match = $line -match '>\s(\S*)\s*\S*\s*\S*\s*(\S*)'
if (!$match) {
# the line doesn't contain a package information, continue
continue
}
# update an outdated package
$added = dotnet add $project package $Matches.1 --version $Matches.2
if ($LASTEXITCODE -ne 0) {
# error while updating the package
Write-Error "dotnet add $project package $Matches.1 --version $Matches.2 exit with code $LASTEXITCODE"
Write-Host $added
break
}
$return = $true
}
return $return
}
# Restore dependencies
dotnet restore
# Get all project list in the solution
$projectList = dotnet sln list
$updated = $false
foreach($path in $projectList) {
if ($path -eq "Project(s)" -or $path -eq "----------") {
# The line doesn't contain a path, continue
continue
}
# Update project dependencies
$projectUpdated = UpdatePackages -project $path
if ($LASTEXITCODE -ne 0) {
#The update fail, exit
exit $LASTEXITCODE
}
$updated = $updated -or $projectUpdated
}
if (!$updated) {
# No packages to update found, exit
Write-Host "nothing to update"
exit 0
}