svnvisual-build-professional

How can I use the SVN revision number in Visual Build?


I want to use the SVN revision number as the 4th digit in my version number. In Visual Build, my version number is defined as

%MAJOR%.%MINOR%.%BUILD%.%REVISION%

and I'd like to populate the %REVISION% variable from SVN.

How can I achieve that in Visual Build?


Solution

  • Option 1: parse the XML

    It's possible to implement that as a reusable subroutine step:

    1. Update the repository if needed.

      Use a "Subversion" action with the following settings:

      Path = %PROJDIR%
      Subcommand = update
      
    2. Store the SVN information into a XML file

      Use a "Run Program" action with the following settings:

      Command = %DOSCMD% svn info --xml > "%PROJDIR%\svninfo.xml"
      
    3. Extract the revision

      Use a "Run Script" action for VBScript and apply the following code:

      dim fso
      Set fso = CreateObject("Scripting.FileSystemObject")
      dim filename
      filename = Application.ExpandMacros("%PROJDIR%\svninfo.xml")
      dim file
      set file = fso.OpenTextFile(filename, 1)
      dim filecontent
      filecontent = ""
      Do While Not file.AtEndOfStream
          filecontent = filecontent + file.readline + vbNewLine
      loop
      file.Close()
      set file = nothing
      set fso = nothing
      
      Builder.LogMessage(filecontent)
      
      ' Load as XML document
      set xmlDoc=CreateObject("Microsoft.XMLDOM")
      call xmlDoc.loadxml(filecontent)
      
      ' Extract information
      dim revision
      set revision = xmlDoc.SelectSingleNode("/info/entry/@revision")
      
      ' Set output variable
      dim out
      out = Application.ExpandMacros("%OUTPUTMACRO%")
      call Application.Macros(vbldMacroTemporary).Add(out, revision.Value)
      
    4. Delete the temporary XML file

      Use a "Delete Files" action with the setting

      Folder = %PROJDIR%
      Include = svninfo.xml
      

    The subroutine should now look like this:

    Visual Build subroutine steps

    In order to use it from your build script, use the "Subroutine call" action and add OUTPUTMACRO with value REVISION. It should look like this:

    Visual Build subroutine usage

    Option 2: parse the command line output

    1. Get the information about the repository

      Use a "Subversion" action with the following settings:

      Subcommand = info
      

      In the script editor for the step, add the following code:

      Sub vbld_StepDone()
          If Step.BuildStatus = vbldStepStatSucceeded Then
              ' parse output for Revision number
              out = vbld_AllMacros()("LASTSTEP_OUTPUT").Value
              pos = InStr(out, "Revision: ")+10
              pos2 = InStr(pos, out, vbCrLf)
              bld_TempMacros.Add "GLOBAL_REV", Mid(out, pos, pos2-pos)
          End If  
      End Sub
      
    2. Save the temporary macro

      Use a "Set macro" action with the following settings:

      Name = REVISION
      Value = %GLOBAL_REV%