NuGet不会更新我的项目中的dll引用

时间:2015-11-16 09:55:46

标签: c# nuget nuspec nuget-update

我的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保留为可选的{{1}}文件,但在更新NuGet包时仍然会更新?

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

1 个答案:

答案 0 :(得分: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);
        }
    }
}