Question

Does anyone know if it's possible to add something to a nuspec file so that when a package is installed via NuGet a pre or post build event to a project?

Was it helpful?

Solution

I think editing the user's PostBuildEvent property is the wrong way to go about adding a post-build action to a project. I believe the recommended way is to put your custom action into an MSBuild Target that is imported into the project file. As of NuGet 2.5, if you include a 'build' folder in your package (at the same level as content and tools) and it contains a {packageid}.targets file or {packageid}.props file, NuGet will automatically add an Import to the project file when you install the package.

For example you have a package called MyNuGet. You create a file build\MyNuGet.targets containing:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="MyNuGetCustomTarget" AfterTargets="Build">
        <Message Importance="high" Text="Message from MyNuGetCustomTarget. Configuration: $(Configuration)" />
    </Target>
</Project>

This creates a custom target that is configured to run after the standard Build target. NuGet will handle inserting the Import on install and removing it on uninstall.

OTHER TIPS

This is a slightly cleaner way to set a build event from an install.ps1 powershell script in the NuGet Package:

$project.Properties.Item("PostBuildEvent").Value = "your build event here"

Not to the .nuspec file itself, but you can to install.ps1 which you can add to your package using nuspec. Here is what I'm doing (I don't know if it's the best way to do it but it does work):

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

$project.Properties | where { $_.Name -eq "PreBuildEvent" } | foreach { $_.Value = "copy `"`n`$(ProjectDir)Web.`$(ConfigurationName).config`" `"`$(ProjectDir)Web.config`"" }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top