Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorJason Perkins <starkos@industriousone.com>2011-06-02 23:26:15 +0400
committerJason Perkins <starkos@industriousone.com>2011-06-02 23:26:15 +0400
commit2580bd7798aa41ae14fd1503ed0534aaec7f94ba (patch)
tree6f10aba5be648e589de5bdda6ce8b529f44ae52c /src/base
parenta315c34f15f0b90eb2c611a676cabe07b2a1db5b (diff)
Added support for key-value fields to the API
Diffstat (limited to 'src/base')
-rw-r--r--src/base/api.lua58
-rw-r--r--src/base/bake.lua52
-rw-r--r--src/base/table.lua21
3 files changed, 106 insertions, 25 deletions
diff --git a/src/base/api.lua b/src/base/api.lua
index 18369ba..b78ad19 100644
--- a/src/base/api.lua
+++ b/src/base/api.lua
@@ -378,6 +378,13 @@
kind = "list",
scope = "config",
},
+
+ vpaths =
+ {
+ kind = "keyvalue",
+ scope = "container",
+ },
+
}
@@ -530,6 +537,43 @@
end
+--
+-- Adds values to a key-value field of a solution/project/configuration. `ctype`
+-- specifies the container type (see premake.getobject) for the field.
+--
+
+ function premake.setkeyvalue(ctype, fieldname, value)
+ local container, err = premake.getobject(ctype)
+ if not container then
+ error(err, 4)
+ end
+
+ if not container[fieldname] then
+ container[fieldname] = { }
+ end
+
+ if type(value) ~= "table" then
+ error("invalid value; table expected", 4)
+ end
+
+ local result = container[fieldname]
+
+ local function doinsert(tbl, errordepth)
+ for key,value in pairs(tbl) do
+ if type(value) == "table" then
+ doinsert(value, errordepth + 1)
+ elseif type(key) == "string" and type(value) == "string" then
+ result[key] = value
+ else
+ error("invalid value; both key and value must be a string", errordepth)
+ end
+ end
+ end
+
+ doinsert(value, 4)
+ return container[fieldname]
+ end
+
--
-- Set a new value for a string field of a solution/project/configuration. `ctype`
@@ -567,23 +611,25 @@
local scope = premake.fields[name].scope
local allowed = premake.fields[name].allowed
- if ((kind == "string" or kind == "path") and value) then
+ if (kind == "string" or kind == "path") and value then
if type(value) ~= "string" then
error("string value expected", 3)
end
end
- if (kind == "string") then
+ if kind == "string" then
return premake.setstring(scope, name, value, allowed)
- elseif (kind == "path") then
+ elseif kind == "path" then
if value then value = path.getabsolute(value) end
return premake.setstring(scope, name, value)
- elseif (kind == "list") then
+ elseif kind == "list" then
return premake.setarray(scope, name, value, allowed)
- elseif (kind == "dirlist") then
+ elseif kind == "dirlist" then
return premake.setdirarray(scope, name, value)
- elseif (kind == "filelist") then
+ elseif kind == "filelist" then
return premake.setfilearray(scope, name, value)
+ elseif kind == "keyvalue" then
+ return premake.setkeyvalue(scope, name, value)
end
end
diff --git a/src/base/bake.lua b/src/base/bake.lua
index aa47985..8c7ca77 100644
--- a/src/base/bake.lua
+++ b/src/base/bake.lua
@@ -142,29 +142,43 @@
-- The source object, containing the settings to added to the destination.
--
+ local function mergefield(kind, dest, src)
+ local tbl = dest or { }
+ if kind == "keyvalue" then
+ for key, value in pairs(src) do
+ tbl[key] = value
+ end
+ else
+ for _, item in ipairs(src) do
+ if not tbl[item] then
+ table.insert(tbl, item)
+ tbl[item] = item
+ end
+ end
+ end
+ return tbl
+ end
+
local function mergeobject(dest, src)
- if not src then return end
- for field, value in pairs(src) do
- if not nocopy[field] then
- if type(value) == "table" then
- -- merge two lists, removing any duplicates along the way
- local tbl = dest[field] or { }
-
- if field == 'terms' then
- for term_key,term_value in pairs(value)do
- tbl[term_key]=term_value
- end
+ -- if there's nothing to add, quick out
+ if not src then
+ return
+ end
+
+ for fieldname, value in pairs(src) do
+ if not nocopy[fieldname] then
+ -- fields that are included in the API are merged...
+ local field = premake.fields[fieldname]
+ if field then
+ if type(value) == "table" then
+ dest[fieldname] = mergefield(field.kind, dest[fieldname], value)
else
- for _, item in ipairs(value) do
- if not tbl[item] then
- table.insert(tbl, item)
- tbl[item] = item
- end
- end
+ dest[fieldname] = value
end
- dest[field] = tbl
+
+ -- ...everything else is just copied as-is
else
- dest[field] = value
+ dest[fieldname] = value
end
end
end
diff --git a/src/base/table.lua b/src/base/table.lua
index 8ca0d08..3aeb962 100644
--- a/src/base/table.lua
+++ b/src/base/table.lua
@@ -116,6 +116,27 @@
--
+-- Adds the key-value associations from one table into another
+-- and returns the resulting merged table.
+--
+
+ function table.merge(...)
+ local result = { }
+ for _,t in ipairs(arg) do
+ if type(t) == "table" then
+ for k,v in pairs(t) do
+ result[k] = v
+ end
+ else
+ error("invalid value")
+ end
+ end
+ return result
+ end
+
+
+
+--
-- Translates the values contained in array, using the specified
-- translation table, and returns the results in a new array.
--