How to add an AfterBuild event to a project when installing my NuGet package?

I have a nuget package that adds an executable that I need to run after building the project every time.

I can add this manually by adding a section to each project file like this:

<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 installing the nuget package? (i.e. using the DTE $ project object in the Install.ps1 file)

I would really appreciate any help on this.

thank

Richard

+5
source share
3 answers

Here is a script that adds an afterbuild target. It also uses 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 tough part got the right quotes. This is what looks like my powertools script.

+3

NuGet 2.5, build\{packageid}.targets ( 'build' , ), NuGet .targets . install.ps1. .

, , , , , "":

<?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>
+5

MSBuild Api, .

Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$msbProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
# use $msbProject to add/change AfterBuild target

NugetPowerTools, Powershell Get-MSBuildProject, .

Also, see this forum for more information on adding a new goal.

+1
source

All Articles