Added support for custom rule list variables (VS only)

This commit is contained in:
Jason Perkins 2014-06-11 13:52:08 -04:00
parent efe5f1e292
commit d7bd1e381d
4 changed files with 140 additions and 23 deletions

View File

@ -501,8 +501,17 @@
local condition = m.condition(cfg)
local fcfg = fileconfig.getconfig(file, cfg)
if fcfg and fcfg[var] then
local key = var:sub(9)
_x(3,'<%s %s>%s</%s>', key, m.condition(fcfg.config), fcfg[var], key)
local key = p.api.getCustomVarKey(var)
local value = fcfg[var]
if type(value) == "table" then
local fmt = p.api.getCustomListFormat(var)
value = table.concat(value, fmt[1])
end
if #value > 0 then
_x(3,'<%s %s>%s</%s>', key, m.condition(fcfg.config), value, key)
end
end
end
end

View File

@ -428,6 +428,26 @@
---
-- Reset the API system, clearing out any temporary or cached values.
-- Used by the automated testing framework to clear state between
-- individual test runs.
---
function api.reset()
-- Remove all custom variables
local vars = api.getCustomVars()
for i, var in ipairs(vars) do
local f = premake.field.get(var)
api.unregister(f)
end
-- Remove all custom list variable formats
api._customVarFormats = {}
end
--
-- Arrays are integer indexed tables; unlike lists, a new array value
-- will replace the old one, rather than merging both.
@ -939,27 +959,7 @@
--
-----------------------------------------------------------------------------
function customVar(value)
if type(value) ~= "table" or #value ~= 2 then
error { msg="invalid value for customVar()" }
end
local name = value[1]
local value = value[2]
local fieldName = "_custom_" .. name
local field = premake.field.get(fieldName)
if not field then
field = api.register {
name = fieldName,
scope = "config",
kind = "string"
}
end
_G[fieldName](value)
end
api._customVarFormats = {}
function api.getCustomVars()
local vars = {}
@ -970,3 +970,58 @@
end
return vars
end
function api.getCustomVarKey(var)
return var:sub(9)
end
function api.getCustomListFormat(var)
local key = api.getCustomVarKey(var)
return api._customVarFormats[key] or { " " }
end
function api.setCustomVar(name, kind, value)
local fieldName = "_custom_" .. name
local field = premake.field.get(fieldName)
if not field then
api.register {
name = fieldName,
scope = "config",
kind = kind
}
end
_G[fieldName](value)
end
function customVar(value)
if type(value) ~= "table" or #value ~= 2 then
error { msg="invalid value for customVar()" }
end
api.setCustomVar(value[1], "string", value[2])
end
function customList(value)
if type(value) ~= "table" or #value < 2 then
error { msg="invalid value for customList()" }
end
local name = value[1]
table.remove(value, 1)
api.setCustomVar(name, "list:string", value)
end
function customListFormat(value)
if type(value) ~= "table" or #value < 2 then
error { msg="invalid value for customListFormat()" }
end
local name = value[1]
table.remove(value, 1)
api._customVarFormats[name] = value
end

View File

@ -547,3 +547,55 @@
end
function suite.customRule_onListVars()
files { "hello.dae" }
filter "files:**.dae"
customRule "Animation"
customList { "ExtraDependencies", "File1", "File2" }
prepare()
test.capture [[
<ItemGroup>
<Animation Include="hello.dae">
<ExtraDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">File1 File2</ExtraDependencies>
<ExtraDependencies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">File1 File2</ExtraDependencies>
</Animation>
</ItemGroup>
]]
end
function suite.customRule_onPerConfigListVars()
files { "hello.dae" }
filter { "files:**.dae" }
customRule "Animation"
customList { "ExtraDependencies", "File1", "File2" }
filter { "files:**.dae", "configurations:Release" }
customList { "ExtraDependencies", "File3" }
prepare()
test.capture [[
<ItemGroup>
<Animation Include="hello.dae">
<ExtraDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">File1 File2</ExtraDependencies>
<ExtraDependencies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">File1 File2 File3</ExtraDependencies>
</Animation>
</ItemGroup>
]]
end
function suite.customRule_onListVarsWithCustomFormat()
files { "hello.dae" }
filter "files:**.dae"
customRule "Animation"
customListFormat { "ExtraDependencies", ";" }
customList { "ExtraDependencies", "File1", "File2" }
prepare()
test.capture [[
<ItemGroup>
<Animation Include="hello.dae">
<ExtraDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">File1;File2</ExtraDependencies>
<ExtraDependencies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">File1;File2</ExtraDependencies>
</Animation>
</ItemGroup>
]]
end

View File

@ -288,6 +288,7 @@
premake.eol("\n")
premake.escaper(nil)
premake.indent("\t")
premake.api.reset()
-- reset captured I/O values
test.value_openedfilename = nil