如何在Visual Studio外部使用nuget包管理器从命令行安装/更新包?

Pau*_*lla 6 visual-studio nuget visual-studio-2015

我正在使用微服务架构,核心代码通过nuget包共享.这一切都很好,除了我需要更新我的核心nuget软件包然后还更新10+解决方案的罕见场合.随着visual studio永远加载,我不想进入并打开每个只是为了从nuget包管理器运行Update-Package命令.

我查看了使用nuget.exe命令行,但安装选项只下载包而不是将其安装到我的项目中.我已经尝试过在visual studio之外搜索如何运行包管理器,但最接近的是两年半前这篇文章说这是在路线图上有一个过时的链接.我也不熟悉在本网站上要求更新的方式.

有谁知道我错过了nuget的某些功能吗?如果不是,有没有人知道任何替代方法来更新多个解决方案的nuget包而不必每次等待可怕的Visual Studio加载时间?

Pau*_*lla 7

我最终编写了一个快速的PowerShell脚本来解决我的问题.这是其他任何人感兴趣的:

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)
}
Run Code Online (Sandbox Code Playgroud)