0

I have a dependency on a config file/xml file that I am wrapping up into a nuget package, having googled this and found a SO post or two, is this even possible? I want the project referencing the nuget package to have the config/xml file copied to the debug/release output folder on build to enable it to function properly.

Related SO posts:

Copy xml from nuget dependency to output

NuGet doesn't copy config file

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

I have tried the tips/suggestions mentioned in the above posts/blogs, but no such luck.

Is this even possible? If so, how?

Community
  • 1
  • 1
Chris Watts
  • 822
  • 1
  • 9
  • 27

1 Answers1

0

The answer, as it turns out, is to put the xml file in as a "content" and then use an install.ps1 script in the nuget "tools" directory to set the file to "Copy To Output". The important thing to note is that this script only gets executed in visual studio, so any content files must be checked in/source controlled, so that any build server has the content files when the server checks it out. A small but incredibly important detail!!

The script I use to set the content files as copy to output:

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"

EDIT

As it was pointed out that folderKind wasnt very clear (the guid or purpose at least), there is a link on microsoft site for the list of supported visual studio guids that define sln/project structures i.e. for physical file/folders and many other elements. This can be found here: https://learn.microsoft.com/en-us/visualstudio/extensibility/ide-guids?view=vs-2017

This information is mostly unnecessary as the script above should work as is, but provided for further reading/background material!

Chris Watts
  • 822
  • 1
  • 9
  • 27
  • Can you explan is that folderKind supposed to be like "content" or is that from the .csproj file? I am not familar with folderKind. – vfrank66 Jan 25 '19 at 19:11
  • @vfrank66 , if you look here: https://learn.microsoft.com/en-us/visualstudio/extensibility/ide-guids?view=vs-2017 There is a large list of the guids used in visual studio. FolderKind is simply a variable, but it uses the guid of the physical folder type to say basically, recurse on this physical folder and set the attributes to the right copy to attribute. hope that makes sense to you? – Chris Watts Jan 28 '19 at 13:28