簡體   English   中英

在 PowerShell 腳本中,如何還原包以解決錯誤:此項目引用此計算機上缺少的 NuGet 包

[英]In PowerShell script, how can I restore packages to resolve error: This project references NuGet package(s) that are missing on this computer

我的任務是編寫一個 PowerShell 腳本來下載給定分支的最新源代碼,重建它並部署它。 我編寫的腳本能夠下載項目,並且在大多數情況下能夠重建它們。 但是在一個網絡項目中,我收到了這個錯誤:

error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.Net.Compilers.2.0.1\build\Microsoft.Net.Compilers.props.

我已經研究過 PowerShell 是否具有類似於 VS 命令提示符中可用的更新包命令,但無法找到等效命令。

我知道有一個 Packages cmdlet,但據我所知,它們用於更新特定的包……我需要的是能夠下載/更新項目中引用的所有包。

一些可能感興趣的點......

當我獲得新的空文件夾的最新解決方案時,包文件夾中唯一的東西是 Modernizr.2.6.2。 無論我是在 VS 還是在我的 PowerShell 腳本中獲得最新版本,這都是一樣的。

如果我在 VS 2017 中打開解決方案,我可以毫無問題地重建解決方案。 它下載/安裝十多個其他包……其中一個是錯誤消息中提到的 Microsoft.Net.Compilers.props 包。

但是,如果我刪除所有內容並重新下載源代碼,然后通過我的 PowerShell 腳本調用 MSBuild 來重建解決方案,則會出現上述錯誤。 它似乎永遠不會下載/安裝丟失的軟件包。

任何人都知道如何在我的 PowerShell 腳本中使用 MSBuild 來重建項目並讓它自動更新/安裝它需要的任何包?

謝謝

我能夠在此頁面上找到解決我的問題的方法: 使用 PowerShell 快速恢復 NuGet 包

在該頁面上有一個腳本,它使用 Nuget.exe 下載基於 packages.config 的包:

#This will be the root folder of all your solutions - we will search all children of 
this folder
$SOLUTIONROOT = "C:\Projects\"
#This is where your NuGet.exe is located
$NUGETLOCATION = "C:\Projects\NuGet\NuGet.exe"

Function RestoreAllPackages ($BaseDirectory)
    {
        Write-Host "Starting Package Restore - This may take a few minutes ..."
        $PACKAGECONFIGS = Get-ChildItem -Recurse -Force $BaseDirectory -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -eq     "packages.config")}
        ForEach($PACKAGECONFIG in $PACKAGECONFIGS)
            {
                Write-Host $PACKAGECONFIG.FullName
                $NugetRestore = $NUGETLOCATION + " install " + " '" + $PACKAGECONFIG.FullName + "' -OutputDirectory '" +     $PACKAGECONFIG.Directory.parent.FullName + "\packages'"
                Write-Host $NugetRestore
                Invoke-Expression $NugetRestore
            }
    }

RestoreAllPackages $SOLUTIONROOT
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

我修改了這個函數並將其添加到我的 PS 腳本中,並首先調用它來下載所有包,這就完成了工作!

您需要調用 MSBuild 的restore目標來下載 NuGet 包。 你可以通過運行類似的東西來做到這一點:

git clone [your repo]
cd [your repo]
msbuild /target:Restore [Your Solution]
msbuild [Your Solution]
function buildVS
{
    param
    (
        [parameter(Mandatory=$true)]
        [String] $path,

        [parameter(Mandatory=$false)]
        [bool] $nuget = $true,
        
        [parameter(Mandatory=$false)]
        [bool] $clean = $true
    )
    process
    {
        $msBuildExe = 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe'
        if ($nuget) {
            Write-Host "Restoring NuGet packages" -foregroundcolor green
            & "$($msBuildExe)" "$($path)"  /p:Configuration=Release /p:platform=x64 /t:restore
        }

        if ($clean) {
            Write-Host "Cleaning $($path)" -foregroundcolor green
            & "$($msBuildExe)" "$($path)" /t:Clean /m
        }

        Write-Host "Building $($path)" -foregroundcolor green
        & "$($msBuildExe)" "$($path)" /t:Build /p:Configuration=Release /p:platform=x64

        
    }
}

暫無
暫無

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

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