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

前端 未结 2 1037
终归单人心
终归单人心 2020-12-28 08:50

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 int
2条回答
  •  时光取名叫无心
    2020-12-28 09:31

    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

提交回复
热议问题