summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-10-08 22:49:43 +0800
committerruki <[email protected]>2018-10-08 10:58:23 +0800
commit5e8c2f16adcd0f0549a8c7f019fb7f8bf3d4732a (patch)
tree0bae6e5bc9ab024d0e13a6256331666b3dc935f3
parent56837f4aab00f68e53bf106c555d79d0cb6bf8cc (diff)
add semver instance
-rw-r--r--xmake/core/base/semver.lua83
-rw-r--r--xmake/core/sandbox/modules/import/core/base/semver.lua26
2 files changed, 93 insertions, 16 deletions
diff --git a/xmake/core/base/semver.lua b/xmake/core/base/semver.lua
new file mode 100644
index 000000000..8113cac39
--- /dev/null
+++ b/xmake/core/base/semver.lua
@@ -0,0 +1,83 @@
+--!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
+
+-- 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
diff --git a/xmake/core/sandbox/modules/import/core/base/semver.lua b/xmake/core/sandbox/modules/import/core/base/semver.lua
index 9bbee5d41..57ba86c9f 100644
--- a/xmake/core/sandbox/modules/import/core/base/semver.lua
+++ b/xmake/core/sandbox/modules/import/core/base/semver.lua
@@ -27,22 +27,28 @@ local sandbox_core_base_semver = sandbox_core_base_semver or {}
-- load modules
local table = require("base/table")
+local semver = require("base/semver")
local raise = require("sandbox/modules/raise")
+-- new a version instance
+function sandbox_core_base_semver.new(version)
+ local result, errors = semver.new(version)
+ if errors then
+ raise(errors)
+ end
+ return result
+end
+
-- parse a version string into a props table containing all semver infos
--
-- semver.parse('1.2.3') => { major = 1, minor = 2, patch = 3, ... }
-- semver.parse('a.b.c') => nil
--
function sandbox_core_base_semver.parse(version)
-
- -- compare version
local result, errors = semver.parse(version)
if errors then
raise(errors)
end
-
- -- ok
return result
end
@@ -51,14 +57,10 @@ end
-- semver.compare('1.2.3', '1.3.0') > 0?
--
function sandbox_core_base_semver.compare(version1, version2)
-
- -- compare versions
local result, errors = semver.compare(version1, version2)
if errors then
raise(errors)
end
-
- -- ok
return result
end
@@ -67,14 +69,10 @@ end
-- semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') => true
--
function sandbox_core_base_semver.satisfies(version, range)
-
- -- satisfies version
local result, errors = semver.satisfies(version, range)
if errors then
raise(errors)
end
-
- -- ok
return result
end
@@ -88,14 +86,10 @@ end
-- @source the version source, .e.g versions, tags, branchs
--
function sandbox_core_base_semver.select(range, versions, tags, branches)
-
- -- select version
local verinfo, errors = semver.select(range, table.wrap(versions), table.wrap(tags), table.wrap(branches))
if not verinfo then
raise(errors)
end
-
- -- ok
return verinfo.version, verinfo.source
end