From a21ec09a71f0e4c60957b37eb61e33f1a4ae6e86 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Tue, 24 Aug 2021 19:57:35 +0800 Subject: improve virtualenv on linux --- scripts/get.sh | 8 ++++- scripts/register-completions.sh | 2 ++ scripts/register-virtualenvs.sh | 48 ++++++++++++++++++++++++++++++ xmake/modules/private/xrepo/action/env.lua | 18 +++++++++-- 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 scripts/register-virtualenvs.sh diff --git a/scripts/get.sh b/scripts/get.sh index 8363b4499..21814e45e 100755 --- a/scripts/get.sh +++ b/scripts/get.sh @@ -227,13 +227,19 @@ write_profile() install_profile() { if [ ! -d ~/.xmake ]; then mkdir ~/.xmake; fi - echo "export PATH=$prefix/bin:\$PATH" > ~/.xmake/profile + echo "export XMAKE_ROOTDIR=$prefix/bin" > ~/.xmake/profile if [ -f "$projectdir/scripts/register-completions.sh" ]; then cat "$projectdir/scripts/register-completions.sh" >> ~/.xmake/profile else remote_get_content "$gitrepo_raw/scripts/register-completions.sh" >> ~/.xmake/profile fi + if [ -f "$projectdir/scripts/register-virtualenvs.sh" ]; then + cat "$projectdir/scripts/register-virtualenvs.sh" >> ~/.xmake/profile + else + remote_get_content "$gitrepo_raw/scripts/register-virtualenvs.sh" >> ~/.xmake/profile + fi + if [[ "$SHELL" = */zsh ]]; then write_profile ~/.zshrc elif [[ "$SHELL" = */ksh ]]; then diff --git a/scripts/register-completions.sh b/scripts/register-completions.sh index e274605b2..c93cfb15e 100755 --- a/scripts/register-completions.sh +++ b/scripts/register-completions.sh @@ -1,6 +1,8 @@ # parameter completions for *nix shell +export PATH=${XMAKE_ROOTDIR}:$PATH + if [[ "$SHELL" = */zsh ]]; then # zsh parameter completion for xmake _xmake_zsh_complete() diff --git a/scripts/register-virtualenvs.sh b/scripts/register-virtualenvs.sh new file mode 100644 index 000000000..13a661435 --- /dev/null +++ b/scripts/register-virtualenvs.sh @@ -0,0 +1,48 @@ + +# virtual environments for *nix shell + +if test "${XMAKE_ROOTDIR}"; then + export XMAKE_EXE=${XMAKE_ROOTDIR}/xmake +else + export XMAKE_EXE=xmake +fi + +function xrepo { + if [ $# -eq 2 ] && [ "$1" = "env" ]; then + local cmd="${2-x}" + case "$cmd" in + shell) + if test "${XMAKE_PROMPT_BACKUP}"; then + PS1="${XMAKE_PROMPT_BACKUP}" + source "${XMAKE_ENV_BACKUP}" || return 1 + unset XMAKE_PROMPT_BACKUP + unset XMAKE_ENV_BACKUP + fi + "$XMAKE_EXE" lua private.xrepo.action.env.info config || return 1 + local prompt="$("$XMAKE_EXE" lua private.xrepo.action.env.info prompt)" || return 1 + if [ -z "${prompt+x}" ]; then + return 1 + fi + local activateCommand="$("$XMAKE_EXE" lua private.xrepo.action.env.info script.bash)" || return 1 + export XMAKE_ENV_BACKUP="$("$XMAKE_EXE" lua private.xrepo.action.env.info envfile)" + export XMAKE_PROMPT_BACKUP="${PS1}" + "$XMAKE_EXE" lua private.xrepo.action.env.info backup.bash 1>"$XMAKE_ENV_BACKUP" + eval "$activateCommand" + PS1="${prompt} $PS1" + ;; + quit) + if test "${XMAKE_PROMPT_BACKUP}"; then + PS1="${XMAKE_PROMPT_BACKUP}" + source "${XMAKE_ENV_BACKUP}" || return 1 + unset XMAKE_PROMPT_BACKUP + unset XMAKE_ENV_BACKUP + fi + ;; + *) + "$XMAKE_EXE" lua private.xrepo $@ + ;; + esac + else + "$XMAKE_EXE" lua private.xrepo $@ + fi +} diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index cb6936006..297a0717c 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -285,15 +285,29 @@ function _get_env_script(envs, shell, del) elseif shell == "cmd" then prefix = "@set \"" suffix = "\"" + elseif shell:endswith("sh") then + if del then + prefix = "unset " + connector = "" + else + prefix = "export " + connector = "='" + suffix = "'" + end end + local exceptions = hashset.of("_", "PS1", "PROMPT") local ret = "" if del then for name, _ in pairs(envs) do - ret = ret .. prefix .. name .. connector .. default .. suffix .. "\n" + if not exceptions:has(name) then + ret = ret .. prefix .. name .. connector .. default .. suffix .. "\n" + end end else for name, value in pairs(envs) do - ret = ret .. prefix .. name .. connector .. value .. suffix .. "\n" + if not exceptions:has(name) then + ret = ret .. prefix .. name .. connector .. value .. suffix .. "\n" + end end end return ret -- cgit v1.3.1 From e273de15d253d4dc1b904e4fa0da5fb1bbe57304 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Tue, 24 Aug 2021 21:11:47 +0800 Subject: move PATH setting to toplevel --- scripts/get.sh | 3 ++- scripts/register-completions.sh | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/get.sh b/scripts/get.sh index 21814e45e..060ee4e20 100755 --- a/scripts/get.sh +++ b/scripts/get.sh @@ -227,7 +227,8 @@ write_profile() install_profile() { if [ ! -d ~/.xmake ]; then mkdir ~/.xmake; fi - echo "export XMAKE_ROOTDIR=$prefix/bin" > ~/.xmake/profile + echo "export XMAKE_ROOTDIR=\"$prefix/bin\"" > ~/.xmake/profile + echo "export PATH=\"${XMAKE_ROOTDIR}:$PATH\"" >> ~/.xmake/profile if [ -f "$projectdir/scripts/register-completions.sh" ]; then cat "$projectdir/scripts/register-completions.sh" >> ~/.xmake/profile else diff --git a/scripts/register-completions.sh b/scripts/register-completions.sh index c93cfb15e..e274605b2 100755 --- a/scripts/register-completions.sh +++ b/scripts/register-completions.sh @@ -1,8 +1,6 @@ # parameter completions for *nix shell -export PATH=${XMAKE_ROOTDIR}:$PATH - if [[ "$SHELL" = */zsh ]]; then # zsh parameter completion for xmake _xmake_zsh_complete() -- cgit v1.3.1 From b54b0ba654bb8b230efff2bd709839cf32bfb87e Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Tue, 24 Aug 2021 22:21:17 +0800 Subject: make prompt output more robust --- scripts/register-virtualenvs.sh | 2 +- scripts/xrepo.bat | 6 +++--- xmake/modules/private/xrepo/action/env.lua | 2 +- xmake/scripts/xrepo-hook.psm1 | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/register-virtualenvs.sh b/scripts/register-virtualenvs.sh index 13a661435..700d8dd99 100644 --- a/scripts/register-virtualenvs.sh +++ b/scripts/register-virtualenvs.sh @@ -19,7 +19,7 @@ function xrepo { unset XMAKE_ENV_BACKUP fi "$XMAKE_EXE" lua private.xrepo.action.env.info config || return 1 - local prompt="$("$XMAKE_EXE" lua private.xrepo.action.env.info prompt)" || return 1 + local prompt="$("$XMAKE_EXE" lua --quiet private.xrepo.action.env.info prompt)" || return 1 if [ -z "${prompt+x}" ]; then return 1 fi diff --git a/scripts/xrepo.bat b/scripts/xrepo.bat index c8714a11c..77acf7d24 100755 --- a/scripts/xrepo.bat +++ b/scripts/xrepo.bat @@ -22,7 +22,7 @@ if !errorlevel! neq 0 ( exit /B !errorlevel! ) - @%XMAKE_EXE% lua private.xrepo.action.env.info prompt 1>nul + @%XMAKE_EXE% lua --quiet private.xrepo.action.env.info prompt 1>nul if !errorlevel! neq 0 ( echo error: xmake.lua not found^^! exit /B !errorlevel! @@ -39,13 +39,13 @@ if !errorlevel! neq 0 ( exit /B !errorlevel! ) - @%XMAKE_EXE% lua private.xrepo.action.env.info prompt 1>nul + @%XMAKE_EXE% lua --quiet private.xrepo.action.env.info prompt 1>nul if !errorlevel! neq 0 ( echo error: xmake.lua not found^^! exit /B !errorlevel! ) endlocal - for /f %%i in ('@%XMAKE_EXE% lua private.xrepo.action.env.info prompt') do @( + for /f %%i in ('@%XMAKE_EXE% lua --quiet private.xrepo.action.env.info prompt') do @( @set "PROMPT=%%i %PROMPT%" ) @set XMAKE_PROMPT_BACKUP=%PROMPT% diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index 297a0717c..91d9317ed 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -317,7 +317,7 @@ end function info(key) if key == "prompt" then assert(os.isfile(os.projectfile()), "xmake.lua not found!") - print("[%s]", path.filename(os.projectdir())) + io.write("[" .. path.filename(os.projectdir()) .. "]") elseif key == "envfile" then print(os.tmpfile()) elseif key == "config" then diff --git a/xmake/scripts/xrepo-hook.psm1 b/xmake/scripts/xrepo-hook.psm1 index 902ef3b16..53342fba0 100644 --- a/xmake/scripts/xrepo-hook.psm1 +++ b/xmake/scripts/xrepo-hook.psm1 @@ -21,7 +21,7 @@ function Enter-XrepoEnvironment { } $xmakeColorTermBackup, $Env:XMAKE_COLORTERM = $Env:XMAKE_COLORTERM, "nocolor"; - $xrepoPrompt = (& $Env:XMAKE_EXE lua private.xrepo.action.env.info prompt | Out-String); + $xrepoPrompt = (& $Env:XMAKE_EXE lua --quiet private.xrepo.action.env.info prompt | Out-String); $Env:XMAKE_COLORTERM = $xmakeColorTermBackup; if (-not $xrepoPrompt.StartsWith("[")) { Write-Host $xrepoPrompt; -- cgit v1.3.1 From 34241bb3d8a1dec0ead81316473e505eb2e5f713 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Tue, 24 Aug 2021 23:14:16 +0800 Subject: fix xmake not found issue --- scripts/get.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/get.sh b/scripts/get.sh index 060ee4e20..76d329116 100755 --- a/scripts/get.sh +++ b/scripts/get.sh @@ -227,8 +227,8 @@ write_profile() install_profile() { if [ ! -d ~/.xmake ]; then mkdir ~/.xmake; fi - echo "export XMAKE_ROOTDIR=\"$prefix/bin\"" > ~/.xmake/profile - echo "export PATH=\"${XMAKE_ROOTDIR}:$PATH\"" >> ~/.xmake/profile + echo "export XMAKE_ROOTDIR=\"$prefix/bin\"\n" > ~/.xmake/profile + echo 'export PATH="$XMAKE_ROOTDIR:$PATH"' >> ~/.xmake/profile if [ -f "$projectdir/scripts/register-completions.sh" ]; then cat "$projectdir/scripts/register-completions.sh" >> ~/.xmake/profile else -- cgit v1.3.1 From 65cd1d83d3f61415607b8976e7b5c215b67e42a8 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Tue, 24 Aug 2021 23:46:40 +0800 Subject: fix typo --- scripts/get.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/get.sh b/scripts/get.sh index 76d329116..f64ff8041 100755 --- a/scripts/get.sh +++ b/scripts/get.sh @@ -227,7 +227,7 @@ write_profile() install_profile() { if [ ! -d ~/.xmake ]; then mkdir ~/.xmake; fi - echo "export XMAKE_ROOTDIR=\"$prefix/bin\"\n" > ~/.xmake/profile + echo "export XMAKE_ROOTDIR=\"$prefix/bin\"" > ~/.xmake/profile echo 'export PATH="$XMAKE_ROOTDIR:$PATH"' >> ~/.xmake/profile if [ -f "$projectdir/scripts/register-completions.sh" ]; then cat "$projectdir/scripts/register-completions.sh" >> ~/.xmake/profile -- cgit v1.3.1