diff options
| author | ruki <[email protected]> | 2021-11-05 20:08:05 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-11-05 20:08:05 +0800 |
| commit | 56d44a8672492290c5ed57ee0cbb54e546592e37 (patch) | |
| tree | 146360b1dca54f036f190f61e04548ff372a1782 | |
| parent | 3cb2ad7165bb7460b62578782ed43d24396edd5e (diff) | |
| parent | e79e2886eb22d78910a6c52c5b9f77c698d5d8b9 (diff) | |
Merge pull request #1806 from xq114/dev
improve env scripts to support binding
| -rw-r--r-- | scripts/register-virtualenvs.sh | 23 | ||||
| -rwxr-xr-x | scripts/xrepo.bat | 42 | ||||
| -rw-r--r-- | scripts/xrepo.ps1 | 16 | ||||
| -rw-r--r-- | xmake/modules/private/xrepo/action/env.lua | 38 | ||||
| -rw-r--r-- | xmake/scripts/xrepo-hook.psm1 | 40 |
5 files changed, 128 insertions, 31 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; + } + } } } diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index 8590fcab9..18b59c0e6 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -199,8 +199,8 @@ function _get_envsdir() end -- get bound environment or packages -function _get_boundenv() - local bind = option.get("bind") +function _get_boundenv(opt) + local bind = (opt and opt.bind) or option.get("bind") if bind then local envfile = path.join(_get_envsdir(), bind .. ".lua") if envfile and os.isfile(envfile) then @@ -287,9 +287,9 @@ function _toolchain_addenvs(envs) end -- get package environments -function _package_getenvs() +function _package_getenvs(opt) local envs = os.getenvs() - local boundenv = _get_boundenv() + local boundenv = _get_boundenv(opt) local has_envfile = false local packages = nil if boundenv and os.isfile(boundenv) then @@ -341,15 +341,15 @@ function _get_env_script(envs, shell, del) suffix = "\"" elseif shell:endswith("sh") then if del then - prefix = "unset " - connector = "" + prefix = "unset '" + connector = "'" else - prefix = "export " - connector = "='" + prefix = "export '" + connector = "'='" suffix = "'" end end - local exceptions = hashset.of("_", "PS1", "PROMPT") + local exceptions = hashset.of("_", "PS1", "PROMPT", "!;", "!EXITCODE") local ret = "" if del then for name, _ in pairs(envs) do @@ -368,25 +368,31 @@ function _get_env_script(envs, shell, del) end -- get information of current virtual environment -function info(key) +function info(key, bnd) if key == "prompt" then - assert(os.isfile(os.projectfile()), "xmake.lua not found!") - io.write("[" .. path.filename(os.projectdir()) .. "]") + local boundenv = _get_boundenv({bind = bnd}) + if boundenv then + assert(os.isfile(boundenv), "environment not found!") + io.write("[" .. path.basename(boundenv) .. "]") + elseif not bnd then + assert(os.isfile(os.projectfile()), "xmake.lua not found!") + io.write("[" .. path.filename(os.projectdir()) .. "]") + end elseif key == "envfile" then print(os.tmpfile()) elseif key == "config" then - if os.isfile(os.projectfile()) then + if not bnd and os.isfile(os.projectfile()) then task.run("config", {target = "all"}, {disable_dump = true}) end elseif key:startswith("script.") then local shell = key:match("script%.(.+)") - print(_get_env_script(_package_getenvs(), shell, false)) + io.write(_get_env_script(_package_getenvs({bind = bnd}), shell, false)) elseif key:startswith("backup.") then local shell = key:match("backup%.(.+)") -- remove current environment variables first - print(_get_env_script(_package_getenvs(), shell, true)) - print(_get_env_script(os.getenvs(), shell, false)) + io.write(_get_env_script(_package_getenvs({bind = bnd}), shell, true)) + io.write(_get_env_script(os.getenvs(), shell, false)) end end diff --git a/xmake/scripts/xrepo-hook.psm1 b/xmake/scripts/xrepo-hook.psm1 index 53342fba0..2210f10ef 100644 --- a/xmake/scripts/xrepo-hook.psm1 +++ b/xmake/scripts/xrepo-hook.psm1 @@ -10,25 +10,39 @@ #> function Enter-XrepoEnvironment { [CmdletBinding()] - param(); + param( + [string]$bnd + ); begin { $script:xrepoOldEnvs = (Get-ChildItem -Path Env:); - & $Env:XMAKE_EXE lua private.xrepo.action.env.info config; - if (-not $?) { - Exit 1; - } + if ($bnd -eq $Null) { + & $Env:XMAKE_EXE lua private.xrepo.action.env.info config; + if (-not $?) { + Exit 1; + } - $xmakeColorTermBackup, $Env:XMAKE_COLORTERM = $Env:XMAKE_COLORTERM, "nocolor"; - $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; - Exit 1; - } + $xmakeColorTermBackup, $Env:XMAKE_COLORTERM = $Env:XMAKE_COLORTERM, "nocolor"; + $xrepoPrompt = (& $Env:XMAKE_EXE lua --quiet private.xrepo.action.env.info prompt | Out-String); + $Env:XMAKE_COLORTERM = $xmakeColorTermBackup; + if (-not $xrepoPrompt.StartsWith("[")) { + Write-Host "error: xmake.lua not found!"; + Exit 1; + } - $activateCommand = (& $Env:XMAKE_EXE lua private.xrepo.action.env.info script.powershell | Out-String); + $activateCommand = (& $Env:XMAKE_EXE lua --quiet private.xrepo.action.env.info script.powershell | Out-String); + } else { + $xmakeColorTermBackup, $Env:XMAKE_COLORTERM = $Env:XMAKE_COLORTERM, "nocolor"; + $xrepoPrompt = (& $Env:XMAKE_EXE lua --quiet private.xrepo.action.env.info prompt $bnd | Out-String); + $Env:XMAKE_COLORTERM = $xmakeColorTermBackup; + if (-not $xrepoPrompt.StartsWith("[")) { + Write-Host "error: invalid environment!"; + Exit 1; + } + + $activateCommand = (& $Env:XMAKE_EXE lua --quiet private.xrepo.action.env.info script.powershell $bnd | Out-String); + } Write-Verbose "[xrepo env script.powershell]`n$activateCommand"; Invoke-Expression -Command $activateCommand; |
