diff options
| author | ruki <[email protected]> | 2021-08-25 06:34:15 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-08-25 06:34:15 +0800 |
| commit | a483cecba3feb404388099ccc8bdb32e6382102c (patch) | |
| tree | 4def16435b92dd065fd54bf74ea8de66989e7863 | |
| parent | dbd66858b8b68a784c415557e8958650e3d638c1 (diff) | |
| parent | 65cd1d83d3f61415607b8976e7b5c215b67e42a8 (diff) | |
Merge pull request #1604 from xq114/dev
improve virtualenv on linux
| -rwxr-xr-x | scripts/get.sh | 9 | ||||
| -rw-r--r-- | scripts/register-virtualenvs.sh | 48 | ||||
| -rwxr-xr-x | scripts/xrepo.bat | 6 | ||||
| -rw-r--r-- | xmake/modules/private/xrepo/action/env.lua | 20 | ||||
| -rw-r--r-- | xmake/scripts/xrepo-hook.psm1 | 2 |
5 files changed, 77 insertions, 8 deletions
diff --git a/scripts/get.sh b/scripts/get.sh index 8363b4499..f64ff8041 100755 --- a/scripts/get.sh +++ b/scripts/get.sh @@ -227,13 +227,20 @@ 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 + 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 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-virtualenvs.sh b/scripts/register-virtualenvs.sh new file mode 100644 index 000000000..700d8dd99 --- /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 --quiet 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/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 cb6936006..91d9317ed 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 @@ -303,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; |
