testing presence of ML feature
|
Author |
Message |
mikshaw
Senior Member
   
Posts: 522
Group: Registered
Joined: Apr 2007
Status:
Offline
Reputation: 5
|
testing presence of ML feature
I sometimes write scripts to be supported by multiple versions of murgaLua, for example in the case where I want to utilize a recent addition but don't want the application to break if the user has an older version. What I've done so far is check for the existence of a package that was added with the same version as the desired feature:
if not package.loaded.md5 then
print("This feature requires murgaLua version 0.6 or later")
else
<code for version 0.6>
end
This isn't ideal for more than one reason.
I don't know why it never occurred to me before, but you can use the same concept to check for the specific feature you want to use. For example, if you want to use Fl_Pixmap on 0.6 but prevent a close on 0.5.5 and earlier:
if fltk.Fl_Pixmap then
<code for version 0.6>
end
Of course you'll need to know the path to the specific object you want to test, but you can use this tool to help.
|
|
01-20-2008 05:37 AM |
|
 |
mikshaw
Senior Member
   
Posts: 522
Group: Registered
Joined: Apr 2007
Status:
Offline
Reputation: 5
|
RE: testing presence of ML feature
Another possible test is for a specific version of murgaLua, in the event that you have multiple new features in your script and just don't want to support older versions. You could check for a feature that was added to that specific version, but it's *possible* that sometime in the future that feature could be removed or renamed. You would always need to make sure you use a feature that is specific to your script, rather than just a generic cut-and-paste test. That's not a serious issue, but anything that can reuse existing code makes development go more smoothly.
I couldn't figure out why I couldn't use tonumber(murgaLua.version) for comparison, so I didn't bother with it. Then it struck me that a string with two decimals in it can't be converted into a number, so I decided to do string comparisons instead:
-- find the smallest negative index in the arg table
-- this is the Lua interpreter
for i in pairs(arg) do
if not low or i < low then low=i end
end
lua_interpreter=arg[low]
-- test the murgaLua version
err="This program requires murgaLua 0.5.5 or newer.\nhttp://www.murga-projects.com/murgaLua/index.html"
notallowed={"0.1","0.2","0.3","0.4","0.4.1","0.5"} -- add "0.5.5" if you need 0.6
for k,v in ipairs(notallowed) do
if not murgaLua.version or v == murgaLua.version then
print(lua_interpreter..": "..err) -- print error in case it's tried in regular Lua
fltk.fl_alert(err) -- popup error
os.exit(1)
end
end
This method allows you to simply copy and paste the code reliably into any script that needs at least version 0.5.5
It also uses an additional print() just in case the end user tries to run the script in some other kind of Lua interpreter. I haven't tested this in Lua proper yet, but i think it would exit cleanly just from not finding murgaLua.version
Maybe there are simpler ways to do this, but this at least is more reliable for general use.
|
|
02-07-2008 05:45 AM |
|
 |
Juergen
Member
  
Posts: 81
Group: Registered
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: testing presence of ML feature
I think you are looking for something like that:
do
local iter=string.gmatch(murgaLua.version,"%d+")
local major,minor,sub=tonumber(iter()),tonumber(iter()),tonumber(iter())
if minor<=5 and sub<=5 then print"You must use a murgaLua version > 0.5.5" os.exit(1) end
end
Juergen
|
|
02-07-2008 06:54 AM |
|
 |
Juergen
Member
  
Posts: 81
Group: Registered
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: testing presence of ML feature
Alternatively:
do
local version=0; for x in string.gmatch(murgaLua and murgaLua.version or "0","%d+") do version=version*100+tonumber(x) end
if version<=0505 then print"You must use a murgaLua version > 0.5.5" os.exit(1) end
end
This works also in alternative Lua interpreters. This way you don't have to keep track over all the past murgaLua versions
Juergen
|
|
02-07-2008 08:40 AM |
|
 |
mikshaw
Senior Member
   
Posts: 522
Group: Registered
Joined: Apr 2007
Status:
Offline
Reputation: 5
|
RE: testing presence of ML feature
Yes, that's exactly the result I was originally thinking of, but wasn't sure where to start. Thanks. =o)
I'm definitely saving this for future tests, and putting it in with the rest of the code snippets.
I don't really understand the syntax of "do...end" though. Is that just to keep the variables local?
|
|
02-07-2008 08:44 AM |
|
 |
Juergen
Member
  
Posts: 81
Group: Registered
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: testing presence of ML feature
Yes, that's exactly the result I was originally thinking of, but wasn't sure where to start. Thanks. =o)
I'm definitely saving this for future tests, and putting it in with the rest of the code snippets.
I don't really understand the syntax of "do...end" though. Is that just to keep the variables local?
The do and end delimits a block. Inside you have your own scope. It is basically the same as { } in C, where you can also define new variables.
I used the do .. end only because I wanted to declare the variables local, since it doesn't make sense to have them as globals. Lua doesn't allow you to have local variables in the outermost scope:
> local a=6
> print(a)
nil
which also doesn't make sense.
Of course it would make sense to put it into a function.
function minimal_version(min_version)
local version=0; for x in string.gmatch(murgaLua and murgaLua.version or "0","%d+") do version=version*100+tonumber(x) end
local min_vers=0; for x in string.gmatch(min_version,"%d+") do min_vers=min_vers*100+tonumber(x) end
if version<min_vers then print"You must use a murgaLua version > 0.5.5" os.exit(1) end
end
minimal_version("0.5.5")
Juergen
|
|
02-07-2008 09:07 AM |
|
 |
mikshaw
Senior Member
   
Posts: 522
Group: Registered
Joined: Apr 2007
Status:
Offline
Reputation: 5
|
RE: testing presence of ML feature
Yeah, I had the assumption that local variables could only be used within a function.
I like that last one best. It seems it would be completely reusable without modification, except the print command should use min_version instead of a static "0.5.5" in its string. As a personal preference I'd also change the variable name of min_vers. It's a bit too similar to min_version for quick reading; it took me a few minutes to figure out they were unique.
|
|
02-07-2008 10:13 AM |
|
 |
Juergen
Member
  
Posts: 81
Group: Registered
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: testing presence of ML feature
Yeah, I had the assumption that local variables could only be used within a function.
I like that last one best. It seems it would be completely reusable without modification, except the print command should use min_version instead of a static "0.5.5" in its string. As a personal preference I'd also change the variable name of min_vers. It's a bit too similar to min_version for quick reading; it took me a few minutes to figure out they were unique.
Another version:
function minimal_version(min_version)
local x=string.gmatch((murgaLua and murgaLua.version or "0").." " .. min_version,"%d+")
local t=function(x) return tonumber(x()) end
if t(x)*10000+t(x)*100+t(x)-t(x)*10000-t(x)*100-t(x)<0 then
print("You must use a murgaLua version > "..min_version) os.exit(1) end
end
minimal_version("0.5.6")
Juergen
|
|
02-07-2008 10:52 AM |
|
 |
mikshaw
Senior Member
   
Posts: 522
Group: Registered
Joined: Apr 2007
Status:
Offline
Reputation: 5
|
RE: testing presence of ML feature
It just occurred to me that testing the version string is not going to be as reliable as I'd hoped for a little while. The version string in 0.6 is "0.5.5", so until 0.6.5 it will continue to be a problem for this test. After that, testing for 0.6 will probably need to be explicitly discouraged.
|
|
02-08-2008 12:52 AM |
|
 |
Juergen
Member
  
Posts: 81
Group: Registered
Joined: May 2007
Status:
Offline
Reputation: 0
|
RE: testing presence of ML feature
It just occurred to me that testing the version string is not going to be as reliable as I'd hoped for a little while. The version string in 0.6 is "0.5.5", so until 0.6.5 it will continue to be a problem for this test. After that, testing for 0.6 will probably need to be explicitly discouraged.
Then the function should be changed a little ;-)
function minimal_version(minimal_version)
local x=string.gmatch((murgaLua and murgaLua.version or "0"),"%d+") y=string.gmatch(minimal_version,"%d+")
local t=function(x) return tonumber((x() or 0)) end
local version=0; local min_version=0
for i=1,3 do version=version*100+t(x); min_version=min_version*100+t(y) end
if version<min_version then
print("You must use a murgaLua version > "..minimal_version) os.exit(1) end
end
minimal_version("0.6")
Juergen
|
|
02-08-2008 01:25 AM |
|
 |
|