2

我的 nuspec 中有以下结构:

<package>
  <metadata>
    <references>
      <reference file="A.dll"/>
    </references>
  </metadata>
  <files>
    <file src="A.dll" target="lib\net40\A.dll"/>
    <file src="B.dll" target="lib\net40\B.dll"/>
  </files>
</package>

我没有包含B.dll在参考资料中,因为用户可以选择添加作为参考资料。

现在,当我执行 时Update-Package,它会更新到 的路径A.dll,但不会B.dll在您将其添加为参考时更新。

如何将其保留B.dll为可选lib文件,但在更新 NuGet 包时仍使其更新?

我正在使用 Visual Studio 2013 包管理器控制台。

4

1 回答 1

0

我在以下链接的安装脚本中找到了一种解决方法:http ://www.jaylee.org/post/2011/11/25/NuGet-package-customizations-and-optional-references.aspx

我不得不通过反复试验进行一些修改,最后做出了这个改变:

# removed the + sign in *?\\ and removed the double trailing backslash
$newHintPath = $hintPath -replace "$packageId.*?\\", "$packageName\"

最终脚本现在如下所示:

$packageName = $package.Id + '.' + $package.Version;
$packageId = $package.Id;

# 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

# There is no indexer on ICollection<T> and we cannot call
# Enumerable.First<T> because Powershell does not support it easily and
# we do not want to end up MethodInfo.MakeGenericMethod.
$allProjects = $projectCollection.GetLoadedProjects($project.Object.Project.FullName).GetEnumerator(); 

Write-Host "Replace hint paths in project"
if($allProjects.MoveNext())
{
    foreach($item in $allProjects.Current.GetItems("Reference"))
    {
        $hintPath = $item.GetMetadataValue("HintPath")

        $newHintPath = $hintPath -replace "$packageId.*?\\", "$packageName\"

        if($hintPath -ne $newHintPath)
        {
            Write-Host "Updating $hintPath to $newHintPath"
            $item.SetMetadataValue("HintPath", $newHintPath);
        }
    }
}
于 2015-11-16T12:41:42.470 回答