Есть ли какой-либо доступный API через задачу MSBuild, цель или иным образом, который позволяет мне запрашивать, какую версию пакета NuGet разрешит (или уже разрешила) данная PackageReference?
Например, если у меня есть csproj с
<PackageReference Include=“MyPkg” Version=“1.*”/>
И у меня есть настраиваемая цель, в которой я хочу
<MyTarget>
<GetVersionOfResolvedPackageReference Name=“MyPkg” OutputProperty=“IWantToKnowThis” /> <!— or something —>
...
От Как NuGet разрешает зависимости пакетов:
When the NuGet restore process runs prior to a build, it resolves dependencies first in memory, then writes the resulting graph to a file called project.assets.json in the obj folder of a project using PackageReference. MSBuild then reads this file and translates it into a set of folders where potential references can be found, and then adds them to the project tree in memory.
<#
.Synopsis
Represents the method that
returns the project.assets
content as object.
#>
function Get-ProjectAssest([System.String]$Assest) {
return (Get-Content $Assest | ConvertTo-Json | ConvertFrom-Json).value |
ConvertFrom-Json
}
Get-ChildItem -Path . 'project.assets.json' -Recurse | ForEach-Object { Get-ProjectAssest($_.FullName) } | ForEach-Object {
# K = Package Identity
# V = Package Version
$_.libraries | ForEach-Object { $_.PSObject.Properties.Name } | Out-File 'PackageReference.ini' -Append
}
Этот сценарий PowerShell
рекурсивно анализирует каталоги, чтобы найти project.assets.json
, и записывает результат в файл PackageReference.ini
. Вы можете вызвать скрипт из MSBuild через задачу Exec, а затем прочитать файл через ReadLinesFromFile и произвести дальнейшую обработку.
NOTE: Represented script will be produce duplicates of
PackageReference
lines for multiple projects.