简体   繁体   中英

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?

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM