diff options
| author | xq114 <[email protected]> | 2021-11-05 16:02:33 +0800 |
|---|---|---|
| committer | xq114 <[email protected]> | 2021-11-05 16:02:33 +0800 |
| commit | e79e2886eb22d78910a6c52c5b9f77c698d5d8b9 (patch) | |
| tree | 769d272a7c8cd131c256968ac4c879613d72ef79 /scripts | |
| parent | 66c32d141498f7ae51f7afc6d08848c83e86d88b (diff) | |
improve env scripts to support binding
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/register-virtualenvs.sh | 23 | ||||
| -rwxr-xr-x | scripts/xrepo.bat | 42 | ||||
| -rw-r--r-- | scripts/xrepo.ps1 | 16 |
3 files changed, 79 insertions, 2 deletions
diff --git a/scripts/register-virtualenvs.sh b/scripts/register-virtualenvs.sh index aa97afec5..8518060f8 100644 --- a/scripts/register-virtualenvs.sh +++ b/scripts/register-virtualenvs.sh @@ -8,7 +8,7 @@ else fi function xrepo { - if [ $# -eq 2 ] && [ "$1" = "env" ]; then + if [ $# -ge 2 ] && [ "$1" = "env" ]; then local cmd="${2-x}" case "$cmd" in shell) @@ -38,6 +38,27 @@ function xrepo { unset XMAKE_ENV_BACKUP fi ;; + -b|--bind) + if [ "$4" = "shell" ]; then + local bnd="${3-x}" + 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 + local prompt="$("$XMAKE_EXE" lua --quiet private.xrepo.action.env.info prompt $bnd)" || return 1 + if [ -z "${prompt+x}" ]; then + return 1 + fi + local activateCommand="$("$XMAKE_EXE" lua --quiet private.xrepo.action.env.info script.bash $bnd)" || return 1 + export XMAKE_ENV_BACKUP="$("$XMAKE_EXE" lua private.xrepo.action.env.info envfile $bnd)" + export XMAKE_PROMPT_BACKUP="${PS1}" + "$XMAKE_EXE" lua --quiet private.xrepo.action.env.info backup.bash $bnd 1>"$XMAKE_ENV_BACKUP" + eval "$activateCommand" + PS1="${prompt} $PS1" + fi + ;; *) "$XMAKE_EXE" lua private.xrepo "$@" ;; diff --git a/scripts/xrepo.bat b/scripts/xrepo.bat index 77acf7d24..82180398a 100755 --- a/scripts/xrepo.bat +++ b/scripts/xrepo.bat @@ -60,6 +60,48 @@ ) goto :ENDXREPO ) + set XREPO_BIND_FLAG= + if [%2]==[-b] if [%4]==[shell] ( + set XREPO_BIND_FLAG=1 + ) + if [%2]==[--bind] if [%4]==[shell] ( + set XREPO_BIND_FLAG=1 + ) + if defined XREPO_BIND_FLAG ( + set XREPO_BIND_FLAG= + if defined XMAKE_PROMPT_BACKUP ( + call %XMAKE_ENV_BACKUP% + setlocal EnableDelayedExpansion + if !errorlevel! neq 0 exit /B !errorlevel! + endlocal + set PROMPT=%XMAKE_PROMPT_BACKUP% + set XMAKE_ENV_BACKUP= + set XMAKE_PROMPT_BACKUP= + echo Please rerun `xrepo env shell` to enter the environment. + exit /B 1 + ) else ( + setlocal EnableDelayedExpansion + @%XMAKE_EXE% lua --quiet private.xrepo.action.env.info prompt %3 1>nul + if !errorlevel! neq 0 ( + echo error: environment not found^^! + exit /B !errorlevel! + ) + endlocal + for /f %%i in ('@%XMAKE_EXE% lua --quiet private.xrepo.action.env.info prompt %3') do @( + @set "PROMPT=%%i %PROMPT%" + ) + @set XMAKE_PROMPT_BACKUP=%PROMPT% + ) + for /f %%i in ('@%XMAKE_EXE% lua private.xrepo.action.env.info envfile %3') do @( + @set "XMAKE_ENV_BACKUP=%%i.bat" + @"%XMAKE_EXE%" lua --quiet private.xrepo.action.env.info backup.cmd %3 1>"%%i.bat" + ) + for /f %%i in ('@%XMAKE_EXE% lua private.xrepo.action.env.info envfile %3') do @( + @"%XMAKE_EXE%" lua --quiet private.xrepo.action.env.info script.cmd %3 1>"%%i.bat" + call "%%i.bat" + ) + goto :ENDXREPO + ) ) @call %XMAKE_EXE% lua private.xrepo %* diff --git a/scripts/xrepo.ps1 b/scripts/xrepo.ps1 index a821d8057..74e5e4682 100644 --- a/scripts/xrepo.ps1 +++ b/scripts/xrepo.ps1 @@ -19,13 +19,27 @@ if ($Args.Count -eq 0) { if ((Test-Path 'Env:XMAKE_PROMPT_MODIFIER') -and ($Env:XMAKE_PROMPT_MODIFIER -ne "")) { Exit-XrepoEnvironment; } - Enter-XrepoEnvironment; + Enter-XrepoEnvironment $Null; return; } "quit" { Exit-XrepoEnvironment; return; } + {$_ -in "-b", "--bind"} { + if (($Args.Count -ge 4) -and ($Args[3] -eq "shell")) { + if (-not (Test-Path 'Env:XMAKE_ROOTDIR')) { + $Env:XMAKE_ROOTDIR = $BASE_DIR; + Import-Module "$Env:XMAKE_ROOTDIR\scripts\xrepo-hook.psm1"; + Add-XrepoEnvironmentToPrompt; + } + if ((Test-Path 'Env:XMAKE_PROMPT_MODIFIER') -and ($Env:XMAKE_PROMPT_MODIFIER -ne "")) { + Exit-XrepoEnvironment; + } + Enter-XrepoEnvironment $Args[2]; + return; + } + } } } |
