如何删除安装包后安装的依赖项的文件

How to delete files a dependency installed after your package is installed

我正在开发一个依赖于 Unity 和 Unity.Mvc4 包的 nuget 包。我的 nuspec 文件将它们列为依赖项。一切正常,但是当我的包安装时,首先安装依赖项。

Unity 依赖项将文件部署到我的包已移动到不同位置并自定义的项目的根目录,因此我不希望这些文件在我的包安装后存在于根目录中。

有没有办法覆盖 nuspec 中的依赖文件,或者 运行 安装后的 powershell 脚本来清理它们?

您可以添加一个 Powershell 脚本,将 Unity 创建的那些文件移动到您的实际项目根目录。

有关在 Nuget 包安装期间执行 Powershell 脚本的信息,请参阅 the Nuget documentation。请注意,这些脚本必须放在 Nuget 包的 tools 文件夹中才能自动执行。

这是我安装中的代码。ps1 如果有人需要它作为参考,它可以解决问题。

# Runs every time a package is installed in a project

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

# $installPath is the path to the folder where the package is installed.
# $toolsPath is the path to the tools directory in the folder where the     package is installed.
# $package is a reference to the package object.
# $project is a reference to the project the package was installed to.

#EnvDTE Project Reference
#https://msdn.microsoft.com/en-us/library/envdte.project.projectitems.aspx

function deleteProjectItem($fileName) {
    $file = $null
    foreach ($item in $project.ProjectItems) {
        if ($item.Name.Equals($fileName, "InvariantCultureIgnoreCase")) {
            $file = $item
            break
        }
    }
    if ($file -ne $null) {
        Write-Host "Deleting: $($item.Name) ..." -NoNewline
        $item.Delete()
        Write-Host "Deleted!"
    }
}

deleteProjectItem "BootStrapper.cs"
deleteProjectItem "job_scheduling_data_2_0.xsd"
deleteProjectItem "Unity.Mvc4.README.txt"