简体   繁体   English

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

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

I looked into using the nuget.exe command line but the install option only downloads the package rather than install it into my project. 我查看了使用nuget.exe命令行,但安装选项只下载包而不是将其安装到我的项目中。 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. 我已经尝试过在visual studio之外搜索如何运行包管理器,但最接近的是两年半前这篇帖子说它是在路线图上有一个过时的链接。 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? 有谁知道我错过了nuget的某些功能吗? 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? 如果不是,有没有人知道任何替代方法来更新多个解决方案的nuget包而不必每次等待可怕的Visual Studio加载时间?

I ended up writing a quick powershell script to solve my problem. 我最终编写了一个快速的PowerShell脚本来解决我的问题。 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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有Visual Studio的软件包管理器,如何更新nuget软件包? - How to update nuget packages without package manager of visual studio? 如何在未安装 Nuget 或 Visual Studio 的 Web 服务器上安装、更新、注册 Nuget package? - How do I Install, Update, Register a Nuget package on the Web Server that does not have Nuget or Visual Studio installed? 从Visual Studio外部安装NuGet包 - Installing NuGet package from outside of Visual Studio 如何在NuGet发布后更新Visual Studio Team Services上的NuGet包? - How can I update a NuGet package on Visual Studio Team Services after NuGet Publish? 如何在 Visual Studio 外使用包管理器控制台 powershell - How to use Package Manager Console powershell outside visual studio 如何在没有Visual Studio的情况下安装Nuget包? - How to install Nuget Package without Visual Studio? 无法在Visual Studio 2015中安装Nuget软件包管理器 - Not able to install Nuget Package Manager in Visual Studio 2015 无法使用 Visual Studio NuGet 包管理器控制台安装包 - Cannot install packages using Visual Studio NuGet Package Manager Console 如何在正常的PowerShell会话中(不在Visual Studio中)运行Nuget PowerShell cmdlet Install-Package? - How can I run Nuget PowerShell cmdlet Install-Package in normal PowerShell session (not inside Visual Studio)? 在 Visual Studio 2017 中找不到 nuget 包管理器? - Can't find the nuget package manager in visual studio 2017?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM