Create a custom powershell script for nuget that adds a custom target to the csproj BeforeBuild step

Viewed 5668

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:

  1. Add a new Target into the csproj file (MyCustomBeforeBuildTarget)
  2. Add the BeforeBuild target if it is not already there
  3. 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.

2 Answers
Related