40

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

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

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

4

3 回答 3

13

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

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

mypackage.x86
mypackage.x64
于 2011-11-22T17:33:40.567 回答
13

您可以使用条件引用将 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)
    }
}
于 2012-07-07T16:30:57.137 回答
1

32 位或 64 位架构似乎没有特定的目标。有点痛苦,但你能用 powershell 脚本(install.ps1)来检测架构并相应地安装吗?

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

于 2011-10-14T15:27:25.560 回答