summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-04-16 15:10:57 +0800
committerGitHub <[email protected]>2021-04-16 15:10:57 +0800
commita87d6552004a1fb9f89c29bb89f6918b22aedb3c (patch)
tree64f04062897e929a8cd0eda6d5f419a071c2d676
parente7a60dae13e583aa8616dcdc37742ff1b7b17ddd (diff)
parent1c745e5cfe8e28f8ab4b19bfecce22faccc9aa90 (diff)
Merge pull request #1340 from xq114/dev
improve xrepo env
-rw-r--r--xmake/core/base/os.lua5
-rw-r--r--xmake/core/base/tty.lua40
-rw-r--r--xmake/core/sandbox/modules/os.lua1
-rw-r--r--xmake/modules/private/xrepo/action/env.lua37
-rw-r--r--xmake/themes/default/xmake.lua2
5 files changed, 53 insertions, 32 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..30b6abb99 100644
--- a/xmake/core/base/tty.lua
+++ b/xmake/core/base/tty.lua
@@ -125,6 +125,38 @@ function tty.flush()
return tty
end
+-- get shell name
+function tty.shell()
+ local shell = tty._SHELL
+ if shell == nil then
+ local subhost = xmake._SUBHOST
+ if subhost == "windows" then
+ if os.getenv("PROMPT") then
+ shell = "cmd"
+ else
+ local ok, result = os.iorun("pwsh -v")
+ if ok then
+ shell = "pwsh"
+ else
+ shell = "powershell"
+ end
+ end
+ else
+ shell = os.getenv("SHELL")
+ if shell then
+ for _, shellname in ipairs({"zsh", "bash", "sh"}) do
+ if shell:find(shellname) then
+ shell = shellname
+ break
+ end
+ end
+ end
+ end
+ tty._SHELL = shell or "sh"
+ end
+ return tty._SHELL
+end
+
-- get terminal name
-- - xterm
-- - cmd
@@ -132,6 +164,7 @@ end
-- - vscode (in vscode)
-- - msys2
-- - cygwin
+-- - pwsh
-- - powershell
-- - mintty
-- - windows-terminal
@@ -179,11 +212,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"
@@ -195,7 +225,7 @@ function tty.term()
end
tty._TERM = term or "unknown"
end
- return term
+ return tty._TERM
end
-- has emoji?
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 3866786eb..893b68cce 100644
--- a/xmake/modules/private/xrepo/action/env.lua
+++ b/xmake/modules/private/xrepo/action/env.lua
@@ -232,38 +232,23 @@ 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
- local term = os.term()
- if term == "cmd" then
- return "cmd"
- elseif term == "powershell" then
- 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 .. "> "
+ 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
- -- 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
diff --git a/xmake/themes/default/xmake.lua b/xmake/themes/default/xmake.lua
index 3434c6e03..554b15c1a 100644
--- a/xmake/themes/default/xmake.lua
+++ b/xmake/themes/default/xmake.lua
@@ -77,7 +77,7 @@ theme("default")
set_color("dump.function", "cyan")
-- menu
- if is_subhost("windows") and os.term() == "powershell" then
+ if is_subhost("windows") and (os.term() == "powershell" or os.term() == "pwsh") then
set_color("menu.main.task.name", "cyan bright")
set_color("menu.option.name", "green bright")
else