2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I have tried this approach, but when I install the package, nothing happens. The file is in the nuget package folder, but it is nowhere in the project or solution. I've pasted the nuspec I used above. It doesn't seem to work in either console or web applications.
Is it put into the output directory when you build the project?
Note that you will need to clear your local packages cache using dotnet nuget locals all --clear if you don't increase the version between test attempts
I bump the version based on time, so I don't think that is the issue. I don't see the file in the output folder, and there is no trace of the file in the csproj or sln. Is the idea that the files won't be visible in the project, but only appear in the output? It appears that a lot of people are running into the same issue. I really hate it when nuget behavior changes so drastically, especially when the docs are poor and there are no full examples. Do you have a working example I can start from? github.com/NuGet/Home/issues/4803 github.com/NuGet/Home/issues/2060
Forgot an important detail about the directory layout, fixed up the xml snippet and pushed an example to GitHub
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.