Nuget - 在子文件夹中的内容上设置CopyToOutputDirectory

时间:2013-03-17 12:48:47

标签: powershell nuget

我是Nuget的新手,我试图找出上传我的第一个套餐。到目前为止,一切都进展顺利。但是,我试图在一些我希望生活在Lib子文件夹中的内容文件上设置CopyToOutputDirectory。我的目录如下所示:

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

从阅读this StackOverflow question和其他一些阅读材料中,我将一个看起来像这样的Install.ps1放在一起:

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

我排除了各种操作,看看它是否有助于我理解这个问题,但它与其他答案几乎相同。

从我的测试中,Install.ps1在查找文件本身时遇到了一些麻烦。在安装软件包后运行时,我收到以下错误:

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

并且,正如您所期望的那样,所有文件都将其CopyToOutputDirectory设置设置为“不复制”,默认设置。

我该如何解决这个问题?在ps脚本中访问子文件夹有不同的语法吗?或者我完全忽略了这些错误消息的意思?

1 个答案:

答案 0 :(得分:10)

请尝试以下方法:

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

我可能错了,但我不认为ProjectItems会允许您找到不是当前项目的直接子项的项目。因此,您需要先找到Lib文件夹项目项,然后查看此项目项目中的dll。

要测试这些,我通常打开Package Manager Console窗口,确保在Default project下拉列表中选择了正确的项目,然后使用命令行访问项目对象:

$ project = Get-Project

这为您提供了与NuGet安装脚本相同的功能,它是项目的Visual Studio对象模型。