Nuget Powershell:如何添加本地依赖?如何将文件添加到文件夹内的项目?

问题描述:

我正在创建一个具有本机依赖关系的Nuget包。通过在.nuspec文件中指定其他file条目,我将它们放入包中并没有问题。Nuget Powershell:如何添加本地依赖?如何将文件添加到文件夹内的项目?

但是,我也想将它们复制到要使用我的包的项目的输出文件夹中,以便可以在运行时找到依赖关系。

我的想法是将本机依赖项添加到项目中,并将它们的BuildAction设置为CopyToOutputDirectory。我也使用下面的PowerShell脚本进行管理:

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(<dependency1>) 
add_file(<dependency2>) 
... 
add_file(<dependencyN>) 

到目前为止这么好。

但是,现在我的项目变得完全受到这些依赖关系的污染。

有没有办法使用PowerShell将文件添加到项目中并将它们放入文件夹中?
或者有另一种方法来实现我想要的:添加本地依赖到NuGet包并使用我的Nu包将它们输出到项目的bin文件夹中?

+0

更新:现在我已经摆脱了上述解决方案,而是选择将本地依赖关系设置为程序集链接资源。这会导致本机依赖项被自动复制。设置程序集链接资源本身不支持Visual Studio中的C#项目,但它是针对C++/CLI项目(我在此上下文中仅使用它)。 – 2012-07-18 09:27:35

+0

即时通讯有同样的问题,但我需要添加一个配置文件这篇文章的标题是与下面的解决方案无关改变titel -.-'' – 2016-08-27 22:07:36

SqlServerCompact包做了类似的事情,将相关的dll复制到post build事件中的bin文件夹中。这里的相关代码:

文件:install.ps1

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

. (Join-Path $toolsPath "GetSqlCEPostBuildCmd.ps1") 

# Get the current Post Build Event cmd 
$currentPostBuildCmd = $project.Properties.Item("PostBuildEvent").Value 

# Append our post build command if it's not already there 
if (!$currentPostBuildCmd.Contains($SqlCEPostBuildCmd)) { 
    $project.Properties.Item("PostBuildEvent").Value += $SqlCEPostBuildCmd 
} 

文件:GetSqlCEPostBuildCmd.ps1

$solutionDir = [System.IO.Path]::GetDirectoryName($dte.Solution.FullName) + "\" 
$path = $installPath.Replace($solutionDir, "`$(SolutionDir)") 

$NativeAssembliesDir = Join-Path $path "NativeBinaries" 
$x86 = $(Join-Path $NativeAssembliesDir "x86\*.*") 
$x64 = $(Join-Path $NativeAssembliesDir "amd64\*.*") 

$SqlCEPostBuildCmd = " 
if not exist `"`$(TargetDir)x86`" md `"`$(TargetDir)x86`" 
xcopy /s /y `"$x86`" `"`$(TargetDir)x86`" 
if not exist `"`$(TargetDir)amd64`" md `"`$(TargetDir)amd64`" 
xcopy /s /y `"$x64`" `"`$(TargetDir)amd64`"" 
+0

谢谢,我会在w/e之后尝试。 – 2012-03-23 19:06:54

我建议你打开4.0.8852.1版本SqlServerCompact NuGet包与Nuget Package Explorer和用它作为模板。它为我工作。

+0

那。是。凉。 – JMarsch 2013-04-26 19:38:54