Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create NuGet package which installs references with Copy Local set to false

Tags:

nuget

Is there a way to create a NuGet package where when the package is installed into a project it adds references to the dlls with "Copy Local" set to false?

I assume it would be some kind of script within the 'install.ps1' file.

like image 469
Kevin Kalitowski Avatar asked Jun 19 '12 16:06

Kevin Kalitowski


2 Answers

Yes you can do this with install.ps1, as you guessed.

Here's an example install.ps1 that will flip the flag on System.dll every time you run it. You should be able to get an idea how to do what you want using this example:

param($installPath, $toolsPath, $package, $project)

foreach ($reference in $project.Object.References)
{
    if($reference.Name -eq "System")
    {
        if($reference.CopyLocal -eq $true)
        {
            $reference.CopyLocal = $false;
        }
        else
        {
            $reference.CopyLocal = $true;
        }
    }
}

But this MSDN documentation should help.

  • The Object Hanselman uses resolves to the VSProject Interface.
  • The $project variable NuGet gives you resolves to the Project Interface.
like image 139
Jim Counts Avatar answered Oct 18 '22 09:10

Jim Counts


I think this install.ps1 does what you ask for - all DLLs added by the package are given a Copy Local value of false. Note that here I'm not doing anything with the AssemblyReferences list other than getting all of the names from it - you may have to do something more if you want conditions based on target framework, etc.

param($installPath, $toolsPath, $package, $project)

$asms = $package.AssemblyReferences | %{$_.Name}

foreach ($reference in $project.Object.References)
{
    if ($asms -contains $reference.Name + ".dll")
    {
        $reference.CopyLocal = $false;
    }
}
like image 33
nlawalker Avatar answered Oct 18 '22 10:10

nlawalker