Thursday, December 17, 2009

Use svn revision number for assembly version

If you want to use svn revision number as revision number of assembly version this post can be helpful.
I have used information from and to create macro for this.

Here is the macro:

Public Sub UpdateRevisionNumber()

        Dim proj As Project

        Dim objProj As Object()

        objProj = DTE.ActiveSolutionProjects


        If objProj.Length = 0 Then

            Exit Sub

        End If

        proj = DTE.ActiveSolutionProjects(0) 'gets the path of the web project to write to the web.config

        Dim x As String = proj.ProjectItems.ContainingProject.FullName


        Dim p As New System.Diagnostics.Process

        'first update the root to get the latest revision

        p.StartInfo.UseShellExecute = False

        p.StartInfo.RedirectStandardOutput = True

        p.StartInfo.RedirectStandardError = True

        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

        p.StartInfo.FileName = "svn.exe"

        p.StartInfo.WorkingDirectory = proj.Properties.Item("FullPath").Value()

        p.StartInfo.Arguments = "info"


        Dim strerror As String = p.StandardError().ReadToEnd()

        Dim output As String = p.StandardOutput().ReadToEnd()


        'get the revision

        Dim re As New Regex("Revision:\s+(\d+)\s*")

        Dim ver As String

        ver = re.Match(output).Groups(1).Value

        'add one so the number is not one revision behind

        Dim version As Integer

        version = Convert.ToInt32(ver) + 1


        Dim propPath As String

        propPath = proj.Properties.Item("FullPath").Value() + "Properties\AssemblyInfo.cs"


        Dim globalAssemblyContent As String = File.ReadAllText(propPath)

        Dim rVersionAttribute As Regex = New Regex("\[[\s]*(\/\*[\s\S]*?\*\/)?[\s]*assembly[\s]*(\/\*[\s\S]*?\*\/)?[\s]*:[\s]*(\/\*[\s\S]*?\*\/)?[\s]*AssemblyVersion[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\([\s]*(\/\*[\s\S]*?\*\/)?[\s]*\""([0-9]+)\.([0-9]+)(.([0-9]+))?(.([0-9]+))?\""[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\)[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\]")

        Dim rVersionInfoAttribute As Regex = New Regex("\[[\s]*(\/\*[\s\S]*?\*\/)?[\s]*assembly[\s]*(\/\*[\s\S]*?\*\/)?[\s]*:[\s]*(\/\*[\s\S]*?\*\/)?[\s]*AssemblyFileVersion[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\([\s]*(\/\*[\s\S]*?\*\/)?[\s]*\""([0-9]+)\.([0-9]+)(.([0-9]+))?(.([0-9]+))?\""[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\)[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\]")


        'Find Version Attribute for Updating Build Number

        Dim mVersionAttributes As MatchCollection = rVersionAttribute.Matches(globalAssemblyContent)

        Dim mVersionAttribute As Match = GetFirstUnCommentedMatch(mVersionAttributes, globalAssemblyContent)

        Dim gBuildNumber As Group = mVersionAttribute.Groups(11)

        Dim newBuildNumber As String


        'Replace Version Attribute for Updating Build Number

        If (gBuildNumber.Success) Then

            newBuildNumber = version.ToString()

            globalAssemblyContent = globalAssemblyContent.Substring(0, gBuildNumber.Index) + newBuildNumber + globalAssemblyContent.Substring(gBuildNumber.Index + gBuildNumber.Length)

        End If


        'Find Version Info Attribute for Updating Build Number

        Dim mVersionInfoAttributes As MatchCollection = rVersionInfoAttribute.Matches(globalAssemblyContent)

        Dim mVersionInfoAttribute As Match = GetFirstUnCommentedMatch(mVersionInfoAttributes, globalAssemblyContent)

        Dim gBuildNumber2 As Group = mVersionInfoAttribute.Groups(11)


        'Replace AssemblyFileVersion

        If (gBuildNumber2.Success) Then

            If String.IsNullOrEmpty(newBuildNumber) Then

                newBuildNumber = version.ToString()

            End If


            globalAssemblyContent = globalAssemblyContent.Substring(0, gBuildNumber2.Index) + newBuildNumber + globalAssemblyContent.Substring(gBuildNumber2.Index + gBuildNumber2.Length)

        End If


        File.WriteAllText(propPath, globalAssemblyContent)

    End Sub



    Private Function GetFirstUnCommentedMatch(ByRef mc As MatchCollection, ByVal content As String) As Match

        Dim rSingleLineComment As Regex = New Regex("\/\/.*$")

        Dim rMultiLineComment As Regex = New Regex("\/\*[\s\S]*?\*\/")


        Dim mSingleLineComments As MatchCollection = rSingleLineComment.Matches(content)

        Dim mMultiLineComments As MatchCollection = rMultiLineComment.Matches(content)


        For Each m As Match In mc

            If m.Success Then

                For Each singleLine As Match In mSingleLineComments

                    If singleLine.Success Then

                        If m.Index >= singleLine.Index And m.Index + m.Length <= singleLine.Index + singleLine.Length Then

                            GoTo NextAttribute

                        End If

                    End If



                For Each multiLine As Match In mMultiLineComments

                    If multiLine.Success Then

                        If m.Index >= multiLine.Index And m.Index + m.Length <= multiLine.Index + multiLine.Length Then

                            GoTo NextAttribute

                        End If

                    End If



                Return m

            End If




        Return Nothing


    End Function

1 comment:

Rocci said...

This ended up helping me BIG time with a side project of mine. Thanks!