Skip to content

Commit 3a0dffa

Browse files
authored
Merge pull request #6808 from xmake-io/env
fix xrepo env #6806
2 parents 8dd95ee + cc32363 commit 3a0dffa

File tree

1 file changed

+39
-25
lines changed
  • xmake/modules/private/xrepo/action

1 file changed

+39
-25
lines changed

xmake/modules/private/xrepo/action/env.lua

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,39 @@ function _get_envsdir_builtin()
139139
return path.join(os.programdir(), "scripts", "xrepo", "envs")
140140
end
141141

142-
-- get bound environment or packages
143-
function _get_boundenv(opt)
142+
-- get envfironment files
143+
function _get_envfiles()
144+
local envfiles = {}
145+
for _, envsdir in ipairs({_get_envsdir(), _get_envsdir_builtin()}) do
146+
for _, envfile in ipairs(os.files(path.join(envsdir, "*.lua"))) do
147+
envfiles[path.basename(envfile)] = envfile
148+
end
149+
end
150+
return envfiles
151+
end
152+
153+
-- get bound environment files or packages
154+
function _get_boundenvs(opt)
155+
local packages = {}
156+
local files = {}
144157
local bind = (opt and opt.bind) or option.get("bind")
145158
if bind then
146-
for _, envsdir in ipairs({_get_envsdir(), _get_envsdir_builtin()}) do
147-
local envfile = path.join(envsdir, bind .. ".lua")
148-
if envfile and os.isfile(envfile) then
149-
return envfile
159+
local envfiles = _get_envfiles()
160+
for _, binditem in ipairs(bind:split(',', {plain = true})) do
161+
binditem = binditem:trim()
162+
if envfiles[binditem] then
163+
table.insert(files, envfiles[binditem])
164+
else
165+
table.insert(packages, binditem)
150166
end
151167
end
168+
else
169+
local program = option.get("program")
170+
if program then
171+
table.insert(packages, program)
172+
end
152173
end
153-
return bind
174+
return packages, files
154175
end
155176

156177
-- add values to environment variable
@@ -244,19 +265,13 @@ end
244265
-- get package environments
245266
function _package_getenvs(opt)
246267
local envs = os.getenvs()
247-
local boundenv = _get_boundenv(opt)
248-
local has_envfile = false
249-
local packages = nil
250-
if boundenv and os.isfile(boundenv) then
251-
has_envfile = true
252-
else
253-
packages = boundenv or option.get("program")
254-
end
268+
local packages, envfiles = _get_boundenvs(opt)
269+
local has_envfile = #envfiles > 0
255270
local oldir = os.curdir()
256-
if (os.isfile(os.projectfile()) and not boundenv) or has_envfile then
271+
if (os.isfile(os.projectfile()) and not option.get("bind")) or has_envfile then
257272
if has_envfile then
258273
_enter_project()
259-
table.insert(project.rcfiles(), boundenv)
274+
table.join2(project.rcfiles(), envfiles)
260275
end
261276
task.run("config", {}, {disable_dump = true})
262277
_toolchain_addenvs(envs)
@@ -267,10 +282,9 @@ function _package_getenvs(opt)
267282
if not has_envfile then
268283
_target_addenvs(envs)
269284
end
270-
elseif packages then
285+
elseif #packages > 0 then
271286
_enter_project()
272287
local envfile = os.tmpfile() .. ".lua"
273-
packages = packages:split(',', {plain = true})
274288
local file = io.open(envfile, "w")
275289
for _, requirename in ipairs(packages) do
276290
file:print("add_requires(\"%s\")", requirename)
@@ -337,13 +351,13 @@ end
337351
function _get_prompt(bnd)
338352
bnd = bnd or option.get("bind")
339353
local prompt
340-
local boundenv = _get_boundenv({bind = bnd})
341-
if boundenv then
342-
if os.isfile(boundenv) then
343-
prompt = path.basename(boundenv)
354+
local packages, envfiles = _get_boundenvs({bind = bnd})
355+
if #packages > 0 or #envfiles > 0 then
356+
if #packages == 0 and #envfiles == 1 then
357+
prompt = path.basename(envfiles[1])
344358
else
345-
prompt = boundenv:sub(1, math.min(#boundenv, 16))
346-
if boundenv ~= prompt then
359+
prompt = bnd:sub(1, math.min(#bnd, 16))
360+
if bnd ~= prompt then
347361
prompt = prompt .. ".."
348362
end
349363
end

0 commit comments

Comments
 (0)