I want to create a nuget package that adds a BeforeBuild step to my csproj using a custom MSBuild task I have created. Ideally, I want to:
The NuGetPowerTools is a package that adds some PowerShell functions that makes it easier to work with the project setup of the project you're adding a package to. To use the functions available you only need to make your package depend on the NuGetPowerTools, using the dependencies tag in your packages nuspec file like this:
This will make it possible to grab a reference to a build project representation of your project.
Then you need to put an install.ps1
file in the tools
folder of your NuGet package, this PowerShell file will run when you install the package and never again after installation.
The file should look something like this:
#First some common params, delivered by the nuget package installer
param($installPath, $toolsPath, $package, $project)
# Grab a reference to the buildproject using a function from NuGetPowerTools
$buildProject = Get-MSBuildProject
# Add a target to your build project
$target = $buildProject.Xml.AddTarget("PublishWebsite")
# Make this target run before build
# You don't need to call your target from the beforebuild target,
# just state it using the BeforeTargets attribute
$target.BeforeTargets = "BeforeBuild"
# Add your task to the newly created target
$target.AddTask("MyCustomTask")
# Save the buildproject
$buildProject.Save()
# Save the project from the params
$project.Save()
That should be it.
Regards
Jesper Hauge