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:
- Add a new Target into the csproj file (MyCustomBeforeBuildTarget)
- Add the BeforeBuild target if it is not already there
- Edit the BeforeBuild DependsOnTargets attribute to include my custom target
So after install my csproj should have the following in:
<Target Name="MyCustomBeforeBuildTarget" Condition="SomeCondition">
<MyCustomTask />
</Target>
<Target Name="BeforeBuild" DependsOnTargets="MyCustomBeforeBuildTarget">
</Target>
Also, it would be nice that when my package is removed, the custom target disappears too,
although I have added a condition that should make it ignore the target if my custom task DLL is not present.
What is the simplest Powershell nuget install script I can write that will add my custom target? I have a feeling that the PowerShell scripts here might form part of the solution, but I don’t have enough PowerShell experience to know how to actually use them to edit the csproj.
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.ps1file in thetoolsfolder 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:
That should be it.
Regards
Jesper Hauge