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?
It's possible to implement that as a reusable subroutine step:
Update the repository if needed.
Use a "Subversion" action with the following settings:
Path = %PROJDIR%
Subcommand = update
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"
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)
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:
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:
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
Save the temporary macro
Use a "Set macro" action with the following settings:
Name = REVISION
Value = %GLOBAL_REV%