簡體   English   中英

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

[英]How can I use the nuget Package Manager outside Visual Studio to install/update a package from the commandline?

我正在使用微服務架構,核心代碼通過nuget包共享。 這一切都很好,除了我需要更新我的核心nuget軟件包然后還更新10+解決方案的罕見場合。 隨着visual studio永遠加載,我不想進入並打開每個只是為了從nuget包管理器運行Update-Package命令。

我查看了使用nuget.exe命令行,但安裝選項只下載包而不是將其安裝到我的項目中。 我已經嘗試過在visual studio之外搜索如何運行包管理器,但最接近的是兩年半前這篇帖子說它是在路線圖上有一個過時的鏈接。 我也不熟悉在本網站上要求更新的方式。

有誰知道我錯過了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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM