diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:49:06 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
commit | c3822ad23d588f69931ebc98d870ba2e2e29eece (patch) | |
tree | 77c3ad1eb50e2d417acfddc6a4712aa044f97ee2 | |
parent | b5fc16cf0403e5685ba2d31c4723fa9997b5b5f7 (diff) | |
download | luarocks-c3822ad23d588f69931ebc98d870ba2e2e29eece.tar.gz luarocks-c3822ad23d588f69931ebc98d870ba2e2e29eece.tar.bz2 luarocks-c3822ad23d588f69931ebc98d870ba2e2e29eece.zip |
Teal: convert luarocks.core.vers
-rw-r--r-- | src/luarocks/core/vers.tl (renamed from src/luarocks/core/vers.lua) | 84 |
1 files changed, 44 insertions, 40 deletions
diff --git a/src/luarocks/core/vers.lua b/src/luarocks/core/vers.tl index 8e617984..35dd4bbd 100644 --- a/src/luarocks/core/vers.lua +++ b/src/luarocks/core/vers.tl | |||
@@ -1,11 +1,12 @@ | |||
1 | 1 | local record vers | |
2 | local vers = {} | 2 | end |
3 | 3 | ||
4 | local util = require("luarocks.core.util") | 4 | local util = require("luarocks.core.util") |
5 | local require = nil | 5 | local type Version = require("luarocks.core.types.version").Version |
6 | local type Constraint = require("luarocks.core.types.version").Constraint | ||
6 | -------------------------------------------------------------------------------- | 7 | -------------------------------------------------------------------------------- |
7 | 8 | ||
8 | local deltas = { | 9 | local deltas: {string: integer} = { |
9 | dev = 120000000, | 10 | dev = 120000000, |
10 | scm = 110000000, | 11 | scm = 110000000, |
11 | cvs = 100000000, | 12 | cvs = 100000000, |
@@ -15,7 +16,7 @@ local deltas = { | |||
15 | alpha = -1000000 | 16 | alpha = -1000000 |
16 | } | 17 | } |
17 | 18 | ||
18 | local version_mt = { | 19 | local version_mt: metatable<Version> = { |
19 | --- Equality comparison for versions. | 20 | --- Equality comparison for versions. |
20 | -- All version numbers must be equal. | 21 | -- All version numbers must be equal. |
21 | -- If both versions have revision numbers, they must be equal; | 22 | -- If both versions have revision numbers, they must be equal; |
@@ -23,7 +24,7 @@ local version_mt = { | |||
23 | -- @param v1 table: version table to compare. | 24 | -- @param v1 table: version table to compare. |
24 | -- @param v2 table: version table to compare. | 25 | -- @param v2 table: version table to compare. |
25 | -- @return boolean: true if they are considered equivalent. | 26 | -- @return boolean: true if they are considered equivalent. |
26 | __eq = function(v1, v2) | 27 | __eq = function(v1: Version, v2: Version): boolean |
27 | if #v1 ~= #v2 then | 28 | if #v1 ~= #v2 then |
28 | return false | 29 | return false |
29 | end | 30 | end |
@@ -44,7 +45,7 @@ local version_mt = { | |||
44 | -- @param v1 table: version table to compare. | 45 | -- @param v1 table: version table to compare. |
45 | -- @param v2 table: version table to compare. | 46 | -- @param v2 table: version table to compare. |
46 | -- @return boolean: true if v1 is considered lower than v2. | 47 | -- @return boolean: true if v1 is considered lower than v2. |
47 | __lt = function(v1, v2) | 48 | __lt = function(v1: Version, v2: Version): boolean |
48 | for i = 1, math.max(#v1, #v2) do | 49 | for i = 1, math.max(#v1, #v2) do |
49 | local v1i, v2i = v1[i] or 0, v2[i] or 0 | 50 | local v1i, v2i = v1[i] or 0, v2[i] or 0 |
50 | if v1i ~= v2i then | 51 | if v1i ~= v2i then |
@@ -59,18 +60,18 @@ local version_mt = { | |||
59 | -- @param v1 table: version table to compare. | 60 | -- @param v1 table: version table to compare. |
60 | -- @param v2 table: version table to compare. | 61 | -- @param v2 table: version table to compare. |
61 | -- @return boolean: true if v1 is considered lower than or equal to v2. | 62 | -- @return boolean: true if v1 is considered lower than or equal to v2. |
62 | __le = function(v1, v2) | 63 | __le = function(v1: Version, v2: Version): boolean |
63 | return not (v2 < v1) -- luacheck: ignore | 64 | return not (v2 < v1) |
64 | end, | 65 | end, |
65 | --- Return version as a string. | 66 | --- Return version as a string. |
66 | -- @param v The version table. | 67 | -- @param v The version table. |
67 | -- @return The string representation. | 68 | -- @return The string representation. |
68 | __tostring = function(v) | 69 | __tostring = function(v: Version): string |
69 | return v.string | 70 | return v.string |
70 | end, | 71 | end, |
71 | } | 72 | } |
72 | 73 | ||
73 | local version_cache = {} | 74 | local version_cache: {string: Version} = {} |
74 | setmetatable(version_cache, { | 75 | setmetatable(version_cache, { |
75 | __mode = "kv" | 76 | __mode = "kv" |
76 | }) | 77 | }) |
@@ -86,19 +87,18 @@ setmetatable(version_cache, { | |||
86 | -- @param vstring string: A version number in string format. | 87 | -- @param vstring string: A version number in string format. |
87 | -- @return table or nil: A version table or nil | 88 | -- @return table or nil: A version table or nil |
88 | -- if the input string contains invalid characters. | 89 | -- if the input string contains invalid characters. |
89 | function vers.parse_version(vstring) | 90 | function vers.parse_version(vstring: string): Version |
90 | if not vstring then return nil end | 91 | if not vstring then return nil end |
91 | assert(type(vstring) == "string") | ||
92 | 92 | ||
93 | local cached = version_cache[vstring] | 93 | local cached = version_cache[vstring] |
94 | if cached then | 94 | if cached then |
95 | return cached | 95 | return cached |
96 | end | 96 | end |
97 | 97 | ||
98 | local version = {} | 98 | local version: Version = {} |
99 | local i = 1 | 99 | local i = 1 |
100 | 100 | ||
101 | local function add_token(number) | 101 | local function add_token(number: number) |
102 | version[i] = version[i] and version[i] + number/100000 or number | 102 | version[i] = version[i] and version[i] + number/100000 or number |
103 | i = i + 1 | 103 | i = i + 1 |
104 | end | 104 | end |
@@ -138,11 +138,11 @@ end | |||
138 | -- @param a string: one version. | 138 | -- @param a string: one version. |
139 | -- @param b string: another version. | 139 | -- @param b string: another version. |
140 | -- @return boolean: True if a > b. | 140 | -- @return boolean: True if a > b. |
141 | function vers.compare_versions(a, b) | 141 | function vers.compare_versions(a: string, b: string): boolean |
142 | if a == b then | 142 | if a == b then |
143 | return false | 143 | return false |
144 | end | 144 | end |
145 | return vers.parse_version(a) > vers.parse_version(b) | 145 | return vers.parse_version(b) < vers.parse_version(a) |
146 | end | 146 | end |
147 | 147 | ||
148 | --- A more lenient check for equivalence between versions. | 148 | --- A more lenient check for equivalence between versions. |
@@ -151,19 +151,21 @@ end | |||
151 | -- when requesting "2", then "2", "2.1", "2.3.5-9"... all match. | 151 | -- when requesting "2", then "2", "2.1", "2.3.5-9"... all match. |
152 | -- When requesting "2.1", then "2.1", "2.1.3" match, but "2.2" | 152 | -- When requesting "2.1", then "2.1", "2.1.3" match, but "2.2" |
153 | -- doesn't. | 153 | -- doesn't. |
154 | -- @param version string or table: Version to be tested; may be | 154 | -- @param input_version string or table: Version to be tested; may be |
155 | -- in string format or already parsed into a table. | 155 | -- in string format or already parsed into a table. |
156 | -- @param requested string or table: Version requested; may be | 156 | -- @param input_requested string or table: Version requested; may be |
157 | -- in string format or already parsed into a table. | 157 | -- in string format or already parsed into a table. |
158 | -- @return boolean: True if the tested version matches the requested | 158 | -- @return boolean: True if the tested version matches the requested |
159 | -- version, false otherwise. | 159 | -- version, false otherwise. |
160 | local function partial_match(version, requested) | 160 | local function partial_match(input_version: string | Version, input_requested: string | Version): boolean |
161 | assert(type(version) == "string" or type(version) == "table") | 161 | |
162 | assert(type(requested) == "string" or type(version) == "table") | 162 | local version, requested: Version, Version |
163 | 163 | ||
164 | if type(version) ~= "table" then version = vers.parse_version(version) end | 164 | if not input_version is Version then version = vers.parse_version(input_version) |
165 | if type(requested) ~= "table" then requested = vers.parse_version(requested) end | 165 | else version = input_version end |
166 | if not version or not requested then return false end | 166 | if not input_requested is Version then requested = vers.parse_version(input_requested) |
167 | else requested = input_requested end | ||
168 | if not version is Version or not requested is Version then return false end | ||
167 | 169 | ||
168 | for i, ri in ipairs(requested) do | 170 | for i, ri in ipairs(requested) do |
169 | local vi = version[i] or 0 | 171 | local vi = version[i] or 0 |
@@ -180,24 +182,26 @@ end | |||
180 | -- @param constraints table: An array of constraints in table format. | 182 | -- @param constraints table: An array of constraints in table format. |
181 | -- @return boolean: True if version satisfies all constraints, | 183 | -- @return boolean: True if version satisfies all constraints, |
182 | -- false otherwise. | 184 | -- false otherwise. |
183 | function vers.match_constraints(version, constraints) | 185 | function vers.match_constraints(version: Version, constraints: {Constraint}): boolean |
184 | assert(type(version) == "table") | ||
185 | assert(type(constraints) == "table") | ||
186 | local ok = true | 186 | local ok = true |
187 | setmetatable(version, version_mt) | 187 | setmetatable(version, version_mt) |
188 | for _, constr in pairs(constraints) do | 188 | for _, constr in ipairs(constraints) do |
189 | if type(constr.version) == "string" then | ||
190 | constr.version = vers.parse_version(constr.version) | ||
191 | end | ||
192 | local constr_version, constr_op = constr.version, constr.op | 189 | local constr_version, constr_op = constr.version, constr.op |
193 | setmetatable(constr_version, version_mt) | 190 | local cv: Version |
194 | if constr_op == "==" then ok = version == constr_version | 191 | if constr_version is string then |
195 | elseif constr_op == "~=" then ok = version ~= constr_version | 192 | cv = vers.parse_version(constr_version) |
196 | elseif constr_op == ">" then ok = version > constr_version | 193 | constr.version = cv |
197 | elseif constr_op == "<" then ok = version < constr_version | 194 | else |
198 | elseif constr_op == ">=" then ok = version >= constr_version | 195 | cv = constr_version |
199 | elseif constr_op == "<=" then ok = version <= constr_version | 196 | end |
200 | elseif constr_op == "~>" then ok = partial_match(version, constr_version) | 197 | setmetatable(cv, version_mt) |
198 | if constr_op == "==" then ok = version == cv | ||
199 | elseif constr_op == "~=" then ok = version ~= cv | ||
200 | elseif constr_op == ">" then ok = cv < version | ||
201 | elseif constr_op == "<" then ok = version < cv | ||
202 | elseif constr_op == ">=" then ok = cv <= version | ||
203 | elseif constr_op == "<=" then ok = version <= cv | ||
204 | elseif constr_op == "~>" then ok = partial_match(version, cv) | ||
201 | end | 205 | end |
202 | if not ok then break end | 206 | if not ok then break end |
203 | end | 207 | end |