-1

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?

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222

2 Answers2

2

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%
    
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
2

There is a command in svn commandline tools for exactly this:

svnversion [path_to_workingcopy]

It returns the current revision number of the working copy (or current dir, if arg is omitted). If working copy is modified, mixed or sparse checked out, it will output this as well. Excerpt from the documentation available via --help cmd-switch:

   4123:4168     mixed revision working copy
   4168M         modified working copy
   4123S         switched working copy
   4123P         partial working copy, from a sparse checkout
   4123:4168MS   mixed revision, modified, switched working copy

So you can just invoke svnversion in your buildscript and either process the output or put it into a file and read this file.

Peter Parker
  • 29,093
  • 5
  • 52
  • 80