VersionCompleteXXXX

From NSIS Wiki
Jump to navigationJump to search
Author: ChrisMorgan (talk, contrib)


Description

There are a few scripts out there to find version numbers with !searchparse or !exec or a helper NSIS script compiled before hand, but I haven't found any to turn a version number like 0.47 into an X.X.X.X version number - 0.47.0.0 - such as is required for VIProductVersion and VIAddVersionKey (File|Product)Version.

The closest I'd come across wasn't in NSIS. It was in a line of code I wrote for BPBible's make_release.py script:

".".join((version.split(".") + ["0", "0", "0"])[:4])

But that didn't help with this because I didn't want to use a Python script, I wanted to keep it NSIS. So I wrote one myself for the Inkscape installer and decided it could be useful elsewhere, so here it is.

The Script

; See http://nsis.sourceforge.net/VersionCompleteXXXX for documentation
!macro VersionCompleteXXXRevision _INPUT_VALUE _OUTPUT_SYMBOL _REVISION
	!searchparse /noerrors ${_INPUT_VALUE} "" _VERSION_1 "." _VERSION_2 "." _VERSION_3 "." _VERSION_4
	!ifndef _VERSION_1
		!define _VERSION_1 0
	!else if `${_VERSION_1}` == ``
		!define /redef _VERSION_1 0
	!endif
	!ifndef _VERSION_2
		!define _VERSION_2 0
	!else if `${_VERSION_2}` == ``
		!define /redef _VERSION_2 0
	!endif
	!ifndef _VERSION_3
		!define _VERSION_3 0
	!else if `${_VERSION_3}` == ``
		!define /redef _VERSION_3 0
	!endif
	!ifndef _VERSION_4
		!define _VERSION_4 0
	!else if `${_VERSION_4}` == ``
		!define /redef _VERSION_4 0
	!endif
	!define ${_OUTPUT_SYMBOL} ${_VERSION_1}.${_VERSION_2}.${_VERSION_3}.${_REVISION}
	!undef _VERSION_1
	!undef _VERSION_2
	!undef _VERSION_3
	!undef _VERSION_4
	!undef _REVISION
!macroend
!define VersionCompleteXXXRevision `!insertmacro VersionCompleteXXXRevision`
!macro VersionCompleteXXXX _INPUT_VALUE _OUTPUT_SYMBOL
	!searchparse /noerrors ${_INPUT_VALUE} "" _VERSION_1 "." _VERSION_2 "." _VERSION_3 "." _VERSION_4
	!ifndef _VERSION_1
		!define _VERSION_1 0
	!else if `${_VERSION_1}` == ``
		!define /redef _VERSION_1 0
	!endif
	!ifndef _VERSION_2
		!define _VERSION_2 0
	!else if `${_VERSION_2}` == ``
		!define /redef _VERSION_2 0
	!endif
	!ifndef _VERSION_3
		!define _VERSION_3 0
	!else if `${_VERSION_3}` == ``
		!define /redef _VERSION_3 0
	!endif
	!ifndef _VERSION_4
		!define _VERSION_4 0
	!else if `${_VERSION_4}` == ``
		!define /redef _VERSION_4 0
	!endif
	!define ${_OUTPUT_SYMBOL} ${_VERSION_1}.${_VERSION_2}.${_VERSION_3}.${_VERSION_4}
	!undef _VERSION_1
	!undef _VERSION_2
	!undef _VERSION_3
	!undef _VERSION_4
!macroend
!define VersionCompleteXXXX `!insertmacro VersionCompleteXXXX`

Usage

${VersionCompleteXXXX} version_number output_symbol
${VersionCompleteXXXRevision} version_number output_symbol revision_number

version_number is a string like "0.47", "1.0", "3.04", "7.2.1.1433", "2".

output_symbol is the symbol which will be set (so if you provide "VERSION_4_PART", ${VERSION_4_PART} will be defined).

revision_number is a special number which can be used to override the fourth number in the version number (the revision numeral); this can be useful in conjunction with !searchparse. See the example for more details.

This is different from most scripts in that it is a compile-time macro, and not runtime. I may do a runtime version of it at some point but for now it's just compile-time (which is much easier, thanks to !searchparse).

Examples

Both of these examples there is a file next to the installer version.ini with a line "version=4.1.6".

Example of ${VersionCompleteXXXX}

This example is just the "simpler" functionality of this script.

!searchparse /file version.ini `version=` VERSION_SHORT
;${VERSION_SHORT} = 4.1.6
${VersionCompleteXXXX} ${VERSION_SHORT} VERSION_FOUR
;${VERSION_FOUR} = 4.1.6.0
VIProductVersion                ${VERSION_FOUR}
VIAddVersionKey  FileVersion    ${VERSION_FOUR}
VIAddVersionKey  ProductVersion ${VERSION_FOUR}

Example of ${VersionCompleteXXXRevision}

This second example demonstrates VersionCompleteXXXRevision. For this example, the NSIS installer is in a Bazaar branch, two directories down from the repository root.

..\..\.bzr\branch\last-revision is a file with one line of the format "123456 example@example.com-20091222141015-wd0gxu55vf0p78fi", and we want to get the number 123456 out as the revision number.

!searchparse /noerrors /file ..\..\.bzr\branch\last-revision "" BZR_REVISION " "
;${BZR_REVISION} = 123456
!searchparse /file version.ini `version=` VERSION_SHORT
;${VERSION_SHORT} = 4.1.6
${VersionCompleteXXXRevision} ${VERSION_SHORT} VERSION_FOUR ${BZR_REVISION}
;${VERSION_FOUR} = 4.1.6.123456
VIProductVersion                ${VERSION_FOUR}
VIAddVersionKey  FileVersion    ${VERSION_FOUR}
VIAddVersionKey  ProductVersion ${VERSION_FOUR}

Increment installer revision

If you wanted to, you could have the installer increment its own revision number, so the first time you build it it'll be 1.0.0.1, then the next, 1.0.0.2, etc. Make sure you create installer-revision.txt with the text "0" in it for the first time.

!define /file REVISION_LAST installer-revision.txt
!define /math REVISION ${REVISION_LAST} + 1
!delfile installer-revision.txt
!appendfile installer-revision.txt ${REVISION}
!searchparse /file version.ini `version=` VERSION_SHORT
${VersionCompleteXXXN} ${VERSION_SHORT} VERSION_FOUR ${REVISION}