Skip to content

Commit 067fe29

Browse files
committed
Fix version comparison
Currently this is a textual string comparison which will break with 10.0.0 API version.
1 parent 926272b commit 067fe29

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

lib/dtutils.lua

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ dtutils.libdoc.functions["check_min_api_version"] = {
6161

6262
function dtutils.check_min_api_version(min_api, script_name)
6363
local current_api = dt.configuration.api_version_string
64-
if min_api > current_api then
64+
if dtutils.compare_versions(min_api, current_api) > 0 then
6565
dt.print_error("This application is written for lua api version " .. min_api .. " or later.")
6666
dt.print_error("The current lua api version is " .. current_api)
6767
dt.print("ERROR: " .. script_name .. " failed to load. Lua API version " .. min_api .. " or later required.")
@@ -96,7 +96,7 @@ dtutils.libdoc.functions["check_max_api_version"] = {
9696

9797
function dtutils.check_max_api_version(max_api, script_name)
9898
local current_api = dt.configuration.api_version_string
99-
if current_api > max_api then
99+
if dtutils.compare_versions(current_api, max_api) > 0 then
100100
dt.print_error("This application is written for lua api version " .. max_api .. " or earlier.")
101101
dt.print_error("The current lua api version is " .. current_api)
102102
dt.print("ERROR: " .. script_name .. " failed to load. Lua API version " .. max_api .. " or earlier required.")
@@ -427,4 +427,41 @@ function dtutils.gen_uuid(case)
427427
return uuid
428428
end
429429

430+
dtutils.libdoc.functions["compare_versions"] = {
431+
Name = [[compare_versions]],
432+
Synopsis = [[compare two version strings]],
433+
Usage = [[local du = require "lib/dtutils"
434+
435+
local result = du.compare_versions(version1, version2)
436+
version1 - string - the first version string to compare (example: "5.0.0")
437+
version2 - string - the second version string to compare (example: "5.1.0")]],
438+
Description = [[compare_versions compares two version strings and returns 1 if version1 is greater,
439+
-1 if version2 is greater, and 0 if they are equal.]],
440+
Return_Value = [[result - 1 if version1 is greater, -1 if version2 is greater, 0 if they are equal.]],
441+
Limitations = [[]],
442+
Example = [[compare_versions("5.0.0", "5.1.0") returns -1]],
443+
See_Also = [[]],
444+
Reference = [[]],
445+
License = [[]],
446+
Copyright = [[]],
447+
}
448+
449+
function dtutils.compare_versions(version1, version2)
450+
local v1 = {}
451+
for num in version1:gmatch("%d+") do table.insert(v1, tonumber(num)) end
452+
local v2 = {}
453+
for num in version2:gmatch("%d+") do table.insert(v2, tonumber(num)) end
454+
455+
for i = 1, math.max(#v1, #v2) do
456+
local num1 = v1[i] or 0
457+
local num2 = v2[i] or 0
458+
if num1 > num2 then
459+
return 1
460+
elseif num1 < num2 then
461+
return -1
462+
end
463+
end
464+
return 0
465+
end
466+
430467
return dtutils

0 commit comments

Comments
 (0)