138
|
Chapter 4, Customizing Visual Studio
#35 Modify the Build Output and Navigate the Results
HACK
HACK
#35
Modify the Build Output and Navigate the
Results Hack #35
Find out how to add your own information into the build output, as well as get
more out of the standard results by jumping directly to source of the problem.
The build output window is very important to developers. It tells us when
we have errors and warnings in our builds, provides a means of tracking
those issues down, and gives us a view into the progress of the build process.
Modifying the Build Output Results
You have the capability of modifying the output of the build results to pro-
vide some extra information. All you need to do is tap into the build events
[Hack #34] and add your own output. For example, a failed build of a project
looks like the screen shown in Figure 4-27.
If you use the hack to stop a build process when one of the project builds
fail
[Hack #34], then on a large solution build, you’ll want to know exactly
which project is causing the build to die. Starting with the code from
“Customize Your Project Build Process” [Hack #34], the code in the Macro
Explorer looks like this:
Figure 4-27. Failed build output
Modify the Build Output and Navigate the Results #35
Chapter 4, Customizing Visual Studio
|
139
HACK
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")
End If
End Sub
You want to add some code to add a little more information into the output
window. To do this, you simply need to add the following lines of code:
Dim win As Window = _
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim OW As OutputWindow = CType(win.Object, OutputWindow)
OW.OutputWindowPanes.Item("Build").OutputString( _
String.Format( _
"Build Stopped with a failure on the {0} project. {1}", _
Project, _
System.Environment.NewLine))
This code gets a reference to the output windows, then the Build output
windows specifically. It then uses the
OutputString method to write out a
formatted string indicating that the build failed and on what project.
The default Visual Studio settings will open the Task List
window when a build fails. This action usually hides the out-
put window completely, so you will need to first close the
Task List window to see the output or configure Visual Stu-
dio to not automatically open the task list through Tools
Options Project and Solutions “Show Task List win-
dow if build finishes with errors”.
The final code looks like this:
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
Dim win As Window = _
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim OW As OutputWindow = CType(win.Object, OutputWindow)
OW.OutputWindowPanes.Item("Build").OutputString( _
String.Format( _

Get Visual Studio Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.