Search code examples
powershellnuget.net-core.net-standardnuspec

Options for placing and updating PowerShell files in Project folder using NuGet


I am building a NuGet package that only contains a set of PowerShell scripts. The desired behavior is for the scripts to be placed in the project and/or solution folder, removed when the package is uninstalled, and updated when the package is updated. These scripts just need to live in the folder, and be copied to output folder (they are deploy scripts).

I've done this before using content target in a nuspec, but this does not work in netstandard/.NET Core applications (i.e., anything that uses PackageReference). The NuGet documentation mentioned the contentFiles element under the metadata element, but that also does not work with PackageReference. The only thing that I have been able to get working at all is copying the PowerShell scripts in tools/init.ps1. I have something partially working, but it doesn't handle the uninstall or upgrade path. And DTE is never fun.

Is there a way to use content files in netstandard?

If not, does there exist a sample or example of how to properly manage the NuGet lifecycle (copy file, update file, delete file)?

EDIT: This is for a console application, but it also should work in an asp.net application.

EDIT2: This is the nuspec that I have tried to do things via the contentFiles element. When I install the package, I don't see anything in project or solution.

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>Test</id>
        <version>1.0.0</version>
        <contentFiles>
            <files include="Build.ps1" buildAction="Content" copyToOutput="true" />
        </contentFiles>
    </metadata>
    <files>
        <file src="Build.ps1" target="content" />
    </files>
</package>

Thanks, Erick


Solution

  • As you have noticed, NuGet packages referenced via PackageReference no longer modify the project when installing. This is also the case for .NET Framework projects using this new feature (it was made public for non-core projects in VS 2017 15.2).

    The content/someScript.ps1 file is still necessary for compatibility with "classic" packages.config based project, but a new feature for PackageReference projects is contentFiles.

    When packing manually using a nuspec file, copying a file on build can be done by adding a contentFiles section:

    <package>
      <metadata>
        ...
        <contentFiles>
            <files include="**/*.ps1" buildAction="Content" copyToOutput="true" />
        </contentFiles>
      </metadata>
      <files>
        <file src="Build.ps1" target="content" />
        <file src="Build.ps1" target="contentFiles/any/any" />
      </files>
    </package>
    

    See the documentation on contentFiles for more details my example on GitHub.