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

标签 visual-studio visual-studio-2015 nuget

我正在使用通过 nuget 包共享核心代码的微服务架构。除了我需要更新我的核心 nuget 包之一,然后还将 10 多个解决方案更新到最新解决方案的罕见情况外,这一切都很好。由于 Visual Studio 需要永远加载,我不想为了从 nuget 包管理器运行 Update-Package 命令而进入并打开每个。

我研究过使用 nuget.exe 命令行,但安装选项只下载包,而不是将它安装到我的项目中。我试过寻找如何在 Visual Studio 外运行包管理器,但最接近的是 this post两年半前说它在路线图上,但链接已过时。我也不熟悉如何在此站点上请求更新。

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

最佳答案

我最终编写了一个快速的 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)
}

关于visual-studio - 如何在 Visual Studio 之外使用 nuget 包管理器从命令行安装/更新包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40940941/

相关文章:

c++ - 开始使用 Crypto++

visual-studio - 使用 MS VS15 2017 编译 OpenCV 3.4 和 Cuda 9.2

visual-studio - Visual Studio 2015 中缺少 SSDT(SQL Server Data Tools)

nuget - 如何使用 NuGet 为基于文件的包源设置相对路径?

visual-studio-2012 - 无法解析远程名称 'www.nuget.org'

c++ - <chrono> 错误太多 (std::chrono::timepoint) (VS2015)

c# - 警告尝试构建 Service Fabric AspNetCore2 项目

c++ - 我可以使用 Visual Studio 2015 创建 Windows Phone 8 应用程序吗?

Xamarin iOS : How to add properities into info. plist 源部分

msbuild - Nuget.targets : error could not find a part of the path