2

I have the following structure in my 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>

I didn't include B.dll in the references, because it is optional for the user to add as a reference.

Now, when I perform Update-Package, it updates the path to A.dll, but not to B.dll when you've added it as a reference.

How can I keep B.dll as an optional lib file, but still make it update when the NuGet package is updated?

I am using the Visual Studio 2013 Package Manager Console.

Marnix
  • 6,384
  • 4
  • 43
  • 78
  • Hmm, you are micro-optimizing too much, shaving off a few millliseconds from the compile time is something nobody ever notices. And requires a manual. Either always add the reference or make it a separate package. – Hans Passant Nov 16 '15 at 12:42

1 Answers1

0

I have found a workaround in the install script in the following link: http://www.jaylee.org/post/2011/11/25/NuGet-package-customizations-and-optional-references.aspx

I had to make some modifications by trial and error and finally made this change:

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

The final script now looks like this:

$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);
        }
    }
}
Marnix
  • 6,384
  • 4
  • 43
  • 78