【问题标题】:Nuget xml file dependencyNuget xml文件依赖
【发布时间】:2019-01-28 21:31:02
【问题描述】:

我依赖于一个配置文件/xml 文件,我将其打包到一个 nuget 包中,在谷歌上搜索并找到了一两个 SO 帖子,这甚至可能吗?我希望引用 nuget 包的项目将 config/xml 文件复制到构建时的调试/发布输出文件夹中,以使其正常运行。

相关的 SO 帖子:

Copy xml from nuget dependency to output

NuGet doesn't copy config file

http://blog.nuget.org/20160126/nuget-contentFiles-demystified.html

我已经尝试过上述帖子/博客中提到的提示/建议,但没有这样的运气。

这甚至可能吗?如果有,怎么做?

【问题讨论】:

    标签: xml configuration nuget


    【解决方案1】:

    事实证明,答案是将 xml 文件作为“内容”放入,然后使用 nuget“工具”目录中的 install.ps1 脚本将文件设置为“复制到输出”。需要注意的重要一点是,此脚本仅在 Visual Studio 中执行,因此必须签入/源代码控制任何内容文件,以便任何构建服务器在服务器签出时都有内容文件。一个小但非常重要的细节!

    我用来将内容文件设置为复制到输出的脚本:

    param($installPath, $toolsPath, $package, $project)
    
    function SetFilePropertiesRecursivelyByFolderName
    {
        $folderKind = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}"; #Physical Folder
        foreach ($subItem in $args[0].ProjectItems)
        {
            $path = $args[1]
            if ($subItem.Kind -eq $folderKind)
            {
                SetFilePropertiesRecursively $subItem ("{0}{1}{2}" -f $path, $args[0].Name, "\")
            }
            else
            {
                Write-Host -NoNewLine ("{0}{1}{2}" -f $path, $args[0].Name, "\")
                SetFileProperties $subItem 2 2 ""
            }
        }
    }
    
    function SetFileProperties
    {
        param([__ComObject]$item, [int]$buildAction, [int]$copyTo, [string]$customTool)
        Write-Host $item.Name
        Write-Host "  Setting Build Action to Content"
        $item.Properties.Item("BuildAction").Value = $buildAction
        Write-Host "  Setting Copy To Output Directory to Copy if newer"
        $item.Properties.Item("CopyToOutputDirectory").Value = $copyTo
        Write-Host "  Setting Custom Tool to blank"
        $item.Properties.Item("CustomTool").Value = $customTool
    }
    
    function SetIndividualFileProperties
    {
        param([string]$fileName)
        Write-Host $fileName
        $file1 = $project.ProjectItems.Item($fileName)
        $file1.Properties.Item("CopyToOutputDirectory").Value = 2
        $file1.Properties.Item("BuildAction").Value = 2
    }
    
    #SetFilePropertiesRecursivelyByFolderName $project.ProjectItems.Item("FolderName")
    #SetIndividualFileProperties "FileName.ext"
    

    编辑

    正如所指出的那样,folderKind 不是很清楚(至少是 guid 或目的),微软网站上有一个链接,用于定义 sln/project 结构的受支持的 Visual Studio guid 列表,即物理文件/文件夹和许多其他元素。可以在这里找到:https://docs.microsoft.com/en-us/visualstudio/extensibility/ide-guids?view=vs-2017

    这些信息大部分是不必要的,因为上面的脚本应该按原样工作,但提供用于进一步阅读/背景材料!

    【讨论】:

    • 你能解释一下 folderKind 应该像“内容”还是来自 .csproj 文件?我对 folderKind 不熟悉。
    • @vfrank66,如果你看这里:docs.microsoft.com/en-us/visualstudio/extensibility/… Visual Studio 中使用的 guid 列表很大。 FolderKind 只是一个变量,但它基本上使用物理文件夹类型的 guid 来表示,在这个物理文件夹上递归并将属性设置为正确的副本到属性。希望这对你有意义?
    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 2019-09-18
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多