建立 NuGet 的 x 腳本資產快取來源
注意
本節涵蓋 vcpkg 的實驗功能,隨時可能會變更或移除。
在此範例中,我們將使用腳本來還原和推送成品,將 NuGet 摘要設定為資產快取來源。
必要條件
- nuget.exe
- NuGet 套件摘要
步驟 1:建立 asset-source.nuspec
使用下列內容建立 NuGet 套件規格範本:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>$sha$</id>
<version>1.0.0</version>
<description>vcpkg download asset</description>
<authors>vcpkg</authors>
</metadata>
<files>
<file src="$file$" />
</files>
</package>
步驟 2:建立資產提供者腳本
現在,您必須建立腳本,以在可用時從 NuGet 摘要下載套件,並在未下載套件時將遺漏的套件上傳至您的摘要。
使用下面提供的內容建立 asset-provider.bat
,請務必以系統上的正確值取代 NuGet 摘要 URL 和 路徑 asset-source.nuspec
。
@echo off
set url=%1
set sha512=%2
set dst=%3
set "_dst=%dst:/=\%"
set "_sha512=%sha512:~0,90%"
cd /d %~dp3
%NUGET% install %sha512:~0,90% -Source https://your-nuget-feed-url
echo.
if exist %_sha512%.1.0.0 (
echo "Pull from the NuGet feed"
cd %_sha512%.1.0.0
REM Assume both are files not directories
echo "F" | xcopy /f *.part %_dst%
) else (
echo "Fetch from the url"
curl.exe -L %url% --create-dirs --output %dst%
REM Replace with the correct path
%NUGET% pack C:\path\to\asset-source.nuspec -BasePath %~dp3 -Properties "sha=%_sha512%;file=%dst%" -OutputDirectory %TEMP%
%NUGET% push -ApiKey az -SkipDuplicate %TEMP%\%_sha512%.1.0.0.nupkg -Source https://your-nuget-feed-url
)
步驟 3:設定資產快取來源
既然您已建立資產提供者腳本,您必須指示 vcpkg 使用它作為資產快取來源。 若要這樣做,請設定下列環境變數:
$env:X_VCPKG_ASSET_SOURCES="clear;x-script,C:/path/to/asset-provider.bat {url} {sha512} {dst};x-block-origin"
$env:NUGET="C:/path/to/nuget.exe"
$env:VCPKG_KEEP_ENV_VARS="NUGET"
注意:請務必將資產提供者腳本和 nuget.exe 的佔位元路徑取代為您系統中的正確路徑。
X_VCPKG_ASSET_SOURCES
是用來設定 vcpkg 使用之資產快取來源的環境變數。 在此範例中,我們會設定下列值:
clear
會移除預設資產快取位置。x-script
將腳本新增為資產快取來源,第一個參數表示命令行 vcpkg 應該叫用,在此範例中,我們會呼叫asset-provider.bat
腳本,並轉送一些必要的參數。x-block-origin
會強制所有下載來自已設定的資產快取來源。
VCPKG_KEEP_ENV_VARS
是用來將環境變數轉送至 vcpkg 的組建環境。 在建置期間,vcpkg 會藉由新增 NUGET
來 VCPKG_KEEP_ENV_VARS
建立全新的環境,以確保 NuGet 可執行檔位置會在組建期間轉送。
一旦所有專案都已正確設定,每當 vcpkg 下載資產時,就會將其上傳至您的 NuGet 摘要,以在未來的下載中使用。 您會發現快取的資產是以其檔案 SHA512 和 中指定的 asset-source.nuspec
版本命名。 如果您想要為套件提供更卑鄙的名稱,您可以使用自己的邏輯來修改套件範本和資產提供者腳本。