From bdb88e74c5e80ce5a9bd6eea268f11970c6d4962 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 23 Aug 2022 19:29:45 +1000 Subject: [PATCH] fix(version): properly compare minor/patch semver Comparing "2.7.4" to "2.10.1" would compare each number, i.e. 2 = 2, 7 < 10, 4 > 1 Instead compare the major (2 v 2), then minor.patch (7.4 v 10.1). where doka to do my math --- resource/version/server.lua | 10 ++++------ resource/version/shared.lua | 18 ++++++++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/resource/version/server.lua b/resource/version/server.lua index 89bdfddf..6a96d1e6 100644 --- a/resource/version/server.lua +++ b/resource/version/server.lua @@ -19,13 +19,11 @@ function lib.versionCheck(repository) local latestVersion = response.tag_name:match('%d%.%d+%.%d+') if not latestVersion or latestVersion == currentVersion then return end - local cv = { string.strsplit('.', currentVersion) } - local lv = { string.strsplit('.', latestVersion) } + local cMajor, cMinor = string.strsplit('.', currentVersion, 2) + local lMajor, lMinor = string.strsplit('.', latestVersion, 2) - for i = 1, #cv do - if tonumber(cv[i]) < tonumber(lv[i]) then - return print(('^3An update is available for %s (current version: %s)\r\n%s^0'):format(resource, currentVersion, response.html_url)) - end + if tonumber(cMajor) < tonumber(lMajor) or tonumber(cMinor) < tonumber(lMinor) then + return print(('^3An update is available for %s (current version: %s)\r\n%s^0'):format(resource, currentVersion, response.html_url)) end end, 'GET') end) diff --git a/resource/version/shared.lua b/resource/version/shared.lua index c1c1a3af..3d14e457 100644 --- a/resource/version/shared.lua +++ b/resource/version/shared.lua @@ -2,18 +2,16 @@ function lib.checkDependency(resource, minimumVersion, printMessage) local currentVersion = GetResourceMetadata(resource, 'version', 0):match('%d%.%d+%.%d+') if currentVersion ~= minimumVersion then - local cv = { string.strsplit('.', currentVersion) } - local mv = { string.strsplit('.', minimumVersion) } - local msg = ("^1%s requires version '%s' of '%s' (current version: %s)^0"):format(GetInvokingResource() or GetCurrentResourceName(), minimumVersion, resource, currentVersion) - - for i = 1, #cv do - if tonumber(cv[i]) < tonumber(mv[i]) then - if printMessage then - return print(msg) - end + local cMajor, cMinor = string.strsplit('.', currentVersion, 2) + local mMajor, mMinor = string.strsplit('.', minimumVersion, 2) - return false, msg + local msg = ("^1%s requires version '%s' of '%s' (current version: %s)^0"):format(GetInvokingResource() or GetCurrentResourceName(), minimumVersion, resource, currentVersion) + if tonumber(cMajor) < tonumber(mMajor) or tonumber(cMinor) < tonumber(mMinor) then + if printMessage then + return print(msg) end + + return false, msg end end