[英]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.