Solution-wide pre-build event?

后端 未结 7 1356
北恋
北恋 2020-11-30 01:37

I have a solution in Visual Studio which contains several projects. I\'d like to run a command at the very beginning of every build - no matter which projects are involved a

相关标签:
7条回答
  • 2020-11-30 02:22

    Another old post but inspired by @reg solution I wanted to run a simple build timer that would log the elapsed time for a solution build. I got the build events working using a powershell module which I load via the package manager console when the Visual Studio IDE starts.

    So create a powershell module like BuildEvents.psm1:

    <#
    .SYNOPSIS
        Register solution build events
    
    .DESCRIPTION
        Registers the OnBuildBegin and OnBuildDone events for the entire solution
        De-registers the events if called multiple times.
    
    .EXAMPLE
        RegisterBuildEvents
    #>
    function RegisterBuildEvents{
      try {
        Unregister-Event -SourceIdentifier "OnBuildBegin" -Force
      } catch {
        #we don't care if this doesn't work
      }
      try {
        Unregister-Event -SourceIdentifier "OnBuildDone" -Force
      } catch {
        #we don't care if this doesn't work
      }
      $obj = [System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($dte.Application.Events.BuildEvents, [EnvDTE.BuildEventsClass])
      Register-ObjectEvent -InputObject $obj -EventName OnBuildBegin -Action {
        # do stuff here on build begin
        Write-Host "Solution build started!"
      } -SourceIdentifier "OnBuildBegin"
      Register-ObjectEvent -InputObject $obj -EventName OnBuildDone -Action {
        # do stuff here on build done
        Write-Host "Solution build done!" 
      } -SourceIdentifier "OnBuildDone"
    }
    
    # export the functions from the module
    export-modulemember -function RegisterBuildEvents
    

    Import the module when the Package Manager host initialises:

    1. In the package manager console type $profile to get the location of your powershell profile
    2. Browse to that directory on disk, if there is no file there create one with the name returned by the above command (e.g. NuGet_profile.ps1)
    3. Open the file in notepad and add the following lines

      Import-Module -Name <Path to your ps module>\BuildEvents -Force
      RegisterBuildEvents
      
    0 讨论(0)
提交回复
热议问题