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:
- 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
...
- 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
-
Now the automatic incremental versioning new feature is ready to be used !
-
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() {}
- 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