Search code examples
mefnugetnuspec

How can I write Nuspec for MEF Plugin File


I want to write nuspec for MEF plugin. I can copy xxx.dll to content directory, like below.

<files>
  <file src="Alhambra\bin\Release\Plugins\Alhambra.Plugin.SqlServer.dll" target="content\Plugins\Alhambra.Plugin.SqlServer.dll" />
  <file src="Alhambra\bin\Release\Alhambra.dll" target="lib\Alhambra.dll" />
</files>

but I can't set file property in user project to copy output directory.

Thanks for any suggestions or code snippets.


Solution

  • My approach would be to add the plugin to the project as a separate file. For that you need an install script (see NuGet docs for this).

    My solution for this is the following script:

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

    Function add_file($file)
    {
        $do_add = 1
        foreach($item in $project.DTE.ActiveSolutionProjects[0].ProjectItems)
        {
            if ($item -eq $file)
            { $do_add = 0 }
        }
        if ($do_add -eq 1)
        {
            $added = $project.DTE.ItemOperations.AddExistingItem($file)
            $added.Properties.Item("CopyToOutputDirectory").Value = 2
            $added.Properties.Item("BuildAction").Value = 0        
        }
    }
    add_file(<file1>)
    add_file(<file2>)
    

    Of course, when the user uninstalls the package, you need to clean up:

    param($installPath, $toolsPath, $package, $project)
    
    Function remove_file($file)
    {
        foreach($item in $project.DTE.ActiveSolutionProjects[0].ProjectItems)
        {
            if ($item.Name -eq $file)
            { 
              $item.Delete() 
            }
        }
    }
    remove_file(<file1>)
    remove_file(<file2>)