summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-02-06 23:17:02 +0800
committerruki <[email protected]>2020-02-06 13:42:16 +0800
commitc1262215a240eefc46a97b88ec3086e0c55947e3 (patch)
tree28fa7055f874028a37341f7986e49707be68c10b
parent77f79dd72801215d403b1a5a7b7a130c68bd4e2b (diff)
improve semver and find_programver
-rwxr-xr-xtests/modules/semver/test.lua43
-rw-r--r--xmake/core/base/semver.lua20
-rw-r--r--xmake/core/sandbox/modules/import/core/base/semver.lua14
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_programver.lua7
-rw-r--r--xmake/modules/detect/tools/find_dmd.lua1
5 files changed, 52 insertions, 33 deletions
diff --git a/tests/modules/semver/test.lua b/tests/modules/semver/test.lua
index 3a7049c9a..f39f3acde 100755
--- a/tests/modules/semver/test.lua
+++ b/tests/modules/semver/test.lua
@@ -1,10 +1,7 @@
--- imports
import("core.base.semver")
-- select version
function _check_semver_select(t, results, required_ver, versions, tags, branches)
-
- -- select it
local version, source = semver.select(required_ver, versions or {}, tags or {}, branches or {})
t:are_equal((version.version or version), results[1])
t:are_equal(source, results[2])
@@ -34,8 +31,6 @@ end
-- select version
function _check_semver_satisfies(t, expected, version, range)
-
- -- select it
local result = semver.satisfies(version, range)
t:are_equal(result, expected)
end
@@ -51,21 +46,29 @@ function test_semver_satisfies(t)
_check_semver_satisfies(t, false, "1.6.1", "~1.5.0")
_check_semver_satisfies(t, false, "2.5.1", "^1.5.0")
_check_semver_satisfies(t, false, "1.4.1", ">=1.5.0 <1.6.0")
-
end
-- parse version
function _check_semver_parse(t, version_str, major, minor, patch, prerelease, build)
+ local version = semver.new(version_str)
+ t:require(version)
+ t:are_equal(version:major(), major)
+ t:are_equal(version:minor(), minor)
+ t:are_equal(version:patch(), patch)
+ t:are_equal(version:prerelease(), prerelease or {})
+ t:are_equal(version:build(), build or {})
+end
- -- create it
- local version = semver.parse(version_str)
-
- t:are_equal(version.major, major)
- t:are_equal(version.minor, minor)
- t:are_equal(version.patch, patch)
- t:are_equal(version.prerelease, prerelease or {})
- t:are_equal(version.build, build or {})
-
+-- match version
+function _check_semver_match(t, str, version_str, major, minor, patch, prerelease, build)
+ local version = semver.match(str)
+ t:require(version)
+ t:are_equal(version:rawstr(), version_str)
+ t:are_equal(version:major(), major)
+ t:are_equal(version:minor(), minor)
+ t:are_equal(version:patch(), patch)
+ t:are_equal(version:prerelease(), prerelease or {})
+ t:are_equal(version:build(), build or {})
end
-- test parse version
@@ -76,5 +79,13 @@ function test_semver_parse(t)
_check_semver_parse(t, "1.2.3-beta+77", 1, 2, 3, {"beta"}, {77})
_check_semver_parse(t, "v1.2.3-alpha.1+77", 1, 2, 3, {"alpha", 1}, {77})
_check_semver_parse(t, "v3.2.1-alpha.1+77.foo", 3, 2, 1, {"alpha", 1}, {77, "foo"})
+end
-end \ No newline at end of file
+-- test match version string
+function test_semver_match(t)
+ _check_semver_match(t, "gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0", "7.4.0-1ubuntu1", 7, 4, 0, {"1ubuntu1"})
+ _check_semver_match(t, "gcc (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0", "8.1.0", 8, 1, 0)
+ _check_semver_match(t, "DMD64 D Compiler v2.090.0", "2.090.0", 2, 90, 0)
+ _check_semver_match(t, "Apple clang version 11.0.0 (clang-1100.0.33.12)", "11.0.0", 11, 0, 0)
+ _check_semver_match(t, "curl 7.54.0 (x86_64-apple-darwin18.0) libcurl/7.54.0 LibreSSL/2.6.5 zlib/1.2.11 nghttp2/1.24.1", "7.54.0", 7, 54, 0)
+end
diff --git a/xmake/core/base/semver.lua b/xmake/core/base/semver.lua
index 789a30f1d..ba8a6bfe8 100644
--- a/xmake/core/base/semver.lua
+++ b/xmake/core/base/semver.lua
@@ -156,13 +156,25 @@ function semver.new(version)
-- new an instance
local instance = table.inherit(_instance)
-
- -- init instance
instance._INFO = info
-
- -- ok
return instance
end
+-- try to match valid version from string
+function semver.match(str, pos, pattern)
+ local patterns = pattern or {"%d+[.]%d+[-+.%w]*", "%d+[.]%d+[.]%d+", "%d+[.]%d+"}
+ for _, pattern in ipairs(table.wrap(patterns)) do
+ local version_str = str:match(pattern, pos)
+ if version_str then
+ local info = semver.parse(version_str)
+ if info then
+ local instance = table.inherit(_instance)
+ instance._INFO = info
+ return instance
+ end
+ end
+ end
+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 8ab0844a1..69a33c982 100644
--- a/xmake/core/sandbox/modules/import/core/base/semver.lua
+++ b/xmake/core/sandbox/modules/import/core/base/semver.lua
@@ -35,17 +35,13 @@ function sandbox_core_base_semver.new(version)
return result
end
--- parse a version string into a props table containing all semver infos
+-- match a valid version from the string
--
--- semver.parse('1.2.3') => { major = 1, minor = 2, patch = 3, ... }
--- semver.parse('a.b.c') => nil
+-- semver.match('xxx 1.2.3 xxx') => { major = 1, minor = 2, patch = 3, ... }
+-- semver.match('a.b.c') => nil
--
-function sandbox_core_base_semver.parse(version)
- local result, errors = semver.parse(version)
- if errors then
- raise(errors)
- end
- return result
+function sandbox_core_base_semver.match(str, pos, pattern)
+ return semver.match(str, pos)
end
-- is valid version?
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua b/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua
index b851dab82..e48fb6d9e 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_programver.lua
@@ -27,6 +27,7 @@ local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
local option = require("base/option")
+local semver = require("base/semver")
local project = require("project/project")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
@@ -90,9 +91,9 @@ function sandbox_lib_detect_find_programver.main(program, opt)
result = nil
end
elseif parse == nil or type(parse) == "string" then
- result = outdata:match(parse or "(%d+%.?%d*%.?%d*.-)%s")
- if not result then
- result = outdata:match(parse or "(%d+%.?%d*%.?%d*.-)")
+ result = semver.match(outdata, 1, parse)
+ if result then
+ result = result:rawstr()
end
end
end
diff --git a/xmake/modules/detect/tools/find_dmd.lua b/xmake/modules/detect/tools/find_dmd.lua
index 189b3508c..930ef4390 100644
--- a/xmake/modules/detect/tools/find_dmd.lua
+++ b/xmake/modules/detect/tools/find_dmd.lua
@@ -39,7 +39,6 @@ function main(opt)
-- init options
opt = opt or {}
opt.command = opt.command or "--version"
- opt.parse = opt.parse or function (output) return output:match("v(%d+%.?%d*%.?%d*.-)%s") end
-- find program
local program = find_program(opt.program or "dmd", opt)