Skip to content

Commit

Permalink
improve is_config
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 8, 2025
1 parent 279b1f0 commit 7a4c72e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
35 changes: 17 additions & 18 deletions xmake/core/project/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ function config._use_workingdir()
return use_workingdir
end

-- the current config is belong to the given config values?
function config._is_value(value, ...)
if value == nil then
return false
end

value = tostring(value)
for _, v in ipairs(table.pack(...)) do
-- escape '-'
v = tostring(v)
if value == v or value:find("^" .. v:gsub("%-", "%%-") .. "$") then
return true
end
end
return false
end

-- get the current given configuration
function config.get(name)
local value = nil
Expand Down Expand Up @@ -278,24 +295,6 @@ function config.is_cross()
return is_cross(config.plat(), config.arch())
end

-- the current config is belong to the given config values?
function config.is_value(name, ...)
local value = config.get(name)
if value == nil then
return false
end

value = tostring(value)
for _, v in ipairs(table.pack(...)) do
-- escape '-'
v = tostring(v)
if value == v or value:find("^" .. v:gsub("%-", "%%-") .. "$") then
return true
end
end
return false
end

-- dump the configure
function config.dump()
if not option.get("quiet") then
Expand Down
7 changes: 6 additions & 1 deletion xmake/core/project/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ end

-- the current config is belong to the given config values?
function project._api_is_config(interp, name, ...)
return config.is_value(name, ...)
local value = config.get(name)
local namespace = interp:namespace()
if value == nil and namespace then
value = config.get(namespace .. "::" .. name)
end
return config._is_value(value, ...)
end

-- some configs are enabled?
Expand Down
17 changes: 15 additions & 2 deletions xmake/core/sandbox/modules/is_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
-- @file is_config.lua
--

-- return module
return require("project/config").is_value
local config = require("project/config")
local sandbox = require("sandbox/sandbox")

return function (name, ...)
local namespace
local instance = sandbox.instance()
if instance then
namespace = instance:namespace()
end
local value = config.get(name)
if value == nil and namespace then
value = config.get(namespace .. "::" .. name)
end
return config._is_value(value, ...)
end

0 comments on commit 7a4c72e

Please sign in to comment.