I have a nuget package which adds an executable that I need to run after a project builds each time.
I can manually add this by adding a section to each project file like so:
<Target Name="AfterBuild">
<PropertyGroup>
<PathToOutputExe>..\bin\Executable.exe</PathToOutputExe>
<PathToOutputJs>"$(MSBuildProjectDirectory)\Scripts\Output.js"</PathToOutputJs>
<DirectoryOfAssemblies>"$(MSBuildProjectDirectory)\bin\"</DirectoryOfAssemblies>
</PropertyGroup>
<AspNetCompiler Condition="'$(MvcBuildViews)'=='true'" VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
<Exec Command="$(PathToOutputExe) $(PathToOutputJs) $(DirectoryOfAssemblies)" />
</Target>
How can I add this to a project when I install the nuget package? (i.e. using the DTE $project object in the Install.ps1 file)
I would greatly appreciate any help on this.
Thanks
Richard
As of NuGet 2.5, by convention, if you add a targets file at build\{packageid}.targets
(note 'build' is at the same level as content and tools), NuGet will automatically add an Import to the .targets file in the project. Then you don't need handle anything in install.ps1. The Import will be automatically removed on uninstall.
Also, I think the recommended way to do what you want would to create a separate target that is configured to run after the standard "Build" target:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="NuGetCustomTarget" AfterTargets="Build">
<PropertyGroup>
<PathToOutputExe>..\bin\Executable.exe</PathToOutputExe>
<PathToOutputJs>"$(MSBuildProjectDirectory)\Scripts\Output.js"</PathToOutputJs>
<DirectoryOfAssemblies>"$(MSBuildProjectDirectory)\bin\"</DirectoryOfAssemblies>
</PropertyGroup>
<AspNetCompiler Condition="'$(MvcBuildViews)'=='true'" VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
<Exec Command="$(PathToOutputExe) $(PathToOutputJs) $(DirectoryOfAssemblies)" />
</Target>
</Project>
Here's a script that adds a afterbuild target. It also user the NugetPowerTools mentioned above.
$project = Get-Project
$buildProject = Get-MSBuildProject
$target = $buildProject.Xml.AddTarget("MyCustomTarget")
$target.AfterTargets = "AfterBuild"
$task = $target.AddTask("Exec")
$task.SetParameter("Command", "`"PathToYourEXe`" $(TargetFileName)")
The hard part was getting the quotes right. This is what it looks like in my powertools script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With