summaryrefslogtreecommitdiff
path: root/core/src/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2017-08-14 11:27:19 +0800
committerruki <[email protected]>2017-08-14 11:29:30 +0800
commitdba45d143d8aff047da82cbe1a881056e6e4bc00 (patch)
tree7b80d5de264c17b538a7c9d0fbb234ded39ac683 /core/src/xmake
parent34706ba053dda9dd86902cacf42a6c1878c85234 (diff)
merge repo and require codes
Diffstat (limited to 'core/src/xmake')
-rw-r--r--core/src/xmake/hash/prefix.h36
-rw-r--r--core/src/xmake/hash/sha256.c110
-rw-r--r--core/src/xmake/hash/uuid.c (renamed from core/src/xmake/os/uuid.c)2
-rw-r--r--core/src/xmake/machine.c36
-rw-r--r--core/src/xmake/makefile11
-rw-r--r--core/src/xmake/os/sleep.c53
-rw-r--r--core/src/xmake/semver/parse.c65
-rw-r--r--core/src/xmake/semver/prefix.h37
-rw-r--r--core/src/xmake/semver/satisfies.c78
-rw-r--r--core/src/xmake/semver/select.c160
-rw-r--r--core/src/xmake/semver/semver.c91
-rw-r--r--core/src/xmake/xmake.lua2
12 files changed, 675 insertions, 6 deletions
diff --git a/core/src/xmake/hash/prefix.h b/core/src/xmake/hash/prefix.h
new file mode 100644
index 000000000..5df85a1d3
--- /dev/null
+++ b/core/src/xmake/hash/prefix.h
@@ -0,0 +1,36 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file prefix.h
+ *
+ */
+#ifndef XM_HASH_PREFIX_H
+#define XM_HASH_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+
+
+#endif
+
+
diff --git a/core/src/xmake/hash/sha256.c b/core/src/xmake/hash/sha256.c
new file mode 100644
index 000000000..6a9b1f5df
--- /dev/null
+++ b/core/src/xmake/hash/sha256.c
@@ -0,0 +1,110 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file sha256.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "sha256"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_hash_sha256(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the filename
+ tb_char_t const* filename = luaL_checkstring(lua, 1);
+ tb_check_return_val(filename, 0);
+
+ // load data from file
+ tb_bool_t ok = tb_false;
+ tb_stream_ref_t stream = tb_stream_init_from_file(filename, TB_FILE_MODE_RO);
+ if (stream)
+ {
+ // open stream
+ if (tb_stream_open(stream))
+ {
+ // init sha256
+ tb_sha_t sha;
+ tb_sha_init(&sha, TB_SHA_MODE_SHA2_256);
+
+ // read data and update sha256
+ tb_byte_t data[TB_STREAM_BLOCK_MAXN];
+ while (!tb_stream_beof(stream))
+ {
+ // read data
+ tb_long_t real = tb_stream_read(stream, data, sizeof(data));
+
+ // ok?
+ if (real > 0) tb_sha_spak(&sha, data, real);
+ // no data? continue it
+ else if (!real)
+ {
+ // wait
+ real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream));
+ tb_check_break(real > 0);
+
+ // has read?
+ tb_assert_and_check_break(real & TB_STREAM_WAIT_READ);
+ }
+ // failed or end?
+ else break;
+ }
+
+ // exit sha256
+ tb_byte_t buffer[32];
+ tb_sha_exit(&sha, buffer, sizeof(buffer));
+
+ // make sha256 string
+ tb_size_t i = 0;
+ tb_size_t n = sha.digest_len << 2;
+ tb_char_t s[256] = {0};
+ for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
+
+ // save result
+ lua_pushstring(lua, s);
+
+ // ok
+ ok = tb_true;
+ }
+
+ // exit stream
+ tb_stream_exit(stream);
+ }
+
+ // failed? return nil
+ if (!ok) lua_pushnil(lua);
+
+ // ok
+ return 1;
+}
diff --git a/core/src/xmake/os/uuid.c b/core/src/xmake/hash/uuid.c
index 044bbe83d..9dc8e0eb7 100644
--- a/core/src/xmake/os/uuid.c
+++ b/core/src/xmake/hash/uuid.c
@@ -37,7 +37,7 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
-tb_int_t xm_os_uuid(lua_State* lua)
+tb_int_t xm_hash_uuid(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 7f1acfbd0..e1a080d3a 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -60,13 +60,13 @@ typedef struct __xm_machine_impl_t
// the os functions
tb_int_t xm_os_argv(lua_State* lua);
tb_int_t xm_os_find(lua_State* lua);
-tb_int_t xm_os_uuid(lua_State* lua);
tb_int_t xm_os_isdir(lua_State* lua);
tb_int_t xm_os_rmdir(lua_State* lua);
tb_int_t xm_os_mkdir(lua_State* lua);
tb_int_t xm_os_cpdir(lua_State* lua);
tb_int_t xm_os_chdir(lua_State* lua);
tb_int_t xm_os_mtime(lua_State* lua);
+tb_int_t xm_os_sleep(lua_State* lua);
tb_int_t xm_os_mclock(lua_State* lua);
tb_int_t xm_os_curdir(lua_State* lua);
tb_int_t xm_os_tmpdir(lua_State* lua);
@@ -93,6 +93,10 @@ tb_int_t xm_path_absolute(lua_State* lua);
tb_int_t xm_path_translate(lua_State* lua);
tb_int_t xm_path_is_absolute(lua_State* lua);
+// the hash functions
+tb_int_t xm_hash_uuid(lua_State* lua);
+tb_int_t xm_hash_sha256(lua_State* lua);
+
// the winreg functions
#ifdef TB_CONFIG_OS_WINDOWS
tb_int_t xm_winreg_query(lua_State* lua);
@@ -120,6 +124,11 @@ tb_int_t xm_readline_add_history(lua_State* lua);
tb_int_t xm_readline_clear_history(lua_State* lua);
#endif
+// the semver functions
+tb_int_t xm_semver_parse(lua_State* lua);
+tb_int_t xm_semver_satisfies(lua_State* lua);
+tb_int_t xm_semver_select(lua_State* lua);
+
/* //////////////////////////////////////////////////////////////////////////////////////
* globals
*/
@@ -129,13 +138,13 @@ static luaL_Reg const g_os_functions[] =
{
{ "argv", xm_os_argv }
, { "find", xm_os_find }
-, { "uuid", xm_os_uuid }
, { "isdir", xm_os_isdir }
, { "rmdir", xm_os_rmdir }
, { "mkdir", xm_os_mkdir }
, { "cpdir", xm_os_cpdir }
, { "chdir", xm_os_chdir }
, { "mtime", xm_os_mtime }
+, { "sleep", xm_os_sleep }
, { "mclock", xm_os_mclock }
, { "curdir", xm_os_curdir }
, { "tmpdir", xm_os_tmpdir }
@@ -168,6 +177,14 @@ static luaL_Reg const g_path_functions[] =
, { tb_null, tb_null }
};
+// the hash functions
+static luaL_Reg const g_hash_functions[] =
+{
+ { "uuid", xm_hash_uuid }
+, { "sha256", xm_hash_sha256 }
+, { tb_null, tb_null }
+};
+
// the string functions
static luaL_Reg const g_string_functions[] =
{
@@ -215,6 +232,15 @@ static luaL_Reg const g_readline_functions[] =
};
#endif
+// the semver functions
+static luaL_Reg const g_semver_functions[] =
+{
+ { "parse", xm_semver_parse }
+, { "satisfies", xm_semver_satisfies }
+, { "select", xm_semver_select }
+, { tb_null, tb_null }
+};
+
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
@@ -422,6 +448,9 @@ xm_machine_ref_t xm_machine_init()
// bind path functions
luaL_register(impl->lua, "path", g_path_functions);
+ // bind hash functions
+ luaL_register(impl->lua, "hash", g_hash_functions);
+
// bind string functions
luaL_register(impl->lua, "string", g_string_functions);
@@ -441,6 +470,9 @@ xm_machine_ref_t xm_machine_init()
luaL_register(impl->lua, "readline", g_readline_functions);
#endif
+ // bind semver functions
+ luaL_register(impl->lua, "semver", g_semver_functions);
+
// init host
#if defined(TB_CONFIG_OS_WINDOWS)
lua_pushstring(impl->lua, "windows");
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index e9f5badce..aa003cc35 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -16,13 +16,13 @@ xmake_C_FILES += \
machine \
os/argv \
os/find \
- os/uuid \
os/isdir \
os/rmdir \
os/mkdir \
os/cpdir \
os/chdir \
os/mtime \
+ os/sleep \
os/mclock \
os/curdir \
os/tmpdir \
@@ -44,6 +44,8 @@ xmake_C_FILES += \
path/absolute \
path/translate \
path/is_absolute \
+ hash/uuid \
+ hash/sha256 \
winreg/query \
string/endswith \
string/startswith \
@@ -53,6 +55,10 @@ xmake_C_FILES += \
process/waitlist \
process/close \
sandbox/interactive \
+ semver/parse \
+ semver/satisfies \
+ semver/select \
+ semver/semver \
readline/readline \
readline/history_list \
readline/add_history \
@@ -65,7 +71,8 @@ xmake_CXFLAGS += $(if $(findstring readline,$(base_LIBNAMES)),-DXM_CONFIG_API
# includes
xmake_INC_DIRS += \
- ../luajit/src
+ ../luajit/src \
+ ../sv/include
# suffix
diff --git a/core/src/xmake/os/sleep.c b/core/src/xmake/os/sleep.c
new file mode 100644
index 000000000..4615c274f
--- /dev/null
+++ b/core/src/xmake/os/sleep.c
@@ -0,0 +1,53 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file sleep.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "sleep"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_os_sleep(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the interval (ms)
+ tb_long_t interval = (tb_long_t)luaL_checklong(lua, 1);
+
+ // sleep it
+ if (interval >= 0) tb_msleep(interval);
+
+ // ok
+ return 0;
+}
diff --git a/core/src/xmake/semver/parse.c b/core/src/xmake/semver/parse.c
new file mode 100644
index 000000000..976f198fb
--- /dev/null
+++ b/core/src/xmake/semver/parse.c
@@ -0,0 +1,65 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author uael
+ * @file parse.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "parse"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+// parse wrapper
+tb_int_t xm_semver_parse(lua_State* lua)
+{
+ semver_t semver = {0};
+
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the version string
+ tb_char_t const* str = luaL_checkstring(lua, 1);
+ tb_check_return_val(str, 0);
+
+ if (semvern(&semver, str, tb_strlen(str))) {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "Unable to parse semver '%s'", str);
+
+ return 2;
+ }
+
+ lua_pushsemver(lua, semver);
+ semver_dtor(&semver);
+
+ // ok
+ return 1;
+}
diff --git a/core/src/xmake/semver/prefix.h b/core/src/xmake/semver/prefix.h
new file mode 100644
index 000000000..17c4e9f5a
--- /dev/null
+++ b/core/src/xmake/semver/prefix.h
@@ -0,0 +1,37 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author uael
+ * @file prefix.h
+ *
+ */
+#ifndef XM_SEMVER_PREFIX_H
+#define XM_SEMVER_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+
+#include "semver.h"
+
+void lua_pushsemver(lua_State *lua, semver_t semver);
+
+#endif
diff --git a/core/src/xmake/semver/satisfies.c b/core/src/xmake/semver/satisfies.c
new file mode 100644
index 000000000..f3b34890f
--- /dev/null
+++ b/core/src/xmake/semver/satisfies.c
@@ -0,0 +1,78 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author uael
+ * @file satisfies.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "satisfies"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+// satisfies wrapper
+tb_int_t xm_semver_satisfies(lua_State* lua)
+{
+ semver_t semver = {0};
+ semver_range_t range = {0};
+
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the version string
+ tb_char_t const* str = luaL_checkstring(lua, 1);
+ tb_char_t const* range_str = luaL_checkstring(lua, 2);
+
+ tb_check_return_val(str, 0);
+ tb_check_return_val(range_str, 0);
+
+ if (semvern(&semver, str, tb_strlen(str))) {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "Unable to parse semver '%s'", str);
+
+ return 2;
+ }
+
+ if (semver_rangen(&range, range_str, tb_strlen(range_str))) {
+ semver_dtor(&semver);
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "Unable to parse semver range '%s'", range_str);
+
+ return 2;
+ }
+
+ lua_pushboolean(lua, semver_range_match(semver, range));
+ semver_dtor(&semver);
+ semver_range_dtor(&range);
+
+ // ok
+ return 1;
+}
diff --git a/core/src/xmake/semver/select.c b/core/src/xmake/semver/select.c
new file mode 100644
index 000000000..6400de6a8
--- /dev/null
+++ b/core/src/xmake/semver/select.c
@@ -0,0 +1,160 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author uael
+ * @file select.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "select"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include <stdlib.h>
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+// satisfies wrapper
+tb_int_t xm_semver_select(lua_State* lua)
+{
+ semver_t semver = {0};
+ semvers_t matches = {0};
+ semver_range_t range = {0};
+ lua_Integer i;
+ size_t range_len = 0, source_len;
+ tb_char_t const* source_str;
+ tb_char_t const* source;
+ tb_bool_t is_range;
+
+ lua_settop(lua, 4);
+
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the version string
+ tb_char_t const* range_str = luaL_checkstring(lua, 1);
+ tb_check_return_val(range_str, 0);
+ range_len = tb_strlen(range_str);
+
+ is_range = semver_rangen(&range, range_str, range_len) == 0;
+ if (is_range) {
+ source = "versions";
+ luaL_checktype(lua, 2, LUA_TTABLE);
+ for (i = lua_objlen(lua, 2); i > 0; --i) {
+ lua_pushinteger(lua, i);
+ lua_gettable(lua, 2);
+
+ source_str = luaL_checkstring(lua, -1);
+ tb_check_return_val(source_str, 0);
+
+ if (semver_tryn(&semver, source_str, tb_strlen(source_str)) == 0) {
+ if (semver_range_match(semver, range)) {
+ semvers_push(matches, semver);
+ } else {
+ semver_dtor(&semver);
+ }
+ }
+ }
+ if (matches.length) {
+ goto match;
+ }
+ semvers_clear(matches);
+ source = "tags";
+ luaL_checktype(lua, 3, LUA_TTABLE);
+ for (i = lua_objlen(lua, 3); i > 0; --i) {
+ lua_pushinteger(lua, i);
+ lua_gettable(lua, 3);
+
+ source_str = luaL_checkstring(lua, -1);
+ tb_check_return_val(source_str, 0);
+
+ if (semver_tryn(&semver, source_str, tb_strlen(source_str)) == 0) {
+ if (semver_range_match(semver, range)) {
+ semvers_push(matches, semver);
+ } else {
+ semver_dtor(&semver);
+ }
+ }
+ }
+ if (matches.length) {
+ goto match;
+ }
+ semvers_dtor(matches);
+ semver_range_dtor(&range);
+ }
+
+ source = "branches";
+ luaL_checktype(lua, 4, LUA_TTABLE);
+ for (i = lua_objlen(lua, 4); i > 0; --i) {
+ lua_pushinteger(lua, i);
+ lua_gettable(lua, 4);
+
+ source_str = luaL_checkstring(lua, -1);
+ tb_check_return_val(source_str, 0);
+ source_len = tb_strlen(source_str);
+
+ if (source_len == range_len && tb_memcmp(source_str, range_str, source_len) == 0) {
+ lua_createtable(lua, 0, 2);
+
+ lua_pushlstring(lua, source_str, source_len);
+ lua_setfield(lua, -2, "version");
+
+ lua_pushstring(lua, source);
+ lua_setfield(lua, -2, "source");
+
+ return 1;
+ }
+ }
+
+ if (!is_range) {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "Unable to parse semver range '%s'", range_str);
+
+ return 2;
+ }
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "Unable to select version for range '%s'", range_str);
+
+ return 2;
+ match:
+ semvers_sort(matches);
+ semver = semvers_pop(matches);
+ lua_createtable(lua, 0, 2);
+
+ lua_pushstring(lua, semver.raw);
+ lua_setfield(lua, -2, "version");
+
+ lua_pushstring(lua, source);
+ lua_setfield(lua, -2, "source");
+
+ semvers_dtor(matches);
+ semver_dtor(&semver);
+ semver_range_dtor(&range);
+
+ return 1;
+}
diff --git a/core/src/xmake/semver/semver.c b/core/src/xmake/semver/semver.c
new file mode 100644
index 000000000..27dfb3fb6
--- /dev/null
+++ b/core/src/xmake/semver/semver.c
@@ -0,0 +1,91 @@
+/*!The Make-like 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 - 2017, TBOOX Open Source Group.
+ *
+ * @author uael
+ * @file semver.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "semver"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+void lua_pushsemver(lua_State *lua, const semver_t semver)
+{
+ semver_id_t const *id;
+ tb_uchar_t i = 0;
+
+ lua_createtable(lua, 0, 7);
+
+ lua_pushstring(lua, semver.raw);
+ lua_setfield(lua, -2, "raw");
+
+ lua_pushlstring(lua, semver.raw, semver.len);
+ lua_setfield(lua, -2, "version");
+
+ lua_pushinteger(lua, semver.major);
+ lua_setfield(lua, -2, "major");
+
+ lua_pushinteger(lua, semver.minor);
+ lua_setfield(lua, -2, "minor");
+
+ lua_pushinteger(lua, semver.patch);
+ lua_setfield(lua, -2, "patch");
+
+ lua_pushstring(lua, "prerelease");
+ lua_newtable(lua);
+ id = &semver.prerelease;
+ while (id && id->len) {
+ if (id->numeric) {
+ lua_pushinteger(lua, id->num);
+ } else {
+ lua_pushlstring(lua, id->raw, id->len);
+ }
+ id = id->next;
+ lua_rawseti(lua, -2, ++i);
+ }
+ lua_settable(lua, -3);
+
+ i = 0;
+ lua_pushstring(lua, "build");
+ lua_newtable(lua);
+ id = &semver.build;
+ while (id && id->len) {
+ if (id->numeric) {
+ lua_pushinteger(lua, id->num);
+ } else {
+ lua_pushlstring(lua, id->raw, id->len);
+ }
+ id = id->next;
+ lua_rawseti(lua, -2, ++i);
+ }
+ lua_settable(lua, -3);
+}
diff --git a/core/src/xmake/xmake.lua b/core/src/xmake/xmake.lua
index 484d94b98..acc71ed46 100644
--- a/core/src/xmake/xmake.lua
+++ b/core/src/xmake/xmake.lua
@@ -5,7 +5,7 @@ target("xmake")
set_kind("static")
-- add deps
- add_deps("luajit")
+ add_deps("sv", "luajit")
-- add defines
add_defines("__tb_prefix__=\"xmake\"")