From b335fe5f2a21b0663fe0347069940ed5e7f5f5f8 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Fri, 16 Apr 2021 11:05:10 +0800 Subject: improve xrepo env --- xmake/modules/private/xrepo/action/env.lua | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'xmake/modules') diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index 3866786eb..04e68726a 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -23,6 +23,7 @@ import("core.base.option") import("core.base.task") import("core.project.config") import("lib.detect.find_tool") +import("lib.detect.find_program") import("private.action.require.impl.package") import("private.action.require.impl.utils.get_requires") @@ -242,10 +243,11 @@ function _get_shell() end end end - local term = os.term() - if term == "cmd" then + if os.getenv("PROMPT") then return "cmd" - elseif term == "powershell" then + elseif find_program("pwsh") then + return "pwsh" + else return "powershell" end return "sh" @@ -256,14 +258,18 @@ function _run_shell(envs) local shell = _get_shell() local projectname = path.filename(os.projectdir()) if shell:endswith("sh") then - local prompt = projectname .. "> " + local prompt = "[" .. projectname .. "] " .. os.getenv("PS1") os.execv(shell, option.get("arguments"), {envs = table.join({PS1 = prompt}, envs)}) + elseif shell == "pwsh" then + local args = table.join({"-c", "'function prompt { \\\"[" .. projectname .. "] \\\" + $(Get-Location) + \\\"> \\\" }'"}, option.get("arguments")) + os.execv("pwsh", args, {envs = envs}) elseif shell == "powershell" then - -- TODO - os.execv("powershell", option.get("arguments"), {envs = envs}) + local args = table.join({"-c", "'function prompt { \\\"[" .. projectname .. "] \\\" + $(Get-Location) + \\\"> \\\" }'"}, option.get("arguments")) + os.execv("powershell", args, {envs = envs}) elseif shell == "cmd" or is_host("windows") then - local prompt = projectname .. "$G " - os.execv("cmd", table.join({"/k", "prompt " .. prompt}, option.get("arguments")), {envs = envs}) + local prompt = "[" .. projectname .. "] $P$G" + local args = table.join({"/k", "set PROMPT=[" .. projectname .. "] $P$G"}, option.get("arguments")) + os.execv("cmd", args, {envs = envs}) else assert("shell not found!") end -- cgit v1.3.1 From 0ba602c1fdb417188c215ccda4b342549da9924d Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Fri, 16 Apr 2021 13:07:55 +0800 Subject: add os.shell --- xmake/core/base/os.lua | 5 +++++ xmake/core/base/tty.lua | 32 ++++++++++++++++++++++++++---- xmake/core/sandbox/modules/os.lua | 1 + xmake/modules/private/xrepo/action/env.lua | 30 +++++----------------------- 4 files changed, 39 insertions(+), 29 deletions(-) (limited to 'xmake/modules') diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index e294fbcdb..4afa5ef3e 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -979,6 +979,11 @@ function os.fscase() return os._FSCASE end +-- get shell +function os.shell() + return require("base/tty").shell() +end + -- get term function os.term() return require("base/tty").term() diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 074c6e31a..b18ef540a 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -125,6 +125,32 @@ function tty.flush() return tty end +-- get shell name +function tty.shell() + local shell = os.getenv("SHELL") + if shell then + for _, shellname in ipairs({"zsh", "bash", "sh"}) do + if shell:find(shellname) then + return shellname + end + end + end + local subhost = xmake._SUBHOST + if subhost == "windows" then + if os.getenv("PROMPT") then + return "cmd" + else + local ok, result = os.iorun("pwsh -v") + if ok then + return "pwsh" + else + return "powershell" + end + end + end + return "sh" +end + -- get terminal name -- - xterm -- - cmd @@ -132,6 +158,7 @@ end -- - vscode (in vscode) -- - msys2 -- - cygwin +-- - pwsh -- - powershell -- - mintty -- - windows-terminal @@ -179,11 +206,8 @@ function tty.term() term = "vstudio" elseif os.getenv("WT_SESSION") then term = "windows-terminal" - elseif os.getenv("PROMPT") then -- $PROMPT == "$P$G" - term = "cmd" else - -- TODO maybe powershell if no $PROMPT, we need improve it - term = "powershell" + term = tty.shell() end elseif subhost == "msys" then term = "msys2" diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 9700aebb8..34d749429 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -33,6 +33,7 @@ local vformat = require("sandbox/modules/vformat") local sandbox_os = sandbox_os or {} -- inherit some builtin interfaces +sandbox_os.shell = os.shell sandbox_os.term = os.term sandbox_os.host = os.host sandbox_os.arch = os.arch diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index 04e68726a..d6d481d9a 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -233,36 +233,16 @@ function _package_getenvs() return envs end --- get current shell -function _get_shell() - local shell = os.getenv("SHELL") - if shell then - for _, shellname in ipairs({"zsh", "bash", "sh"}) do - if shell:find(shellname) then - return shellname - end - end - end - if os.getenv("PROMPT") then - return "cmd" - elseif find_program("pwsh") then - return "pwsh" - else - return "powershell" - end - return "sh" -end - -- run shell function _run_shell(envs) - local shell = _get_shell() + local shell = os.shell() local projectname = path.filename(os.projectdir()) - if shell:endswith("sh") then - local prompt = "[" .. projectname .. "] " .. os.getenv("PS1") - os.execv(shell, option.get("arguments"), {envs = table.join({PS1 = prompt}, envs)}) - elseif shell == "pwsh" then + if shell == "pwsh" then local args = table.join({"-c", "'function prompt { \\\"[" .. projectname .. "] \\\" + $(Get-Location) + \\\"> \\\" }'"}, option.get("arguments")) os.execv("pwsh", args, {envs = envs}) + elseif shell:endswith("sh") then + local prompt = "[" .. projectname .. "] " .. (os.getenv("PS1") or "\\W $ ") + os.execv(shell, option.get("arguments"), {envs = table.join({PS1 = prompt}, envs)}) elseif shell == "powershell" then local args = table.join({"-c", "'function prompt { \\\"[" .. projectname .. "] \\\" + $(Get-Location) + \\\"> \\\" }'"}, option.get("arguments")) os.execv("powershell", args, {envs = envs}) -- cgit v1.3.1 From 1c745e5cfe8e28f8ab4b19bfecce22faccc9aa90 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Fri, 16 Apr 2021 13:25:16 +0800 Subject: remove unused find_program --- xmake/modules/private/xrepo/action/env.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'xmake/modules') diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index d6d481d9a..893b68cce 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -23,7 +23,6 @@ import("core.base.option") import("core.base.task") import("core.project.config") import("lib.detect.find_tool") -import("lib.detect.find_program") import("private.action.require.impl.package") import("private.action.require.impl.utils.get_requires") -- cgit v1.3.1