简体   繁体   English

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

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

I have an x86 and x64 version of a binary that I want to upload to NuGet. 我有一个x86和x64版本的二进制文件,我想上传到NuGet。 What's the recommendation or required method for creating/uploading that package? 创建/上传该软件包的建议或要求是什么? I can't find much to base my decision on. 找不到太多决定。 I see two methods... 我看到两种方法......

  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 上传两个单独的包

Bonus question: What if I'm using something like Chocolatey , which wraps up NuGet with package manager semantics? 奖金问题:如果我使用像Chocolatey这样的东西,它会用包管理器语义包装NuGet怎么办? I might need/want the x86 and x64 packages installed on my system. 我可能需要/想要在我的系统上安装x86和x64软件包。

You can add x64 and x86 support to a project by using conditional references. 您可以使用条件引用向项目添加x64和x86支持。 It would appear that for now Nuget does not like having two references with the same name. 现在看起来Nuget不喜欢有两个同名的引用。 So we need to add in the second reference manually and then make the references conditional. 所以我们需要手动添加第二个引用,然后使引用成为条件。

Save x64 assemblies in a folder called x64 & x86 assemblies in a folder called x86 They must both have the same assembly name. 将x64程序集保存在名为x86的文件夹中名为x64和x86程序集的文件夹中。它们必须具有相同的程序集名称。 Then update the allowedReferences array with the names of all assemblies to add. 然后使用要添加的所有程序集的名称更新allowedReferences数组。

Use the following scripts. 使用以下脚本。

Install.ps1 Install.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'"           
                    }               
                }               
            }           
        }
    }
}

Uninstall.ps1 Uninstall.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)
    }
}

We've been discussing a similar issue on the Chocolatey Google Group . 我们一直在讨论关于Chocolatey Google Group的类似问题。 There aren't any semantics built into NuGet. 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 AnyCPU as well. 然后这使事情变得复杂......你也必须了解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.ps1 that can handle querying the project target. 当我修复一个可以处理查询项目目标的install.ps1时,我总是可以发布一个组合的。

mypackage.x86
mypackage.x64

There doesn't seem to be a specific target for 32 or 64 bit architectures. 对于32位或64位架构似乎没有特定的目标。 Bit of a pain, but can you do something with the powershell scripts (install.ps1) to detect the architecture and install accordingly? 有点痛苦,但你能用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM