Arduino IDE compilation with auto-incremental versioning

Hello,

This topic explain how i've add automatic incremental versioning directly inside a sketch.
I'm using Arduino IDE 1.8.9 in Windows 10 OS environment, so i use Microsoft VBscript to add this feature.

First, thanks to Vurdalakov for the first step to this idea (CodeBlog: Automatically increment version number in Arduino IDE).

Next, below the explanations how add this feature to Arduino IDE:

  1. Locate platform.txt file and add it this line :

recipe.hooks.prebuild.0.pattern=cscript.exe "{runtime.ide.path}/incrementalversion.vbs" "{build.source.path}" "{build.project_name}" "SKETCH_VERSION"

# ESP8266 platform
# ------------------------------

# For more info:
# https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification

name=ESP8266 Boards (2.5.1)
version=2.5.1

# These will be removed by the packager script when doing a JSON release

recipe.hooks.prebuild.0.pattern=cscript.exe "{runtime.ide.path}/incrementalversion.vbs" "{build.source.path}" "{build.project_name}" "SKETCH_VERSION"

runtime.tools.signing={runtime.platform.path}/tools/signing.py
runtime.tools.elf2bin={runtime.platform.path}/tools/elf2bin.py
...
  1. Create the incrementalversion.vbs file in the Arduino IDE folder installation (C:\Program Files (x86)\Arduino):
Const ForReading = 1, ForWriting = 2
Dim FSO, FileINO, strText, strs, build, i

Set FSO=CreateObject("Scripting.FileSystemObject")

Set FileINO=FSO.OpenTextFile(WScript.Arguments.Item(0) & "\" & WScript.Arguments.Item(1), ForReading)
strText=FileINO.ReadAll
FileINO.Close

If  InStr(1, strText, "#define ", vbTextCompare) > 0 _
and InStr(1, strText, WScript.Arguments.Item(2), vbTextCompare) > 0 Then
	Set FileINO=FSO.OpenTextFile(WScript.Arguments.Item(0) & "\" & WScript.Arguments.Item(1), ForWriting, True)'
	strs=Split(replace(strText,vbcrlf,vbcr),vbcr)
	For i=Lbound(strs) to Ubound(strs)
		If  InStr(1, strs(i), "#define ", vbTextCompare) > 0 _
		and InStr(1, strs(i), WScript.Arguments.Item(2), vbTextCompare) > 0 Then
			Wscript.Echo "Current: " & strs(i)
			build=Split(Split(strs(i),"""")(1),".")(2)
			strs(i)=replace(strs(i),"." & build & chr(34), "." & build+1 & chr(34))
			Wscript.Echo "Next   : " & strs(i)
		End If
		FileINO.write strs(i) & vbcrlf
	Next
	FileINO.Close
End If
  1. Now the automatic incremental versioning new feature is ready to be used !

  2. To use it you need to add this define in your sketch :
    #define SKETCH_VERSION "1.0.0"

Version string must consist of 3 numbers separated by dots, e.g. 1.0.2. You could modify the two first numbers manually as you want. The last number is a build number that is automatically incremented by the VBS script.

Sketch Example :

#include <Arduino.h>

#define SKETCH_VERSION "1.0.2"
//                      | | |_______ Build version : Automatic control
//                      | |_________ Minor Version : Human control
//                      |___________ Major Version : Human control

void setup() {
  Serial.print ("Version: " SKETCH_VERSION);
}

void loop() {}
  1. Now, each time you will compile your sketch, the current SKETCH_VERSION will appear in your application.
    The compilation will increment the build number in your INO file for the next modification.

IMPORTANT : After the compilation, Arduino IDE couldn't automatically reload the sketch modified in background by this script, so you must first click in menu Tools the command Fix Encoding & Reload and now you could see the next SKETCH_VERSION updated for the next modification of your sketch.

That's all ! I hope this feature could help others.
Patrick

Thanks so much for sharing this information! This sort of thing has been discussed several times here on the Forum over the years, but this is the first time I can remember seeing a fully documented solution presented.

I have a couple of suggestions:

BRIOT_Patrick:

#define SKETCH_VERSION "1.0.2"

//                      | | |_______ Build version : Automatic control
//                      | |_________ Minor Version : Human control
//                      |___________ Major Version : Human control

I recommend following the semver specification in your versioning:

Build metadata MAY be denoted by appending a plus sign and a series of dot separated identifiers immediately following the patch or pre-release version. Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty. Build metadata SHOULD be ignored when determining version precedence. Thus two versions that differ only in the build metadata, have the same precedence. Examples: 1.0.0-alpha+001, 1.0.0+20130313144700, 1.0.0-beta+exp.sha.5114f85.

So it would look like this:

#define SKETCH_VERSION "1.0.2+1"
//                      | | | |______Build version : Automatic control
//                      | | |_______ Patch version : Human control
//                      | |_________ Minor Version : Human control
//                      |___________ Major Version : Human control

BRIOT_Patrick:
5. Now, each time you will compile your sketch, the current SKETCH_VERSION will appear in your application.
The compilation will increment the build number in your INO file for the next modification.

In order for this to work, the sketch must be saved before each compilation. There is a preference option (File > Preferences > Save when verifying or uploading), that is on by default, but I normally find that behavior annoying and so always have it off. For this reason, it would be a good idea to mention that the sketch must be saved (either automatically or manually) before compiling/uploading.

BRIOT_Patrick:
After the compilation, Arduino IDE couldn't automatically reload the sketch modified in background by this script, so you must first click in menu Tools the command Fix Encoding & Reload and now you could see the next SKETCH_VERSION updated for the next modification of your sketch.

There is a feature that will make this step no longer necessary in the works:

That feature is already available in the Arduino IDE Beta Build:

Hello all,

A new version solving unwanted new blank line at the end of INO file and write file only if revision discovered in existing INO file.

WARNING : You can use this code only after removing the space on "c hr(34)" word (2 times). If i want to publish my code as is, it is considered as a virus and blocked by arduino forum, i don't know why. That's why i've done this workaround to publish it.

Const ForReading = 1, ForWriting = 2
Dim FSO, FileINO, strText, strs, build, rev, i

Set FSO=CreateObject("Scripting.FileSystemObject")

Set FileINO=FSO.OpenTextFile(WScript.Arguments.Item(0) & "\" & WScript.Arguments.Item(1), ForReading)
strText=FileINO.ReadAll
FileINO.Close

rev=False
strs=Split(replace(strText,vbcrlf,vbcr),vbcr)
For i=Lbound(strs) to Ubound(strs)-2
	If  InStr(1, strs(i), "#define ", vbTextCompare) > 0 _
	and InStr(1, strs(i), WScript.Arguments.Item(2), vbTextCompare) > 0 Then
		Wscript.Echo "Current: " & strs(i)
		build=Split(Split(strs(i),"""")(1),".")(2)
		strs(i)=replace(strs(i),"." & build & c hr(34), "." & build+1 & c hr(34))
		Wscript.Echo "Next   : " & strs(i)
		rev = True
	End If
Next

If (rev) Then
	Set FileINO=FSO.OpenTextFile(WScript.Arguments.Item(0) & "\" & WScript.Arguments.Item(1), ForWriting, True)
	FileINO.write Join(strs,vbcr)
	FileINO.Close
End If

Best regards.
Patrick

BRIOT_Patrick:
Hello,

  1. Locate platform.txt file and add it this line :

recipe.hooks.prebuild.0.pattern=cscript.exe "{runtime.ide.path}/incrementalversion.vbs" "{build.source.path}" "{build.project_name}" "SKETCH_VERSION"

Hi looks very good.

I found differnt platform.txt file.

Which folder has it to be?

thanks T

riker1:
I found differnt platform.txt file.

Which folder has it to be

The tricky thing is that each hardware core has its own platform.txt file. You need to modify the platform.txt that is associated with the board you have selected from the Tools > Board menu.

The easiest way to find the active hardware package location is as follows:

  • Select a board from the hardware package you want to find from the Tools > Board menu
  • File > Examples > SPI > BarometricPressureSensor (or any other SPI example sketch)
  • Sketch > Show Sketch Folder
  • Move up folder levels until you reach the one that contains platform.txt

Hi

thanks for the explanation.

As I use different Boards I have to do it for every board.

Will this work ?

Thanks

After reading different topics and the need for a build number, I just implemented a script to implement semenatic versioning 2.0. Aim was to simply increment build number, data and time automatically pre-build and the major, minor and patch level manually.

Find the script here:

Enjoy
Robert