diff options
| author | ruki <[email protected]> | 2025-12-15 16:48:57 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-15 16:48:57 +0800 |
| commit | bab1a618ff5ef169f93d561eb91e42da3fe722ec (patch) | |
| tree | ea2b1df9e3c38ccf3f9ab6281054a6b3706ccd1f | |
| parent | e58fd4cd9145d5a060a00c513cbe76a1011c013f (diff) | |
| parent | 5ddfccee2173989269704dfc463c948b8bd6653e (diff) | |
Merge pull request #7135 from xmake-io/session
Improve `xrepo env` to add session id
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/tty/session_id.c | 65 | ||||
| -rw-r--r-- | xmake/core/base/tty.lua | 20 | ||||
| -rw-r--r-- | xmake/modules/private/xrepo/action/env.lua | 3 |
4 files changed, 89 insertions, 1 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 530071a68..ea9dc2af1 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -330,6 +330,7 @@ tb_int_t xm_libc_setbyte(lua_State *lua); // the tty functions tb_int_t xm_tty_term_mode(lua_State *lua); +tb_int_t xm_tty_session_id(lua_State *lua); // the package functions tb_int_t xm_package_loadxmi(lua_State *lua); @@ -649,6 +650,7 @@ static luaL_Reg const g_libc_functions[] = { // the tty functions static luaL_Reg const g_tty_functions[] = { { "term_mode", xm_tty_term_mode }, + { "session_id", xm_tty_session_id }, { tb_null, tb_null }, }; diff --git a/core/src/xmake/tty/session_id.c b/core/src/xmake/tty/session_id.c new file mode 100644 index 000000000..d075a624a --- /dev/null +++ b/core/src/xmake/tty/session_id.c @@ -0,0 +1,65 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed 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-present, Xmake Open Source Community. + * + * @author ruki + * @file session_id.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "session_id" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_OS_WINDOWS +#include <windows.h> +#else +#include <unistd.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// tty.session_id() +tb_int_t xm_tty_session_id(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + +#ifdef TB_CONFIG_OS_WINDOWS + HWND hwnd = GetConsoleWindow(); + if (hwnd) { + // use hex string of hwnd as session id + lua_pushfstring(lua, "%p", hwnd); + return 1; + } +#else + // we use the tty name as session id + tb_char_t const* name = ttyname(STDIN_FILENO); + if (name) { + lua_pushstring(lua, name); + return 1; + } +#endif + + // failed + lua_pushnil(lua); + return 1; +} diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index d35705770..dd053b3e6 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -26,6 +26,7 @@ local io = require("base/io") -- save metatable and builtin functions tty._term_mode = tty._term_mode or tty.term_mode +tty._session_id = tty._session_id or tty.session_id -- @see https://www2.ccs.neu.edu/research/gpc/VonaUtils/vona/terminal/vtansi.htm -- http://www.termsys.demon.co.uk/vtansi.htm @@ -538,6 +539,25 @@ function tty.term_mode(stdtype, newmode) return oldmode end +-- get session id +function tty.session_id() + local session_id = tty._SESSION_ID + if session_id == nil then + if tty._session_id then + local sid = tty._session_id() + if sid then + local hash = require("base/hash") + session_id = hash.strhash32(sid) + end + end + if not session_id then + session_id = "00000000" + end + tty._SESSION_ID = session_id + end + return session_id +end + -- return module return tty diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index b318e6031..a45d08b1c 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -19,6 +19,7 @@ -- -- imports +import("core.base.tty") import("core.base.option") import("core.base.task") import("core.base.hashset") @@ -98,7 +99,7 @@ end -- enter the working project function _enter_project() - local workdir = path.join(os.tmpdir(), "xrepo", "working") + local workdir = path.join(os.tmpdir(), "xrepo", "working-" .. tty.session_id()) if not os.isdir(workdir) then os.mkdir(workdir) os.cd(workdir) |
