Sign in to follow this  
  • entries
    102
  • comments
    146
  • views
    79725

Build Number Incrementing Macro

Sign in to follow this  
Black Knight

465 views

I finally managed to put together a macro for incrementing a build number each time a project is build on release config.It increments a number in a *.ver file then uses it inside a resource script to add it to the applications version resource but that is not really necessary.It also writes a log file for each project in the solution basically the build number and the time of the build.
This macro is generally compiled from different resources over the web I just made small modifications to it so that it can work with multiple projects in a solution.

Here is the macro source incase any1 wants to use it :
This is the event handler for the BuildEvents_OnBuildProjConfigBegin event.

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
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


Private Sub BuildEvents_OnBuildProjConfigBegin(ByVal Project As String,
ByVal ProjectConfig As String,
ByVal Platform As String, ByVal SolutionConfig As String)
Handles BuildEvents.OnBuildProjConfigBegin
' ------------------------------------
' OnBuildProjConfigBegin event handler
' ------------------------------------

' abort if build type is debug

If InStr(1, ProjectConfig, "Debug", 1) Then Exit Sub

' get ver filename

Dim res_filename As String

res_filename = Path.GetDirectoryName(DTE.Solution.FullName) + "\" + Project
res_filename = Path.ChangeExtension(res_filename, ".ver")

' open VERSION FILE and increment build number

Dim msg_text As String

If File.Exists(res_filename) Then

Dim line As String

Try

Dim sr As StreamReader = New StreamReader(res_filename)
line = sr.ReadLine()
sr.Close()

Catch ex As Exception

Module1.WriteToOutputBuildPane(vbCrLf & _
"Version file read failed : " & ex.Message & vbCrLf)

End Try

line = Right(line, line.Length - 18)

Try

Dim sw As StreamWriter = File.CreateText(res_filename)
sw.WriteLine("#define BUILD_NUM {0}", line + 1)
sw.WriteLine("#define STRBUILD_NUM " & Chr(34) & "1, 0, 0, {0}" & Chr(34), line + 1)


sw.Close()

Catch ex As Exception

Module1.WriteToOutputBuildPane(vbCrLf & _
"Version file write failed : " & ex.Message & vbCrLf)

End Try
Dim proj_name As String
proj_name = Right(Project, Project.Length - Project.LastIndexOf("\") - 1)
proj_name = proj_name.Split(".")(0)


msg_text = "[Build Number : " & line + 1 & "-" + proj_name + "], [" & Now + "]"


Module1.WriteToOutputBuildPane(vbCrLf & msg_text & vbCrLf)
Module1.WriteToLogFile(Project, msg_text)

Else

Try

Dim sw As StreamWriter = File.CreateText(res_filename)
sw.WriteLine("#define BUILD_NUM 1")
sw.WriteLine("#define STRBUILD_NUM " & Chr(34) & "1, 0, 0, 1" & Chr(34))

sw.Close()

Catch ex As Exception

Module1.WriteToOutputBuildPane(vbCrLf & _
"Version file write failed : " & ex.Message & vbCrLf)

End Try

msg_text = "Build number : 1, " & Now

Module1.WriteToOutputBuildPane(vbCrLf & msg_text & vbCrLf)
Module1.WriteToLogFile(Project, msg_text)

End If


WriteToLogFile(Project, "-----------------------------------------------------------")
WriteToLogFile(Project, " ")

End Sub

End Module





And these are the functions to write to files.



Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.IO


Public Module Module1
' ----------------------------------
' write text message to a log file
' ----------------------------------

Sub WriteToLogFile(ByVal Project As String, ByVal msg_text As String)

Dim log_filename As String

'log_filename = DTE.Solution.FullName
'log_filename = Path.ChangeExtension(log_filename, ".log.txt")
log_filename = Path.GetDirectoryName(DTE.Solution.FullName) + "\" + Project
log_filename = Path.ChangeExtension(log_filename, ".log.txt")

'res_filename = Path.GetDirectoryName(DTE.Solution.FullName) + "\" + Project
'res_filename = Path.ChangeExtension(res_filename, ".ver")

Try

Dim sw As StreamWriter = File.AppendText(log_filename)
sw.WriteLine(msg_text)
sw.Close()

Catch ex As Exception

MsgBox("Log file write failed : " & ex.Message)

End Try

End Sub




' ----------------------------------------------------------------
' write a text message to the build pane of the output tool window
' ----------------------------------------------------------------

Sub WriteToOutputBuildPane(ByVal msg_text As String)

' Create a tool window handle for the Output window.

Dim win As Window = DTE.Windows.Item(EnvDTE.Constants. _
vsWindowKindOutput)




' Create handles to the Output window and its build pane.

Dim OW As OutputWindow = win.Object
Dim OWp As OutputWindowPane

OWp = OW.OutputWindowPanes.Item("Build")

' Add a line of text to the output pane.

OWp.OutputString(msg_text & vbCrLf)

End Sub


End Module




Sign in to follow this  


1 Comment


Recommended Comments

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now