@@ -61,7 +61,7 @@ dtutils.libdoc.functions["check_min_api_version"] = {
61
61
62
62
function dtutils .check_min_api_version (min_api , script_name )
63
63
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
65
65
dt .print_error (" This application is written for lua api version " .. min_api .. " or later." )
66
66
dt .print_error (" The current lua api version is " .. current_api )
67
67
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"] = {
96
96
97
97
function dtutils .check_max_api_version (max_api , script_name )
98
98
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
100
100
dt .print_error (" This application is written for lua api version " .. max_api .. " or earlier." )
101
101
dt .print_error (" The current lua api version is " .. current_api )
102
102
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)
427
427
return uuid
428
428
end
429
429
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
+
430
467
return dtutils
0 commit comments