【问题标题】:PackageReference Resolve Version Logic as APIPackageReference 将版本逻辑解析为 API
【发布时间】:2018-08-08 17:47:02
【问题描述】:

是否有任何可通过 MSBuild 任务、目标或其他方式访问的 API,允许我查询给定 PackageReference 将解析(或已经解析)到哪个版本的 NuGet 包?

例如,如果我有一个带有

的 csproj
<PackageReference Include=“MyPkg” Version=“1.*”/>

我有一个我想要的自定义目标

<MyTarget>
  <GetVersionOfResolvedPackageReference Name=“MyPkg” OutputProperty=“IWantToKnowThis” /> <!— or something —>
  ...

【问题讨论】:

    标签: msbuild nuget csproj msbuild-15 packagereference


    【解决方案1】:

    来自How NuGet resolves package dependencies

    当 NuGet 还原过程在构建之前运行时,它会解决 依赖项首先在内存中,然后将结果图写入 使用项目的 obj 文件夹中名为 project.assets.json 的文件 包参考。 MSBuild 然后读取此文件并将其转换为 一组可以找到潜在参考的文件夹,然后 将它们添加到内存中的项目树中。


    <#
    .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 文件。您可以通过Exec 任务从MSBuild 调用脚本,然后通过ReadLinesFromFile 读取文件并执行进一步处理。


    注意:表示的脚本将为多个项目生成 PackageReference 行的副本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2023-01-24
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多