powershell - Nuget Powershell : how to add native dependencies? 如何将文件添加到文件夹内的项目?

标签 powershell nuget

我正在创建一个具有 native 依赖项的 Nuget 包。我通过指定附加 file 将它们毫无问题地放入包中.nuspec 中的条目文件。

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

我的想法是将 native 依赖项添加到项目中并设置它们的 BuildActionCopyToOutputDirectory .我还使用下面的 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 将文件添加到项目并将它们放在文件夹中?
或者是否有另一种方法来实现我想要的:将 native 依赖项添加到 NuGet 包并使用我的 Nu-package 将它们输出到项目的 bin 文件夹?

最佳答案

SqlServerCompact 包做了类似的事情,在 post build 事件中将相关的 dll 复制到 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`""

关于powershell - Nuget Powershell : how to add native dependencies? 如何将文件添加到文件夹内的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9842528/

相关文章:

c# - Nuget 包 Microsoft.TypeScript.MSBuild 在 Dockerfile 中失败

powershell - Powershell sqlserver上下文中的复制项目失败

powershell - 在 AD 计算机对象上设置 ACL

powershell - 测试自动化工具未通过 Windows 任务计划程序运行

c# - 如何卸载特定版本的 NuGet 包?

c# - 调试后 Visual Studio 2019 自动删除 nuget 包

来自不同目录的 Nuget 恢复命令

nuget - Nuget导致建筑问题

powershell - 通过Get-ChildItem仅忽略根目录中的文件

c# - 在 PowerShell cmdlet 中导航父子关系