post compile file increment (BMK NG)?

BlitzMax Forums/BlitzMax NG/post compile file increment (BMK NG)?

ima747(Posted 2010) [#1]
How would I go about incrementing a number in a file in a post compile script?

Basically I want to track compile versions automatically so I don't have to update a const in my code every time I send a new version to my testers, so the logs they send back will be tagged so I know if they're using an out of date version etc.

If I increment a file I could just incbin it and then read it to get the number... sort of roundabout but should work, just need to read a number from a file, add one, and save in the post script (the file will be prepared for the next compile that way)


Brucey(Posted 2010) [#2]
How would I go about incrementing a number in a file in a post compile script?

Well, "post" compile isn't really the right place for this kind of thing, so it really wants to do it as part of the compilation step itself - or at least before the build.

The following is one way of doing it. This uses the "inline" scripting functionality of BMK(NG).

The example application :
SuperStrict


'
' @bmk include file_increment.bmk
' @bmk doIncrement version.txt
'
Incbin "version.txt"

Print "Version = " + LoadText("incbin::version.txt")


The file version.txt has one line in it, with a number. This number will increment every time this file is compiled.

The interesting bit of the application are those two comment lines which begin with @bmk. These are "pragmas" which allow you to perform scripting as part of the compilation. The script parts are executed before the file is compiled.
The first line includes an external script file.
The second line runs a command, passing it a parameter.

Let's take a look at the script file, file_increment.bmk

@define doIncrement
	file = io.open(arg1, "r")
	version = file:read()
	file:close()
	
	version = version + 1
	
	file = io.open(arg1,"w")
	file:write(version)
	file:close()
@end


The @define creates a new function called "doIncrement".
Everything between @define and @end is Lua. (more or less).
Function arguments are available as arg0, arg1, etc... where arg0 is all the arguments as one big string, and arg1 is the first argument, etc.
So, the function doIncrement expects that the first argument is a filename.
It loads the first line of the file, and increments the number. Then it saves that number back into the file.

There is actually a lot more build integration available through the scripting, which I think I've documented in a previous post. But most functionality is limited only by your imagination...

NOTE : you will need the latest BMK NG(2.09) which has a fix for loading local script files properly.


Brucey(Posted 2010) [#3]
Oh.. and of course some example output :

> ./bmk -v
bmk 2.09 mt-macos-x86 / gcc 40201


Building test
Compiling:test.bmx
Linking:test.debug
Executing:test.debug
Version = 7

Process complete


Building test
Compiling:test.bmx
Linking:test.debug
Executing:test.debug
Version = 8

Process complete


Building test
Executing:test.debug
Version = 8

Process complete


Notice that the version bump only occurs when the source file is recompiled...


ima747(Posted 2011) [#4]
I recall posting this, but never saw the answer until I stumbled on it from another thread a year later apparently... just needed to dredge it up to say thanks! :0)


Czar Flavius(Posted 2011) [#5]
What's this! I never knew about this! What other secret commands are there???? Is there anything which could solve the custom compiler directive problem in the other thread?


Brucey(Posted 2011) [#6]
There's no other secret commands :-p
It's just a facility that allows you to make the build system more scriptable using Lua.

I mostly use it for post-build scripting - i.e. copying shared libraries into OS X application bundles, installing icons, etc.
Because I'm so lazy, I don't like to do all those things manually *every* time I go through a build/release process. Computers are meant to help us ;-)


Is there anything which could solve the custom compiler directive problem in the other thread?


No. The inline Lua and .bmk scripts are ignored by the BlitzMax compiler.

You'd have to pre-process the source and output an intermediate source which you then pass to the compiler...