From d7bd1e381dae2038d7aebab3760ba3d08388a1e6 Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Wed, 11 Jun 2014 13:52:08 -0400 Subject: [PATCH] Added support for custom rule list variables (VS only) --- src/actions/vstudio/vs2010_vcxproj.lua | 13 ++- src/base/api.lua | 97 ++++++++++++++++----- tests/actions/vstudio/vc2010/test_files.lua | 52 +++++++++++ tests/testfx.lua | 1 + 4 files changed, 140 insertions(+), 23 deletions(-) diff --git a/src/actions/vstudio/vs2010_vcxproj.lua b/src/actions/vstudio/vs2010_vcxproj.lua index b96df9b6..1f1f87c6 100644 --- a/src/actions/vstudio/vs2010_vcxproj.lua +++ b/src/actions/vstudio/vs2010_vcxproj.lua @@ -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', 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', key, m.condition(fcfg.config), value, key) + end end end end diff --git a/src/base/api.lua b/src/base/api.lua index 20689f03..670c4439 100755 --- a/src/base/api.lua +++ b/src/base/api.lua @@ -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 diff --git a/tests/actions/vstudio/vc2010/test_files.lua b/tests/actions/vstudio/vc2010/test_files.lua index b1599786..796d728a 100755 --- a/tests/actions/vstudio/vc2010/test_files.lua +++ b/tests/actions/vstudio/vc2010/test_files.lua @@ -547,3 +547,55 @@ end + function suite.customRule_onListVars() + files { "hello.dae" } + filter "files:**.dae" + customRule "Animation" + customList { "ExtraDependencies", "File1", "File2" } + prepare() + test.capture [[ + + + File1 File2 + File1 File2 + + + ]] + 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 [[ + + + File1 File2 + File1 File2 File3 + + + ]] + end + + + function suite.customRule_onListVarsWithCustomFormat() + files { "hello.dae" } + filter "files:**.dae" + customRule "Animation" + customListFormat { "ExtraDependencies", ";" } + customList { "ExtraDependencies", "File1", "File2" } + prepare() + test.capture [[ + + + File1;File2 + File1;File2 + + + ]] + end \ No newline at end of file diff --git a/tests/testfx.lua b/tests/testfx.lua index c2ba6bf6..6246e34b 100644 --- a/tests/testfx.lua +++ b/tests/testfx.lua @@ -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