Code archives/File Utilities/AutoVersion

This code has been declared by its author to be Public Domain code.

Download source code

AutoVersion by BlitzSupport2012
This is a really simple/hacky system for automatically updating a program's minor version number, assuming 0.x, 1.x, 1.xx, 1.xxx, etc. It defaults to 0.1, then, for every DEBUG run, it updates to 0.2, 0.3, etc. Release builds will keep the last Debug build number.

The basic principle is that, for each Debug build, GetVersion () will retrieve and then update the minor version number; you can call GetVersion () as often as you like in your program but it'll only update the version once. After building a Release distribution, you can remove "autoversion.txt", as it's Incbin'd with the latest version number. (Release builds won't try and create/update it.)

See the comments in the code for details/excuses, and if you want anything more complex than "1.x" versions (eg. "1.0.1.5"), I strongly recommend re-writing with the general principle in mind, ie. using Incbin and ?Debug to read current version/write next version.

To try it, create a blank text file next to your main source file, called "autoversion.txt" and copy/paste the code as shown.

To read the current version string (and, importantly, update it), just call GetVersion () (even if you just discard the result).

The contents of "autoversion.txt" will (well, should) always reflect the version to be used in the next Debug build. Because Release builds will pick that up upon building, they subtract one from the minor version contained within the Incbin'd file.

At least, that's the theory. It's best not to think about it, and, if in doubt, re-write it.

I have a vague feeling there's something stupid about this, but it seems OK... try it and see!
SuperStrict

' TO USE:

' Create a blank text file called "autoversion.txt" in your
' program's development folder.

' IF PROGRAM FAILS TO BUILD, YOU PROBABLY DIDN'T DO THIS! ^^^

' Call GetVersion () to retrieve version string and update it for next Debug run.

' IN DEBUG MODE ONLY, this will update "autoversion.txt" to
' the NEXT minor version, which will be used next time you run.

' VERSION NUMBER WILL BE ONE LESS THAN "autoversion.txt" FOR RELEASE BUILDS;
' this is expected. Defaults to 0.1 for first Debug run, even if already run
' in Release mode (which will show as 0.1).

' To bump major versions, edit "autoversion.txt" manually. This just
' deals with incremental updates.

' *** You DON'T need to include "autoversion.txt" with your release, as
' it will be included in the binary! ***

' The release version won't create/update an "autoversion.txt" file!

' If it all goes pear-shaped, type a version manually into
' "autoversion.txt"...

' Versions are in the format x.y (y will increase to upper Int range); you'll
' just have to adapt the principle for any other format!

' No serious error-checking, so don't put junk into "autoversion.txt". Valid
' example contents (no more than one line):

' 0.1
' 1.1
' 1.144
' 10.091
' 100.1
' [BLANK FILE]

' -----------------------------------------------------------------------------
' COPY AND PASTE FROM HERE...
' -----------------------------------------------------------------------------

Incbin "autoversion.txt" ' Probably needs to be at top of your code...

Function GetVersion:String ()

	Global AutoVersionSaved:Int = False

	Local version:String = LoadText ("incbin::autoversion.txt")
	Local major:String, minor:String
	
	Local dot:Int = Instr (version, ".")
	
	If dot
		
		major = Left (version, dot - 1)
		minor = Mid (version, dot + 1)
		
		' Hack for Debug Build -> Release Build (Release would
		' use next minor version despite no changes)...
		
		?Not Debug
			minor = Int (minor) - 1
		?
		
	Else
		' Blank/invalid...
		major = "0"
		minor = "1"
	EndIf

	Local autoversion:String = major + "." + minor
	
	?Debug
	
		If Not AutoVersionSaved

			minor = Int (minor) + 1
			
			version = major + "." + minor
			SaveText version, "autoversion.txt"
		
			AutoVersionSaved = True
	
		EndIf
	
	?

	Return autoversion

End Function

' -----------------------------------------------------------------------------
' ... TO HERE.
' -----------------------------------------------------------------------------

Notify "Amazing Apps presents iAyeCapn [Version: " + GetVersion () + "]"

' Uncomment below to confirm that GetVersion only updates version once per run!

'Notify "Amazing Apps presents iAyeCapn [Version: " + GetVersion () + "]"
'Notify "Amazing Apps presents iAyeCapn [Version: " + GetVersion () + "]"

Comments

None.

Code Archives Forum