Blogs

Visual Studio IDE Macro – Build Events

Jan 18

Written by:
18 January 2012 14:20 

In Visual Studio we can create macros that plug in to the Evironment Events. The Macro in this blog post will output to the Build window the date and time the Process was completed. Whilst the macro is simple it is a great starting point for Building more macros that utilise the Visual Studio IDE.

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.IO
 
Public Module EnvironmentEvents
 
#Region "Automatically generated code, do not modify"
    'Automatically generated code, do not modify
    'Event Sources Begin
     Public WithEvents DTEEvents As EnvDTE.DTEEvents
     Public WithEvents DocumentEvents As EnvDTE.DocumentEvents
     Public WithEvents WindowEvents As EnvDTE.WindowEvents
     Public WithEvents TaskListEvents As EnvDTE.TaskListEvents
     Public WithEvents FindEvents As EnvDTE.FindEvents
     Public WithEvents OutputWindowEvents As EnvDTE.OutputWindowEvents
     Public WithEvents SelectionEvents As EnvDTE.SelectionEvents
     Public WithEvents BuildEvents As EnvDTE.BuildEvents
     Public WithEvents SolutionEvents As EnvDTE.SolutionEvents
     Public WithEvents SolutionItemsEvents As EnvDTE.ProjectItemsEvents
     Public WithEvents MiscFilesEvents As EnvDTE.ProjectItemsEvents
     Public WithEvents DebuggerEvents As EnvDTE.DebuggerEvents
     Public WithEvents ProjectsEvents As EnvDTE.ProjectsEvents
     Public WithEvents TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents
     Public WithEvents CodeModelEvents As EnvDTE80.CodeModelEvents
     Public WithEvents DebuggerProcessEvents As EnvDTE80.DebuggerProcessEvents
     Public WithEvents DebuggerExpressionEvaluationEvents As EnvDTE80.DebuggerExpressionEvaluationEvents
    'Event Sources End
    'End of automatically generated code
#End Region
 
    Dim _startTime As DateTime
 
    Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
        _startTime = DateTime.Now
        WriteState(String.Format("Start Build     : {0}", _startTime.ToString("dd MMM yyyy HH:mm:ss")))
    End Sub
 
    Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
        Dim endTime As DateTime = DateTime.Now
        Dim timeSpent As TimeSpan = CType((endTime - _startTime), TimeSpan)
        Dim total As Decimal = GetPreviousTime() + Convert.ToDecimal(timeSpent.TotalSeconds)
        WriteState(String.Format("End Build       : {0}", endTime.ToString("dd MMM yyyy HH:mm:ss")))
        WriteState(String.Format("Total BuildTime : {0}", timeSpent.TotalSeconds))
        WriteToLogFile(String.Format("{0}, {1}, {2}", _startTime.ToString("dd MMM yyyy HH:mm:ss"), timeSpent.TotalSeconds.ToString(), total))
    End Sub
 
    Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone
        If Success = False Then 'The build failed...cancel any further builds.
            DTE.ExecuteCommand("Build.Cancel")
            WriteState(String.Format("Build failed - Build process halted for project {0}", Project))
        End If
    End Sub
 
    Function GetOutputWindow() As OutputWindow
        Return DTE.Windows.Item(Constants.vsWindowKindOutput).Object()
    End Function
 
    Function GetActivePane() As OutputWindowPane
        Return GetOutputWindow.ActivePane
    End Function
 
    Private Sub WriteState(ByVal message As String)
        GetActivePane.OutputString(String.Format(message) & vbCrLf)
    End Sub
 
    Private Function GetPreviousTime() As Decimal
        Dim fi As New FileInfo(DTE.Solution.FullName)
        Dim fileName As String = String.Format("c:\\{0}.build.log", fi.Name)
 
        If (File.Exists(fileName) = False) Then
            Return 0
        End If
 
        Using sr = New StreamReader(fileName, True)
            Dim lastString As String
            While (Not sr.EndOfStream)
                lastString = sr.ReadLine()
            End While
            sr.Close()
            If (lastString = String.Empty) Then
                Return 0
            End If
            Dim characters() As Char = {","c}
            Dim strings() As String = lastString.Split(characters)
            Dim totalString As String = strings(strings.Length - 1)
            Dim total As Decimal = Convert.ToDecimal(totalString)
            Return total
        End Using
 
    End Function
 
    Private Sub WriteToLogFile(ByVal message As String)
 
        Dim fi As New FileInfo(DTE.Solution.FullName)
        Using sw = New StreamWriter("c:\\" & fi.Name & ".build.log", True)
            sw.WriteLine(message)
            sw.Flush()
            sw.Close()
        End Using
    End Sub
 
 
End Module


Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

Comments

Re: PageSearch module Finished
What a fabulous post this has been.I am grateful to you and expect more number of posts like these. Thank you very much.
SEO Expert India | SEO Freelancer India
Re: PageSearch module Finished
Hello Leigh,
It's a great job. I would be pleased to receive a copy of the module to install it on my web site and collect feedback from my customers.
Sincerely,
Bertrand.
Re: DotNetNuke 3D Flags
the language skinobject allows you to use your own location for flagimages, so they won't be overwritten on upgrades. See this blog post for more info on how to use the language skinobject: www.dotnetnuke.com/Resources/Blogs/EntryId/1957/The-Language-SkinObject-explained.aspx

Search