Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the nuget Package Manager outside Visual Studio to install/update a package from the commandline?

I'm working with a microservice architecture with core code that is shared via a nuget package. This all works pretty good except for the rare-ish occasions that I need to update one of my core nuget packages and then also update 10+ solutions to the latest one. With visual studio taking forever to load up I don't want to have to go in and open each one just to run the Update-Package command from the nuget Package Manager.

I looked into using the nuget.exe command line but the install option only downloads the package rather than install it into my project. I've tried searching for how to run the package manager outside visual studio, but the closest thing was this post two and a half years ago saying it's on the roadmap with an out-of-date link. I'm also unfamiliar with how asking for updates work on this site.

Does anyone know if there's some feature of nuget that I just missed? If not does anyone know of any alternative ways to update a nuget package for multiple solutions without having to wait through the horrendous Visual Studio load time every time?

like image 472
Paul Milla Avatar asked Dec 02 '16 21:12

Paul Milla


People also ask

How do I install NuGet packages in Visual Studio?

Installing NuGet Packages In Visual Studio 1 We can install NuGet packages in any .NET project that supports the same target framework as that of the created project. 2 Open NuGet Package Manager. Now, there are two options to open the NuGet Package Manager window. ... 3 Search and install your required NuGet. ...

What is the default project dropdown for the NuGet package manager console?

NuGet Package Manager Console Default Project dropdown is empty 36 Use NuGet PowerShell commandlets from outside Visual Studio 7 Visual Studio add custom command to run in Package Manager Console?

What is a NuGet package?

NuGet packages contain reusable code that other developers make available to you for use in your projects. See What is NuGet? for background. Packages are installed into a Visual Studio project using the NuGet Package Manager or the Package Manager Console.

How do I open the NuGet console in Visual Studio?

To open the console in Visual Studio, go to the main menu and select Tools > NuGet Package Manager > Package Manager Console command. By default, console commands operate against a specific package source and project as set in the control at the top of the window. To install a package, use Install-Package command.


Video Answer


1 Answers

I ended up writing a quick powershell script to solve my problem. Here it is incase anyone else is interested:

param(
    [Parameter(Mandatory=$true)]$package,
    $solution = (Get-Item *.sln),
    $nuget = "nuget.exe"
)

& $nuget update "$solution" -Id "$package"

$sln = Get-Content $solution
[regex]$regex = 'Project\("{.*?}"\) = ".*?", "(.*?\.csproj)", "{.*?}"'
$csprojs = $sln | Select-String $regex -AllMatches | 
                  % {$_.Matches} |
                  % {$_.Groups[1].Value}

Foreach ($csproj_path in $csprojs) {
    $csproj_path = Get-Item $csproj_path
    Write-Host "Updating csproj: $csproj_path"

    [xml]$csproj = Get-Content $csproj_path
    Push-Location (Get-Item $csproj_path).Directory
    $reference = $csproj.Project.ItemGroup.Reference | ? {$_.Include -like "$package,*"}

    $old_include = [string]$reference.Include
    $old_hintpath = [string]$reference.HintPath
    $old_version = $old_include | Select-String 'Version=([\d\.]+?),' |
                                  % {$_.Matches} |
                                  % {$_.Groups[1].Value}

    $all_packages = Get-ChildItem $old_hintpath.Substring(0, $old_hintpath.IndexOf($package))
    $new_package_dir = $all_packages | ? {$_.Name -like "$package.[0-9.]*"} |
                                       ? {$_.Name -notlike "$package.$old_version"} |
                                       Select -First 1
    $new_version = $new_package_dir | Select-String "$package.([\d\.]+)" |
                                  % {$_.Matches} |
                                  % {$_.Groups[1].Value}

    $dll = Get-ChildItem -Path $new_package_dir.FullName -Recurse -Include *.dll | Select -First 1
    $reference.HintPath = [string](Get-Item $dll.FullName | Resolve-Path -Relative)
    $reference.Include = $reference.Include.Replace("Version=$old_version","Version=$new_version")

    Pop-Location
    $csproj.Save($csproj_path)
}
like image 112
Paul Milla Avatar answered Sep 30 '22 13:09

Paul Milla