2012-03-23 103 views
7

我正在創建一個具有本機依賴關係的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

回答

7

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

相關問題