windows 我应该如何创建或上传 32 位和 64 位 NuGet 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7769580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:16:20  来源:igfitidea点击:

How should I create or upload a 32-bit and 64-bit NuGet package?

windowsvisual-studionuget64-bitchocolatey

提问by Anthony Mastrean

I have an x86 and x64 version of a binary that I want to upload to NuGet. What's the recommendation or required method for creating/uploading that package? I can't find muchto base my decision on. I see two methods...

我有一个 x86 和 x64 版本的二进制文件,我想上传到 NuGet。创建/上传该包的建议或所需方法是什么?我找不到太多可以作为我决定的依据。我看到两种方法...

  1. Upload them both in the same package
    • Which one should I install by default?
    • Is there a way to test the processor architecture of the project to make the decision?
  2. Upload two separate packages
  1. 将它们上传到同一个包中
    • 我应该默认安装哪一个?
    • 有没有办法测试项目的处理器架构来做决定?
  2. 上传两个单独的包


Bonus question: What if I'm using something like Chocolatey, which wraps up NuGet with package manager semantics? I might need/want the x86 and x64 packages installed on my system.

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

采纳答案by Anthony Mastrean

We've been discussinga similar issue on the Chocolatey Google Group. There aren't any semantics built into NuGet. The requirement wouldn't be, what processor architecture are you running on. It would have to be what processor architecture is your project targeting.And then that complicates things... you'd have to understand AnyCPUas well.

我们一直在Chocolatey Google Group讨论类似的问题。NuGet 中没有内置任何语义。要求不是,你在什么处理器架构上运行。它必须是您的项目针对的处理器架构。然后这让事情变得复杂……你也必须理解。AnyCPU

I think for now, I'm going to upload two packages. I can always published a combined one when I fix up an install.ps1that can handle querying the project target.

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

mypackage.x86
mypackage.x64

回答by Taliesin

You can add x64 and x86 support to a project by using conditional references. It would appear that for now Nuget does not like having two references with the same name. So we need to add in the second reference manually and then make the references conditional.

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

Save x64 assemblies in a folder called x64 & x86 assemblies in a folder called x86 They must both have the same assembly name. Then update the allowedReferences array with the names of all assemblies to add.

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

Use the following scripts.

使用以下脚本。

Install.ps1

安装.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)$', 'x86'
                $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)$', 'x64'
                $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)$', 'amd64'
                    $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'"           
                    }               
                }               
            }           
        }
    }
}

Uninstall.ps1

卸载.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)
    }
}

回答by Chris Pont

There doesn't seem to be a specific target for 32 or 64 bit architectures. Bit of a pain, but can you do something with the powershell scripts (install.ps1) to detect the architecture and install accordingly?

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

See Automatically Running PowerShell Scripts During Package Installation and Removal - http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package

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