Merge pull request #145 from Meoo/master
Fix #39, working tokens and wildcards in the same path
This commit is contained in:
commit
236dd35f62
@ -743,23 +743,20 @@
|
||||
premake.field.kind("directory", {
|
||||
paths = true,
|
||||
store = function(field, current, value, processor)
|
||||
if string.sub(value, 1, 2) == "%{" then
|
||||
return value
|
||||
elseif value:find("*") then
|
||||
value = os.matchdirs(value)
|
||||
for i, file in ipairs(value) do
|
||||
value[i] = path.getabsolute(value[i])
|
||||
end
|
||||
return value
|
||||
else
|
||||
return path.getabsolute(value)
|
||||
end
|
||||
return path.getabsolute(value)
|
||||
end,
|
||||
remove = function(field, current, value, processor)
|
||||
return path.getabsolute(value)
|
||||
end,
|
||||
compare = function(field, a, b, processor)
|
||||
return (a == b)
|
||||
end,
|
||||
|
||||
translate = function(field, current, _, processor)
|
||||
if current:find("*") then
|
||||
return os.matchdirs(current)
|
||||
end
|
||||
return { current }
|
||||
end
|
||||
})
|
||||
|
||||
@ -773,23 +770,20 @@
|
||||
premake.field.kind("file", {
|
||||
paths = true,
|
||||
store = function(field, current, value, processor)
|
||||
if string.sub(value, 1, 2) == "%{" then
|
||||
return value
|
||||
elseif value:find("*") then
|
||||
value = os.matchfiles(value)
|
||||
for i, file in ipairs(value) do
|
||||
value[i] = path.getabsolute(value[i])
|
||||
end
|
||||
return value
|
||||
else
|
||||
return path.getabsolute(value)
|
||||
end
|
||||
return path.getabsolute(value)
|
||||
end,
|
||||
remove = function(field, current, value, processor)
|
||||
return path.getabsolute(value)
|
||||
end,
|
||||
compare = function(field, a, b, processor)
|
||||
return (a == b)
|
||||
end,
|
||||
|
||||
translate = function(field, current, _, processor)
|
||||
if current:find("*") then
|
||||
return os.matchfiles(current)
|
||||
end
|
||||
return { current }
|
||||
end
|
||||
})
|
||||
|
||||
@ -877,6 +871,16 @@
|
||||
end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
translate = function(field, current, _, processor)
|
||||
if not processor then
|
||||
return { current }
|
||||
end
|
||||
for k, v in pairs(current) do
|
||||
current[k] = processor(field, v, nil)[1]
|
||||
end
|
||||
return { current }
|
||||
end
|
||||
})
|
||||
|
||||
@ -957,6 +961,19 @@
|
||||
end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
translate = function(field, current, _, processor)
|
||||
if not processor then
|
||||
return { current }
|
||||
end
|
||||
local ret = {}
|
||||
for _, value in ipairs(current) do
|
||||
for _, processed in ipairs(processor(field, value, nil)) do
|
||||
table.insert(ret, processed)
|
||||
end
|
||||
end
|
||||
return { ret }
|
||||
end
|
||||
})
|
||||
|
||||
|
@ -56,27 +56,30 @@
|
||||
-- The configuration set to query.
|
||||
-- @param field
|
||||
-- The definition of field to be queried.
|
||||
-- @param context
|
||||
-- @param filter
|
||||
-- A list of lowercase context terms to use during the fetch. Only those
|
||||
-- blocks with terms fully contained by this list will be considered in
|
||||
-- determining the returned value. Terms should be lower case to make
|
||||
-- the context filtering case-insensitive.
|
||||
-- @param ctx
|
||||
-- The context that will be used for detoken.expand
|
||||
-- @return
|
||||
-- The requested value.
|
||||
---
|
||||
|
||||
function configset.fetch(cset, field, context)
|
||||
context = context or {}
|
||||
function configset.fetch(cset, field, filter, ctx)
|
||||
filter = filter or {}
|
||||
ctx = ctx or {}
|
||||
|
||||
if p.field.merges(field) then
|
||||
return configset._fetchMerged(cset, field, context)
|
||||
return configset._fetchMerged(cset, field, filter, ctx)
|
||||
else
|
||||
return configset._fetchDirect(cset, field, context)
|
||||
return configset._fetchDirect(cset, field, filter, ctx)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function configset._fetchDirect(cset, field, filter)
|
||||
function configset._fetchDirect(cset, field, filter, ctx)
|
||||
local abspath = filter.files
|
||||
local basedir
|
||||
|
||||
@ -101,6 +104,11 @@
|
||||
if type(value) == "table" then
|
||||
value = table.deepcopy(value)
|
||||
end
|
||||
-- Detoken
|
||||
if field.tokens and ctx.environ then
|
||||
value = p.detoken.expand(value, ctx.environ, field, ctx._basedir)
|
||||
end
|
||||
|
||||
return value
|
||||
end
|
||||
end
|
||||
@ -108,17 +116,21 @@
|
||||
filter.files = abspath
|
||||
|
||||
if cset.parent then
|
||||
return configset._fetchDirect(cset.parent, field, filter)
|
||||
return configset._fetchDirect(cset.parent, field, filter, ctx)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function configset._fetchMerged(cset, field, filter)
|
||||
function configset._fetchMerged(cset, field, filter, ctx)
|
||||
local result = {}
|
||||
|
||||
local function remove(patterns)
|
||||
for i = 1, #patterns do
|
||||
local pattern = patterns[i]
|
||||
for _, pattern in ipairs(patterns) do
|
||||
-- Detoken
|
||||
if field.tokens and ctx.environ then
|
||||
pattern = p.detoken.expand(pattern, ctx.environ, field, ctx._basedir)
|
||||
end
|
||||
pattern = path.wildcards(pattern):lower()
|
||||
|
||||
local j = 1
|
||||
while j <= #result do
|
||||
@ -134,7 +146,7 @@
|
||||
end
|
||||
|
||||
if cset.parent then
|
||||
result = configset._fetchMerged(cset.parent, field, filter)
|
||||
result = configset._fetchMerged(cset.parent, field, filter, ctx)
|
||||
end
|
||||
|
||||
local abspath = filter.files
|
||||
@ -159,7 +171,23 @@
|
||||
end
|
||||
|
||||
local value = block[key]
|
||||
-- If value is an object, return a copy of it so that any
|
||||
-- changes later made to it by the caller won't alter the
|
||||
-- original value (that was a tough bug to find)
|
||||
if type(value) == "table" then
|
||||
value = table.deepcopy(value)
|
||||
end
|
||||
|
||||
if value then
|
||||
-- Detoken
|
||||
if field.tokens and ctx.environ then
|
||||
value = p.detoken.expand(value, ctx.environ, field, ctx._basedir)
|
||||
end
|
||||
-- Translate
|
||||
if field and p.field.translates(field) then
|
||||
value = p.field.translate(field, value)
|
||||
end
|
||||
|
||||
result = p.field.merge(field, result, value)
|
||||
end
|
||||
end
|
||||
@ -328,6 +356,7 @@
|
||||
table.insert(cset.blocks, block)
|
||||
cset.current = block
|
||||
|
||||
-- TODO This comment is not completely valid anymore
|
||||
-- This needs work; right now it is hardcoded to only work for lists.
|
||||
-- To support removing from keyed collections, I first need to figure
|
||||
-- out how to move the wildcard():lower() bit into the value
|
||||
@ -338,11 +367,6 @@
|
||||
-- api.remove() needs to get pushed down to here (or field).
|
||||
|
||||
values = p.field.remove(field, {}, values)
|
||||
for i, value in ipairs(values) do
|
||||
if type(value) == "string" then
|
||||
values[i] = path.wildcards(value):lower()
|
||||
end
|
||||
end
|
||||
|
||||
-- add a list of removed values to the block
|
||||
current = cset.current
|
||||
|
@ -172,13 +172,8 @@
|
||||
-- If there is a matching field, then go fetch the aggregated value
|
||||
-- from my configuration set, and then cache it future lookups.
|
||||
|
||||
local value = configset.fetch(ctx._cfgset, field, ctx.terms)
|
||||
local value = configset.fetch(ctx._cfgset, field, ctx.terms, ctx)
|
||||
if value then
|
||||
-- do I need to expand tokens?
|
||||
if field and field.tokens then
|
||||
value = p.detoken.expand(value, ctx.environ, field, ctx._basedir)
|
||||
end
|
||||
|
||||
-- store the result for later lookups
|
||||
ctx[key] = value
|
||||
end
|
||||
|
@ -105,7 +105,7 @@
|
||||
|
||||
function expandvalue(value)
|
||||
if type(value) ~= "string" then
|
||||
return
|
||||
return value
|
||||
end
|
||||
|
||||
local count
|
||||
|
@ -371,3 +371,20 @@
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
function field.translate(f, value)
|
||||
local processor = field.accessor(f, "translate")
|
||||
if processor then
|
||||
return processor(f, value, nil)[1]
|
||||
else
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function field.translates(f)
|
||||
return (field.accessor(f, "translate") ~= nil)
|
||||
end
|
||||
|
||||
|
@ -367,13 +367,8 @@
|
||||
if not field then
|
||||
ctx[key] = rawget(ctx, key)
|
||||
else
|
||||
local value = p.configset.fetch(cset, field, terms)
|
||||
local value = p.configset.fetch(cset, field, terms, ctx)
|
||||
if value then
|
||||
-- do I need to expand tokens?
|
||||
if field and field.tokens then
|
||||
value = p.detoken.expand(value, ctx.environ, field, ctx._basedir)
|
||||
end
|
||||
|
||||
ctx[key] = value
|
||||
end
|
||||
end
|
||||
|
@ -28,6 +28,7 @@ return {
|
||||
"project/test_eachconfig.lua",
|
||||
"project/test_getconfig.lua",
|
||||
"project/test_location.lua",
|
||||
"project/test_sources.lua",
|
||||
"project/test_vpaths.lua",
|
||||
|
||||
-- Configuration object tests
|
||||
|
93
tests/project/test_sources.lua
Normal file
93
tests/project/test_sources.lua
Normal file
@ -0,0 +1,93 @@
|
||||
--
|
||||
-- tests/project/test_sources.lua
|
||||
-- Automated test suite for the source tree, including tokens and wildcards.
|
||||
-- Copyright (c) 2011-2013 Jason Perkins and the Premake project
|
||||
--
|
||||
|
||||
local suite = test.declare("project_sources")
|
||||
local project = premake.project
|
||||
|
||||
|
||||
--
|
||||
-- Setup and teardown
|
||||
--
|
||||
|
||||
local sln, prj
|
||||
|
||||
local cwd = os.getcwd()
|
||||
local oldcwd
|
||||
|
||||
function suite.setup()
|
||||
sln, prj = test.createsolution()
|
||||
|
||||
-- We change the directory to get nice relative paths
|
||||
oldcwd = os.getcwd()
|
||||
os.chdir(cwd)
|
||||
|
||||
-- Create a token to be used in search paths
|
||||
premake.api.register { name = "mytoken", kind = "string", scope = "config" }
|
||||
mytoken "test"
|
||||
end
|
||||
|
||||
function suite.teardown()
|
||||
mytoken = nil
|
||||
os.chdir(oldcwd)
|
||||
end
|
||||
|
||||
local function run()
|
||||
local cfg = test.getconfig(prj, "Debug")
|
||||
|
||||
local files = {}
|
||||
for _, file in ipairs(cfg.files) do
|
||||
table.insert(files, path.getrelative(cwd, file))
|
||||
end
|
||||
|
||||
return files
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- Test single file
|
||||
--
|
||||
|
||||
function suite.SingleFile()
|
||||
files { "test_sources.lua" }
|
||||
test.isequal({"test_sources.lua"}, run())
|
||||
end
|
||||
|
||||
--
|
||||
-- Test tokens
|
||||
--
|
||||
|
||||
function suite.SingleFileWithToken()
|
||||
files { "%{cfg.mytoken}_sources.lua" }
|
||||
test.isequal({"test_sources.lua"}, run())
|
||||
end
|
||||
|
||||
--
|
||||
-- Test wildcards
|
||||
--
|
||||
|
||||
function suite.FilesWithWildcard()
|
||||
files { "test_*.lua" }
|
||||
test.contains("test_sources.lua", run())
|
||||
end
|
||||
|
||||
function suite.FilesWithRecursiveWildcard()
|
||||
files { "../**_sources.lua" }
|
||||
test.contains("test_sources.lua", run())
|
||||
end
|
||||
|
||||
--
|
||||
-- Test wildcards and tokens combined
|
||||
--
|
||||
|
||||
function suite.FilesWithWildcardAndToken()
|
||||
files { "%{cfg.mytoken}_*.lua" }
|
||||
test.contains("test_sources.lua", run())
|
||||
end
|
||||
|
||||
function suite.FilesWithRecursiveWildcardAndToken()
|
||||
files { "../**/%{cfg.mytoken}_sources.lua" }
|
||||
test.contains("test_sources.lua", run())
|
||||
end
|
Reference in New Issue
Block a user