From b9116ea123e77faec8a4a0cfbfcbb38f2ebc2e66 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 15 Dec 2025 23:24:55 +0800 Subject: add tty.session_id --- core/src/xmake/engine.c | 2 ++ core/src/xmake/tty/session_id.c | 65 +++++++++++++++++++++++++++++++++++++++++ xmake/core/base/tty.lua | 17 +++++++++++ 3 files changed, 84 insertions(+) create mode 100644 core/src/xmake/tty/session_id.c 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 +#else +#include +#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..742b819ab 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,22 @@ 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 + tty._SESSION_ID = session_id or 0 + end + return session_id +end + -- return module return tty -- cgit v1.3.1 From f185da7553866891b71ab046b44769833cd6e005 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 15 Dec 2025 23:25:55 +0800 Subject: improve xrepo env --- xmake/modules/private/xrepo/action/env.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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) -- cgit v1.3.1 From 5ddfccee2173989269704dfc463c948b8bd6653e Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 15 Dec 2025 23:31:29 +0800 Subject: improve session id --- xmake/core/base/tty.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 742b819ab..dd053b3e6 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -550,7 +550,10 @@ function tty.session_id() session_id = hash.strhash32(sid) end end - tty._SESSION_ID = session_id or 0 + if not session_id then + session_id = "00000000" + end + tty._SESSION_ID = session_id end return session_id end -- cgit v1.3.1