From 32885d1673b50194ad7137261634775d772a4534 Mon Sep 17 00:00:00 2001 From: Tom van Dijck Date: Tue, 14 Jul 2015 08:48:46 -0700 Subject: [PATCH] Files can now be queried for configuration properties. --- src/base/context.lua | 15 +++++++++++++++ src/base/fileconfig.lua | 37 +++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/base/context.lua b/src/base/context.lua index 8b1e753f..52059670 100644 --- a/src/base/context.lua +++ b/src/base/context.lua @@ -98,6 +98,21 @@ end + +-- +-- Merges the list of terms from an existing context. +-- +-- @param ctx +-- The context to receive the copied terms. +-- @param src +-- The context containing the terms to copy. +-- + + function context.mergeFilters(ctx, src) + ctx.terms = table.merge(ctx.terms, table.deepcopy(src.terms)) + end + + -- -- Sets the base directory for path token expansion in non-path fields; such -- values will be made relative to this path. diff --git a/src/base/fileconfig.lua b/src/base/fileconfig.lua index 79897393..e783b174 100644 --- a/src/base/fileconfig.lua +++ b/src/base/fileconfig.lua @@ -43,11 +43,24 @@ -- function fileconfig.new(fname, prj) - local fcfg = {} + local environ = { } + local fcfg = context.new(prj, environ) + context.copyFilters(fcfg, prj) + context.addFilter(fcfg, "files", fname:lower()) + + for key, value in pairs(prj.environ) do + environ[key] = value + end + + environ.file = fcfg + context.compile(fcfg) + fcfg.project = prj fcfg.configs = {} fcfg.abspath = fname + context.basedir(fcfg, prj.location) + -- Most of the other path properties are computed on demand -- from the file's absolute path. @@ -86,7 +99,7 @@ local environ = {} local fsub = context.new(prj, environ) context.copyFilters(fsub, cfg) - context.addFilter(fsub, "files", fcfg.abspath:lower()) + context.mergeFilters(fsub, fcfg) fcfg.configs[cfg] = fsub @@ -98,9 +111,9 @@ environ[key] = value end - -- Make the context being built here accessible to tokens - - environ.file = fsub + for key, value in pairs(fcfg.environ) do + environ[key] = value + end -- finish the setup @@ -204,15 +217,16 @@ -- -- The indexer for the file configurations. If I have a path building function --- to fulfill the request, call it. Else this is a missing index so return nil. +-- to fulfill the request, call it. Else fall back to the context's own value lookups. -- local fcfg_mt = fileconfig.fcfg_mt - fcfg_mt.__index = function(file, key) + fcfg_mt.__index = function(fcfg, key) if type(fcfg_mt[key]) == "function" then - return fcfg_mt[key](file) + return fcfg_mt[key](fcfg) end + return context.__mt.__index(fcfg, key) end @@ -222,8 +236,11 @@ -- TODO: Would be great if this didn't require inside knowledge of context. -- - fileconfig.fsub_mt.__index = function(fcfg, key) - return fcfg_mt.__index(fcfg, key) or context.__mt.__index(fcfg, key) + fileconfig.fsub_mt.__index = function(fsub, key) + if type(fcfg_mt[key]) == "function" then + return fcfg_mt[key](fsub) + end + return context.__mt.__index(fsub, key) end