summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2021-11-05 20:08:05 +0800
committerGitHub <[email protected]>2021-11-05 20:08:05 +0800
commit56d44a8672492290c5ed57ee0cbb54e546592e37 (patch)
tree146360b1dca54f036f190f61e04548ff372a1782 /xmake
parent3cb2ad7165bb7460b62578782ed43d24396edd5e (diff)
parente79e2886eb22d78910a6c52c5b9f77c698d5d8b9 (diff)
Merge pull request #1806 from xq114/dev
improve env scripts to support binding
Diffstat (limited to 'xmake')
-rw-r--r--xmake/modules/private/xrepo/action/env.lua38
-rw-r--r--xmake/scripts/xrepo-hook.psm140
2 files changed, 49 insertions, 29 deletions
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;