Repository navigation
Expand file tree
/
Copy pathsemver.lua
More file actions
116 lines (100 loc) · 3.25 KB
/
Copy pathsemver.lua
File metadata and controls
116 lines (100 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local p = premake
local pkg = p.extensions.pkg
pkg.semver = pkg.semver or {}
local semver = pkg.semver
semver.range = semver.range or {}
local range = semver.range
function semver:new(major, minor, patch)
local object = {
major = major,
minor = minor,
patch = patch
}
setmetatable(object, self)
self.__index = self
return object
end
function semver:fromArray(arr)
if type(arr) ~= "table" then return nil end
if #arr < 2 then return nil end
if #arr == 2 then return self:new(tonumber(arr[1]), tonumber(arr[2]), -1) end
return self:new(tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]))
end
function semver:parse(str)
if type(str) ~= "string" then return nil end
local found, _, major, minor, patch = str:find("^(%d+)%.(%d+)%.?(%d*)$")
if not found then return nil end
if patch:len() == 0 then patch = -1 end
return self:new(tonumber(major), tonumber(minor), tonumber(patch))
end
function semver:__tostring()
if self.patch < 0 then
return string.format("%d.%d", self.major, self.minor)
else
return string.format("%d.%d.%d", self.major, self.minor, self.patch)
end
end
function semver.__eq(lhs, rhs)
return lhs.major == rhs.major and lhs.minor == rhs.minor and lhs.patch == rhs.patch
end
function semver.__lt(lhs, rhs)
if lhs.major > rhs.major then return false elseif lhs.major < rhs.major then return true end
if lhs.minor > rhs.minor then return false elseif lhs.minor < rhs.minor then return true end
return lhs.patch < rhs.patch
end
function semver.__le(lhs, rhs)
if lhs.major > rhs.major then return false elseif lhs.major < rhs.major then return true end
if lhs.minor > rhs.minor then return false elseif lhs.minor < rhs.minor then return true end
return lhs.patch <= rhs.patch
end
function range:new(minver, maxver, minbound, maxbound)
local object = {
minver = minver,
maxver = maxver,
minbound = minbound,
maxbound = maxbound
}
setmetatable(object, self)
self.__index = self
return object
end
function range:inf()
return self:new(nil, nil, true, true)
end
function range:parse(str)
if type(str) ~= "string" then return nil end
if str == "inf" then return self:inf() end
local found, _, lbrack, lver, uver, ubrack = str:find("^([%(%[])([0-9%.]+),([0-9%.]+)([%)%]])$")
if not found then return nil end
local minver = semver:parse(lver)
local maxver = semver:parse(uver)
if not minver or not maxver then return nil end
local minbound = iif(lbrack == "[", true, false)
local maxbound = iif(ubrack == "]", true, false)
return self:new(minver, maxver, minbound, maxbound)
end
function range:inRange(ver)
if not self.minver or not self.maxver then return true end
if self.minbound then
if ver < self.minver then return false end
else
if ver <= self.minver then return false end
end
if self.maxbound then
if ver > self.maxver then return false end
else
if ver >= self.maxver then return false end
end
return true
end
function range:__tostring()
if not self.minver or not self.maxver then return "inf" end
if self.minbound and self.maxbound then
if self.minver == self.maxver then
return self.minver:tostring()
end
end
local lbrack = iif(self.minbound, "[", "(")
local rbrack = iif(self.maxbound, "]", ")")
return string.format("%s%s,%s%s", lbrack, tostring(self.minver), tostring(self.maxver), rbrack)
end