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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015 - 2018, TBOOX Open Source Group.
--
-- @author ruki
-- @file semver.lua
--
-- define module: semver
local semver = semver or {}
local _instance = _instance or {}
-- load modules
local os = require("base/os")
local string = require("base/string")
-- get the version info
function _instance:get(name)
-- get it from info first
local value = self._INFO[name]
if value ~= nil then
return value
end
end
-- get the major version
function _instance:major()
return self:get("major")
end
-- get the minor version
function _instance:minor()
return self:get("minor")
end
-- get the patch version
function _instance:patch()
return self:get("patch")
end
-- get the raw version string
function _instance:rawstr()
return self:get("raw")
end
-- satisfies the given semantic version(.e.g '> 1.0 < 2.0', '~1.5')?
function _instance:satisfies(version)
return semver.satisfies(self:rawstr(), version)
end
-- is in the given version range, [version1, version2]?
function _instance:at(version1, version2)
return self:ge(version1) and self:le(version2)
end
-- v1 == v2 (str/ver)?
function _instance:eq(version)
if type(version) == "string" then
return semver.compare(self:rawstr(), version) == 0
elseif type(version) == "table" then
return semver.compare(self:rawstr(), version:rawstr()) == 0
end
end
-- v1 < v2 (str/ver)?
function _instance:lt(version)
if type(version) == "string" then
return semver.compare(self:rawstr(), version) < 0
elseif type(version) == "table" then
return semver.compare(self:rawstr(), version:rawstr()) < 0
end
end
-- v1 <= v2 (str/ver)?
function _instance:le(version)
if type(version) == "string" then
return semver.compare(self:rawstr(), version) <= 0
elseif type(version) == "table" then
return semver.compare(self:rawstr(), version:rawstr()) <= 0
end
end
-- v1 > v2 (str/ver)?
function _instance:gt(version)
return not self:le(version)
end
-- v1 >= v2 (str/ver)?
function _instance:ge(version)
return not self:lt(version)
end
-- v1 == v2?
function _instance:__eq(version)
return self:eq(version)
end
-- v1 < v2?
function _instance:__lt(version)
return self:lt(version)
end
-- v1 <= v2?
function _instance:__le(version)
return self:le(version)
end
-- get the raw version string
function _instance:__tostring()
return self:rawstr()
end
-- new an instance
function semver.new(version)
-- parse version first
local info, errors = semver.parse(version)
if not info then
return nil, errors
end
-- new an instance
local instance = table.inherit(_instance)
-- init instance
instance._INFO = info
-- ok
return instance
end
-- return module: semver
return semver
|