【问题标题】:How should I create or upload a 32-bit and 64-bit NuGet package?我应该如何创建或上传 32 位和 64 位 NuGet 包?
【发布时间】:2011-12-07 20:21:06
【问题描述】:

我想要上传到 NuGet 的 x86 和 x64 版本的二进制文件。创建/上传该包的建议或所需方法是什么?我不能find much 来做决定。我看到了两种方法...

  1. 将它们上传到同一个包中
    • 我应该默认安装哪一个?
    • 有没有办法测试项目的处理器架构来做出决定?
  2. 上传两个单独的包

额外问题:如果我使用像 Chocolatey 这样的东西,它用包管理器语义封装了 NuGet,该怎么办?我可能需要/想要在我的系统上安装 x86 和 x64 软件包。

【问题讨论】:

  • 如果你也遇到这个问题,请投票给这个作品 NuGet 项目:nuget.codeplex.com/workitem/679
  • 这个问题有更新吗?
  • 让我更新问题,至少更新我的答案。因为我相信我问的是 Chocolatey 软件包,当时它还很年轻,没有内置强大的 32 位和 64 位功能。

标签: windows visual-studio nuget 64-bit chocolatey


【解决方案1】:

我们在Chocolatey Google Group 上遇到过类似的问题discussing。 NuGet 中没有内置任何语义。要求不会是,您在什么处理器架构上运行。它必须是你的项目目标是什么处理器架构。然后事情变得复杂......你还必须了解AnyCPU

我想现在,我要上传两个包。当我修复一个可以处理查询项目目标的install.ps1 时,我总是可以发布一个组合的。

mypackage.x86
mypackage.x64

【讨论】:

【解决方案2】:

您可以使用条件引用将 x64 和 x86 支持添加到项目中。现在看来,Nuget 不喜欢有两个同名的引用。所以我们需要手动添加第二个引用,然后将引用设置为有条件的。

将 x64 程序集保存在名为 x64 的文件夹中,并将 x86 程序集保存在名为 x86 的文件夹中。它们必须具有相同的程序集名称。然后使用要添加的所有程序集的名称更新 allowedReferences 数组。

使用以下脚本。

安装.ps1

$allowedReferences = @("Noesis.Javascript")

# Full assembly name is required
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

$projectCollection = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection

$allProjects = $projectCollection.GetLoadedProjects($project.Object.Project.FullName).GetEnumerator();

if($allProjects.MoveNext())
{
    $currentProject = $allProjects.Current

    foreach($Reference in $currentProject.GetItems('Reference') | ? {$allowedReferences -contains $_.Xml.Include })
    {
        $hintPath = $Reference.GetMetadataValue("HintPath")

        write-host "Matched againt $hintPath"

        #If it is x64 specific add condition (Include 'Any Cpu' as x64)
        if ($hintPath -match '.*\\(amd64|x64)\\.*\.dll$')
        {
            $Reference.Xml.Condition = "'TargetPlatform' != 'x86'"

            $condition = $Reference.Xml.Condition
            write-host "hintPath = $hintPath"
            write-host "condition = $condition"

            #Visual Studio doesnt allow the same reference twice (so try add friends)
            $matchingReferences = $currentProject.GetItems('Reference') | ? {($_.Xml.Include -eq $Reference.Xml.Include) -and ($_.GetMetadataValue("HintPath") -match ".*\\(x86)\\.*\.dll$")}

            if (($matchingReferences | Measure-Object).Count -eq 0)
            {
                $x86 = $hintPath -replace '(.*\\)(amd64|x64)(\\.*\.dll)$', '$1x86$3'
                $x86Path = Join-Path $installPath $x86

                if (Test-Path $x86Path) {
                    #Add 
                    write-host "Adding reference to $x86"

                    $metaData = new-object "System.Collections.Generic.Dictionary``2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
                    $metaData.Add("HintPath", $x86)
                    $currentProject.AddItem('Reference', $Reference.Xml.Include, $metaData)

                    $newReference = $currentProject.GetItems('Reference') | ? {($_.Xml.Include -eq $Reference.Xml.Include) -and ($_.GetMetadataValue("HintPath") -eq $x86)} | Select-Object -First 1

                    $newReference.Xml.Condition = "'TargetPlatform' == 'x86'"           
                }
            }
        }

        #If it is x86 specific add condition 
        if ($hintPath -match '.*\\x86\\.*\.dll$')
        {
            $Reference.Xml.Condition = "'TargetPlatform' == 'x86'"

            $condition = $Reference.Xml.Condition
            write-host "hintPath = $hintPath"
            write-host "condition = $condition"

            #Visual Studio doesnt allow the same reference twice (so try add friends)
            $matchingReferences = $currentProject.GetItems('Reference') | ? {($_.Xml.Include -eq $Reference.Xml.Include) -and ($_.GetMetadataValue("HintPath") -match ".*\\(amd64|x64)\\.*\.dll$")}

            if (($matchingReferences | Measure-Object).Count -eq 0)
            {
                $x64 = $hintPath -replace '(.*\\)(x86)(\\.*\.dll)$', '$1x64$3'
                $x64Path = Join-Path $installPath $x64

                if (Test-Path $x64Path) {
                    #Add 
                    write-host "Adding reference to $x64"

                    $metaData = new-object "System.Collections.Generic.Dictionary``2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
                    $metaData.Add("HintPath", $x64)
                    $currentProject.AddItem('Reference', $Reference.Xml.Include, $metaData)

                    $newReference = $currentProject.GetItems('Reference') | ? {($_.Xml.Include -eq $Reference.Xml.Include) -and ($_.GetMetadataValue("HintPath") -eq $x64)} | Select-Object -First 1

                    $newReference.Xml.Condition = "'TargetPlatform' != 'x86'"           
                } else {
                    $amd64 = $hintPath -replace '(.*\\)(x86)(\\.*\.dll)$', '$1amd64$3'
                    $amd64Path = Join-Path $installPath $amd64

                    if (Test-Path $amd64Path) {
                        #Add 
                        write-host "Adding reference to $amd64"

                        $metaData = new-object "System.Collections.Generic.Dictionary``2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
                        $metaData.Add("HintPath", $amd64)
                        $currentProject.AddItem('Reference', $Reference.Xml.Include, $metaData)

                        $newReference = $currentProject.GetItems('Reference') | ? {($_.Xml.Include -eq $Reference.Xml.Include) -and ($_.GetMetadataValue("HintPath") -eq $amd64)} | Select-Object -First 1

                        $newReference.Xml.Condition = "'TargetPlatform' != 'x86'"           
                    }               
                }               
            }           
        }
    }
}

卸载.ps1

$allowedReferences = @("Noesis.Javascript")

# Full assembly name is required
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

$projectCollection = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection

$allProjects = $projectCollection.GetLoadedProjects($project.Object.Project.FullName).GetEnumerator();

if($allProjects.MoveNext())
{
    foreach($Reference in $allProjects.Current.GetItems('Reference') | ? {$allowedReferences -contains $_.UnevaluatedInclude })
    {
        $allProjects.Current.RemoveItem($Reference)
    }
}

【讨论】:

    【解决方案3】:

    似乎没有针对 32 位或 64 位架构的特定目标。有点痛苦,但是您可以使用 powershell 脚本 (install.ps1) 来检测架构并进行相应的安装吗?

    请参阅在包安装和删除期间自动运行 PowerShell 脚本 - http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 2018-02-21
      • 1970-01-01
      • 2012-10-26
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 2011-03-08
      相关资源
      最近更新 更多