diff options
| author | xq114 <[email protected]> | 2021-04-16 13:07:55 +0800 |
|---|---|---|
| committer | xq114 <[email protected]> | 2021-04-16 13:07:55 +0800 |
| commit | 0ba602c1fdb417188c215ccda4b342549da9924d (patch) | |
| tree | f31e2f10e71bbafe9b90fe19c0715c881374095a | |
| parent | b335fe5f2a21b0663fe0347069940ed5e7f5f5f8 (diff) | |
add os.shell
| -rw-r--r-- | xmake/core/base/os.lua | 5 | ||||
| -rw-r--r-- | xmake/core/base/tty.lua | 32 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 1 | ||||
| -rw-r--r-- | xmake/modules/private/xrepo/action/env.lua | 30 |
4 files changed, 39 insertions, 29 deletions
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}) |
