Ok… I was really sick of incrementing the build number, each time that I built my project. Also, I know that I could use the NAnt task to increment build numbers, but this project does not have NAnt configuration files, and I don’t feel like writing them just to increment the build number. Since no one else has apparently created one of these for Visual Studio .NET C# projects, I hacked together this macro to do it autonomously. It basically opens up all projects in the solution (you can change this to only the active projects) and tries to open the corresponding AssemblyInfo.cs file in each project. You can specify which field you would like to increment, also. To install, do the following:
- Tools->Macros->New Macro Project
- Call it whatever you want. Example BuildMacros
- Alt-F11 (Opens up the Macros IDE)
- Copy and paste the macro code below, in the Module1 code.
- Rename Module1 macro to BuildIncrementer
- Open up EnvironmentEvents macro under BuildMacros (Or whatever you named your macro project)
- Add the following under the Automatically generated code region in the EnvironmentEvents macro
Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
- IncrementBuildNumber()
End Sub
This is the macro:
Imports System.Diagnostics
Public Module BuildIncrementer
- ‘ From http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q237/8/70.ASP&NoWebContent=1
Function GetProjectDir(ByVal FullName)
Dim proj_path proj_path = Split(StrReverse(FullName), "", -1, 1)
Dim count
count = UBound(proj_path)
Dim full_path full_path = ""Dim i
For i = 1 To countfull_path = full_path & "" & proj_path(i)
Next
GetProjectDir = StrReverse(full_path)
End Function
‘ objSel- Represents the TextSelection object highlighting the version string to increment
‘ count- Represents the position, in the version string, to be incremented
‘ incrementBy- Represents a number to increment the version number by
Sub ReplaceText(ByVal objSel As TextSelection, ByVal count As Integer, ByVal incrementBy As Integer)
- Dim strTemp As String() = Nothing
Dim strTemp2 As String = ""
Dim i As Integer = 0
strTemp = objSel.Text.Split(".")
Dim s As String
For i = 1 To strTemp.Length – 1
- If i = count Then
- Dim oldValue As Integer = strTemp.GetValue(i)
Dim newValue As Integer
newValue = oldValue + incrementBy
Dim strNewValue As String = newValue
strTemp.SetValue(strNewValue, i)
Exit For
End If
Next i
objSel.Text = Join(strTemp, ".")
End Sub
‘ This event will be triggered after every build of a project
‘ You can modify the code below to only update projects that are active
‘ It currently will scan all projects in the solution for AssemblyInfo.cs files
‘ to update.
Sub IncrementBuildNumber()
- ‘Comment the follow 3 lines, if you want the build number to increment even if the build fails
If DTE.Solution.SolutionBuild.LastBuildInfo() <> 0 Then
- Exit Sub
End If
‘ Change this, if you would only like to modify the AssemblyInfo file in active project files
‘ For Each proj As Project In DTE.ActiveSolutionProjects
For Each proj As Project In DTE.Solution.Projects
- Dim full_path
full_path = GetProjectDir(proj.FullName)
‘ Attempt to open the AssemblyInfo.cs file
full_path = full_path & "AssemblyInfo.cs"
Try
- DTE.ItemOperations.OpenFile(full_path).Activate()
Dim activeDoc As Document = DTE.ActiveDocument
Dim objSelection As TextSelection
objSelection = DTE.ActiveDocument.Selection
Dim objStartPosition As Integer
‘ Get the text between AssemblyVersion(" and ")
objSelection.FindText("AssemblyVersion(""")
objStartPosition = objSelection.BottomPoint.DisplayColumn
Dim objEndPosition As Integer
objSelection.FindText(""")]")
objEndPosition = objSelection.AnchorPoint.DisplayColumn
‘ Get back to after the AssemblyVersion" part
objSelection.StartOfLine()
objSelection.FindText("AssemblyVersion(""")
objSelection.Collapse()
objSelection.MoveToDisplayColumn(0, objEndPosition, True)
ReplaceText(objSelection, 3, 1)
activeDoc.Close(vsSaveChanges.vsSaveChangesYes)
Catch
End Try
Next
End Sub
End Module