Skip to content

Commit

Permalink
mode select working
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcawood committed Dec 10, 2024
1 parent 482d030 commit eb47c61
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 71 deletions.
2 changes: 1 addition & 1 deletion rt/unload/mf/Core/B/2.0.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
setenv("TOTO","set_in_B/2.0")
if (mode() == "unload") then
LmodError("Error in unload of B/2.0")
LmodError("Sucessfully raised mode error in unload of B/2.0")
end

2 changes: 1 addition & 1 deletion rt/unload/mf/Core/C/1.0.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ append_path{"PATH_TEST", "/last", mode={"load"}}
append_path{"PATH_TEST", "/unload_last", mode={"unload"}}

-- Test remove during specific modes
remove_path{"PATH_TEST", "/to_remove", mode={"unload"}}
--remove_path{"PATH_TEST", "/to_remove", mode={"unload"}}


--setenv{"AA", "X", mode={"load", "unload"}}
Expand Down
6 changes: 3 additions & 3 deletions src/MName.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ local s_rangeFuncT = { ["<="] = {func = l_lessthan_equal, name = "<="},
}


function M.new(self, sType, name, action, is, ie, mode)
function M.new(self, sType, name, action, is, ie)
--dbg.start{"Mname:new(",sType,")"}

local exact_match = cosmic:value("LMOD_EXACT_MATCH")
Expand Down Expand Up @@ -143,8 +143,6 @@ function M.new(self, sType, name, action, is, ie, mode)
o.__userName = (name or ""):trim():gsub("/+$",""):gsub("%.lua$","")
end

o.__mode = mode

--dbg.fini("MName:new")
return o
end
Expand All @@ -168,6 +166,8 @@ end
-- @param sType The type which can be "entryT", "load", "mt"
-- @return An array of MName objects.
function M.buildA(self,sType, ...)


local argA = pack(...)
local a = {}

Expand Down
41 changes: 31 additions & 10 deletions src/MainControl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,22 @@ end
-- @param name the environment variable name.
-- @param value the environment variable value.
-- @param respect If true, then respect the old value.
function M.setenv(self, table) --name, value, respect)
local name = (table[1] or ""):trim()
local value = table[2]
local respect = table[3] or nil
function M.setenv(self, ...) --name, value, respect)
local name, value, respect

local firstArg = select(1, ...)
if type(firstArg) == "table" then
-- Format 2: arguments were passed as a table
local t = firstArg
name = (t[1] or ""):trim()
value = t[2]
respect = t[3] or nil
else
-- Format 1: arguments were passed as individual parameters
name, value, respect = ...
name = (name or ""):trim()
end

dbg.start{"MainControl:setenv(\"",name,"\", \"",value,"\", \"",
respect,"\")"}

Expand All @@ -340,7 +352,6 @@ function M.setenv(self, table) --name, value, respect)
dbg.fini("MainControl:setenv")
end


-------------------------------------------------------------------
-- Set an environment variable.
-- This function just sets the name with value in the current env.
Expand All @@ -361,11 +372,21 @@ end
-- @param name the environment variable name.
-- @param value the environment variable value.
-- @param respect If true, then respect the old value.
function M.unsetenv(self, table) --name, value, respect)

local name = (table[1] or ""):trim()
local value = table[2]
local respect = table[3] or nil
function M.unsetenv(self, ...) --name, value, respect)
local name, value, respect

local firstArg = select(1, ...)
if type(firstArg) == "table" then
-- Format 2: arguments were passed as a table
local t = firstArg
name = (t[1] or ""):trim()
value = t[2]
respect = t[3] or nil
else
-- Format 1: arguments were passed as individual parameters
name, value, respect = ...
name = (name or ""):trim()
end

dbg.start{"MainControl:unsetenv(\"",name,"\", \"",value,"\")"}

Expand Down
89 changes: 33 additions & 56 deletions src/modfuncs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,6 @@ end
-- Validate a function with only string module names table.
-- @param cmdName The command which is getting its arguments validated.
--local function l_validateArgsWithValue(cmdName, ...)
-- local argA = pack(...)

-- for i = 1, argA.n -1 do
-- local v = argA[i]
-- if (type(v) ~= "string") then
-- mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
-- return false
-- end
-- end

-- local v = argA[argA.n]
-- if (type(v) ~= "string" and type(v) ~= "number" and type(v) ~= "boolean") then
-- mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
-- return false
-- end
-- return true
--end

local function l_validateArgsWithValue(cmdName, table)

local n = table.n or #table
Expand Down Expand Up @@ -205,18 +187,6 @@ local function l_validateModules(cmdName, ...)
return allGood
end

--------------------------------------------------------------------------
-- The load function. It can be found in the following forms:
-- "load('name'); load('name/1.2'); load(atleast('name','3.2'))",
--function load_module(...)
-- dbg.start{"load_module(",l_concatTbl({...},", "),")"}
-- if (not l_validateModules("load",...)) then return {} end

-- dbg.print{"mcp:name(): ",mcp:name(),"\n"}
-- local b = mcp:load_usr(MName:buildA(mcp:MNameType(), ...))
-- dbg.fini("load_module")
-- return b
--end

--------------------------------------------------------------------------
-- Convert all function arguments to table form
Expand Down Expand Up @@ -263,11 +233,14 @@ function inTable(tbl, val)
end


--------------------------------------------------------------------------
-- The load function. It can be found in the following forms:
-- "load('name'); load('name/1.2'); load(atleast('name','3.2'))",
--function load_module(...)

function load_module(...)
dbg.start{"load_module(",l_concatTbl({...},", "),")"}

-- Convert arguments into a table, similar to how setenv is handled
local argTable
local mcp_old = mcp
mcp, argTable = list_2_Tbl(MCP, mcp, ...)
Expand All @@ -277,29 +250,13 @@ function load_module(...)
return {}
end

-- If a mode selector is provided, check if current evaluation mode matches
if argTable.mode then
local currentMode = mcp._mode
if (not inTable(argTable.mode, currentMode)) then
-- If the current mode is not allowed, skip this operation
mcp = mcp_old
dbg.fini("load_module")
return {}
end
end

-- Validate the modules as before
if (not l_validateModules("load", table.unpack(argTable))) then
mcp = mcp_old
dbg.fini("load_module")
return {}
end

dbg.print{"mcp:name(): ", mcp:name(), "\n"}

-- Pass mode information along to MName:buildA
-- We assume MName:buildA is modified to accept a mode parameter and forward it to M.new
local b = mcp:load_usr(MName:buildA(mcp:MNameType(), argTable, argTable.mode))
local b = mcp:load_usr(MName:buildA(mcp:MNameType(), table.unpack(argTable)))

mcp = mcp_old
dbg.fini("load_module")
Expand Down Expand Up @@ -423,17 +380,17 @@ end
function setenv(...)
dbg.start{"setenv(",l_concatTbl({...},", "),")"}

local table
local argTable
local mcp_old = mcp
mcp, table = list_2_Tbl(MCP, mcp, ...)
mcp, argTable = list_2_Tbl(MCP, mcp, ...)
if ( not mcp ) then
mcp = mcp_old
return
end

if (not l_validateArgsWithValue("setenv", table)) then return end
if (not l_validateArgsWithValue("setenv", argTable)) then return end

mcp:setenv(table)
mcp:setenv(table.unpack(argTable))
mcp = mcp_old
dbg.fini("setenv")
return
Expand All @@ -444,9 +401,19 @@ end
-- Unset the value of environment variable.
function unsetenv(...)
dbg.start{"unsetenv(",l_concatTbl({...},", "),")"}
if (not l_validateArgsWithValue("unsetenv",...)) then return end

mcp:unsetenv(...)
local argTable
local mcp_old = mcp
mcp, argTable = list_2_Tbl(MCP, mcp, ...)
if ( not mcp ) then
mcp = mcp_old
return
end

if (not l_validateArgsWithValue("unsetenv", argTable)) then return end

mcp:unsetenv(table.unpack(argTable))
mcp = mcp_old
dbg.fini("unsetenv")
return
end
Expand Down Expand Up @@ -958,8 +925,18 @@ end
-- not loaded. The reverse of an unload is a no-op.
function unload(...)
dbg.start{"unload(",l_concatTbl({...},", "),")"}
if (not l_validateModules("unload",...)) then return {} end
local b = unload_internal(MName:buildA("mt",...))

local argTable
local mcp_old = mcp
mcp, argTable = list_2_Tbl(MCP, mcp, ...)
if (not mcp) then
mcp = mcp_old
dbg.fini("unload_module")
return {}
end

if (not l_validateModules("unload", table.unpack(argTable))) then return {} end
local b = unload_internal(MName:buildA("mt",table.unpack(argTable)))
dbg.fini("unload")
return b
end
Expand Down

0 comments on commit eb47c61

Please sign in to comment.