From 5af95803a04619777fbe69e1193ed0978cb01550 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Wed, 28 Apr 2021 21:48:55 +0800 Subject: improve environment management on cmd --- scripts/xrepo.bat | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'scripts') diff --git a/scripts/xrepo.bat b/scripts/xrepo.bat index ff95b722a..a58f5ac02 100755 --- a/scripts/xrepo.bat +++ b/scripts/xrepo.bat @@ -1,2 +1,39 @@ -@echo off -xmake lua private.xrepo %* +@REM @echo off + +@set "XMAKE_EXE=%~dp0xmake.exe" +@if [%1]==[env] ( + if [%2]==[quit] ( + if defined XMAKE_PROMPT_BACKUP ( + call %XMAKE_ENV_BACKUP% + if %errorlevel% neq 0 exit /B %errorlevel% + set PROMPT=%XMAKE_PROMPT_BACKUP% + set XMAKE_ENV_BACKUP= + set XMAKE_PROMPT_BACKUP= + ) + goto :ENDXREPO + ) + if [%2]==[shell] ( + if defined XMAKE_PROMPT_BACKUP ( + call %XMAKE_ENV_BACKUP% + if %errorlevel% neq 0 exit /B %errorlevel% + set PROMPT=%XMAKE_PROMPT_BACKUP% + for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info prompt') do @set "PROMPT=%%i %XMAKE_PROMPT_BACKUP%" + ) else ( + set XMAKE_PROMPT_BACKUP=%PROMPT% + for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info prompt') do @set "PROMPT=%%i %PROMPT%" + ) + for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info envfile') do @( + set "XMAKE_ENV_BACKUP=%%i.bat" + @"%XMAKE_EXE%" lua private.xrepo.action.env.info backup.cmd 1>"%%i.bat" + ) + for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info envfile') do @( + @"%XMAKE_EXE%" lua private.xrepo.action.env.info script.cmd 1>"%%i.bat" + call "%%i.bat" + ) + goto :ENDXREPO + ) +) + +@call %XMAKE_EXE% lua private.xrepo %* + +:ENDXREPO -- cgit v1.3.1 From 947219bb58249dae0c1619157ad7bcc594add156 Mon Sep 17 00:00:00 2001 From: xq114 <1140735506@qq.com> Date: Thu, 29 Apr 2021 15:38:52 +0800 Subject: add detection for xmake.lua --- scripts/xrepo-hook.psm1 | 8 +++++++- scripts/xrepo.bat | 33 +++++++++++++++++++++++------- xmake/modules/private/xrepo/action/env.lua | 22 +++++++++++++------- 3 files changed, 48 insertions(+), 15 deletions(-) (limited to 'scripts') diff --git a/scripts/xrepo-hook.psm1 b/scripts/xrepo-hook.psm1 index ca63fb09b..22a726b97 100644 --- a/scripts/xrepo-hook.psm1 +++ b/scripts/xrepo-hook.psm1 @@ -15,6 +15,10 @@ function Enter-XrepoEnvironment { begin { $script:xrepoOldEnvs = (Get-ChildItem -Path Env:); $xrepoPrompt = (& $Env:XMAKE_EXE lua private.xrepo.action.env.info prompt | Out-String); + if (-not $?) { + Write-Host $xrepoPrompt + Exit 1 + } $activateCommand = (& $Env:XMAKE_EXE lua private.xrepo.action.env.info script.powershell | Out-String); Write-Verbose "[xrepo env script.powershell]`n$activateCommand"; @@ -39,7 +43,9 @@ function Exit-XrepoEnvironment { param(); begin { - # Write-Host $script:xrepoOldEnvs; + ForEach ($p in (Get-ChildItem Env:)) { + [Environment]::SetEnvironmentVariable($p.Name, $Null); + } ForEach ($p in $script:xrepoOldEnvs) { [Environment]::SetEnvironmentVariable($p.Name, $p.Value); } diff --git a/scripts/xrepo.bat b/scripts/xrepo.bat index a58f5ac02..c59fd53d9 100755 --- a/scripts/xrepo.bat +++ b/scripts/xrepo.bat @@ -1,11 +1,13 @@ -@REM @echo off +@echo off @set "XMAKE_EXE=%~dp0xmake.exe" @if [%1]==[env] ( if [%2]==[quit] ( if defined XMAKE_PROMPT_BACKUP ( call %XMAKE_ENV_BACKUP% - if %errorlevel% neq 0 exit /B %errorlevel% + setlocal EnableDelayedExpansion + if !errorlevel! neq 0 exit /B !errorlevel! + endlocal set PROMPT=%XMAKE_PROMPT_BACKUP% set XMAKE_ENV_BACKUP= set XMAKE_PROMPT_BACKUP= @@ -15,18 +17,35 @@ if [%2]==[shell] ( if defined XMAKE_PROMPT_BACKUP ( call %XMAKE_ENV_BACKUP% - if %errorlevel% neq 0 exit /B %errorlevel% + setlocal EnableDelayedExpansion + if !errorlevel! neq 0 exit /B !errorlevel! + @%XMAKE_EXE% lua private.xrepo.action.env.info prompt 1>nul + if !errorlevel! neq 0 ( + echo error: xmake.lua not found^^! + exit /B !errorlevel! + ) + endlocal set PROMPT=%XMAKE_PROMPT_BACKUP% - for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info prompt') do @set "PROMPT=%%i %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 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 @set "PROMPT=%%i %PROMPT%" set XMAKE_PROMPT_BACKUP=%PROMPT% - for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info prompt') do @set "PROMPT=%%i %PROMPT%" ) - for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info envfile') do @( + for /f %%i in ('@%XMAKE_EXE% lua private.xrepo.action.env.info envfile') do @( set "XMAKE_ENV_BACKUP=%%i.bat" @"%XMAKE_EXE%" lua private.xrepo.action.env.info backup.cmd 1>"%%i.bat" ) - for /f %%i in ('%XMAKE_EXE% lua private.xrepo.action.env.info envfile') do @( + for /f %%i in ('@%XMAKE_EXE% lua private.xrepo.action.env.info envfile') do @( @"%XMAKE_EXE%" lua private.xrepo.action.env.info script.cmd 1>"%%i.bat" call "%%i.bat" ) diff --git a/xmake/modules/private/xrepo/action/env.lua b/xmake/modules/private/xrepo/action/env.lua index 7bd8dbd96..abcff7404 100644 --- a/xmake/modules/private/xrepo/action/env.lua +++ b/xmake/modules/private/xrepo/action/env.lua @@ -234,7 +234,7 @@ function _package_getenvs() end -- get environment setting script -function _get_env_script(envs, shell) +function _get_env_script(envs, shell, del) local prefix = "" local connector = "=" local suffix = "" @@ -249,10 +249,14 @@ function _get_env_script(envs, shell) suffix = "\"" end local ret = "" - local modified = {"PATH", "LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH", "ACLOCAL_PATH", "PKG_CONFIG_PATH", "CMAKE_PREFIX_PATH"} - for _, name in ipairs(modified) do - local value = envs[name] or default - ret = ret .. prefix .. name .. connector .. value .. suffix .. "\n" + if del then + for name, _ in pairs(envs) do + ret = ret .. prefix .. name .. connector .. default .. suffix .. "\n" + end + else + for name, value in pairs(envs) do + ret = ret .. prefix .. name .. connector .. value .. suffix .. "\n" + end end return ret end @@ -260,15 +264,19 @@ end -- get information of current virtual environment function info(key) if key == "prompt" then + assert(os.isfile(os.projectfile()), "xmake.lua not found!") print("[%s]", path.filename(os.projectdir())) elseif key == "envfile" then print("%s", os.tmpfile()) elseif key:startswith("script.") then local shell = key:match("script%.(.+)") - print(_get_env_script(_package_getenvs(), shell)) + print(_get_env_script(_package_getenvs(), shell, false)) elseif key:startswith("backup.") then local shell = key:match("backup%.(.+)") - print(_get_env_script(os.getenvs(), shell)) + + -- remove current environment variables first + print(_get_env_script(_package_getenvs(), shell, true)) + print(_get_env_script(os.getenvs(), shell, false)) end end -- cgit v1.3.1