Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget - Setting CopyToOutputDirectory on content in subfolders

I'm new to Nuget and I'm trying to figure out uploading my first package. So far, everything's gone smoothly. However, I'm trying to set CopyToOutputDirectory on some content files that I want to live in a Lib subfolder. My directory looks like this:

│   Readme.txt
│   MyPackage.nupkg
│   MyPackage.nuspec
│
├───content
│   └───Lib
│           native1.dll
│           native2.dll
│           native3.dll
│           native4.dll
│
├───lib
│       MyActualAssembly.dll
│
└───tools
        Install.ps1

From reading this StackOverflow question and some additional reading, I've put together an Install.ps1 that looks like this:

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

$project.ProjectItems.Item("Lib\native1.dll").Properties.Item("CopyToOutputDirectory").Value = 1
$project.ProjectItems.Item("Lib\native2.dll").Properties.Item("CopyToOutputDirectory").Value = 1
$project.ProjectItems.Item("Lib\native3.dll").Properties.Item("CopyToOutputDirectory").Value = 1
$project.ProjectItems.Item("Lib\native4.dll").Properties.Item("CopyToOutputDirectory").Value = 1

I 1-lined the various operations to see if it helped me understand the problem, but it's virtually the same as that answer otherwise.

From my tests, the Install.ps1 is having some trouble with finding the files themselves. When it runs after installing the package, I get the following errors:

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:3 char:1
+ $project.ProjectItems.Item("Lib\native1.dll").Properties.Item("CopyToOutputDirect ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:4 char:1
+ $project.ProjectItems.Item("Lib\native2.dll").Properties.Item("Copy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:5 char:1
+ $project.ProjectItems.Item("Lib\native3.dll").Properties.Item("CopyToOutputDirec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:6 char:1
+ $project.ProjectItems.Item("Lib\native4.dll").Properties.Item("CopyToOut ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

And, as you'd expect, all the files have their CopyToOutputDirectory setting set as Do Not Copy, the default.

How do I resolve this? Is there a different syntax for accessing subfolders in ps scripts? Or am I completely missing the point of those error messages?

like image 592
Craig Brett Avatar asked Mar 17 '13 12:03

Craig Brett


1 Answers

Try the following instead:

$project.ProjectItems.Item("Lib").ProjectItems.Item("native1.dll").Properties.Item("CopyToOutputDirectory").Value = 1

I could be wrong but I do not think that ProjectItems will allow you to locate items that are not direct children of the current item. So you need to find the Lib folder project item first and then look inside this project item for your dll.

To test these I usually open up the Package Manager Console window, make sure the correct project is selected in the Default project drop down list, and then access the project object by using the command line:

$project = Get-Project

This gives you the same thing as the NuGet install script does which is the Visual Studio object model for the project.

like image 92
Matt Ward Avatar answered Oct 21 '22 02:10

Matt Ward