Embedded modules should now return "should load" test function from their _preload.lua script

Each _preload.lua should end by returning a function of the form `function (cfg) … end`, which should itself return true if the module is required by the provided configuration. For the D language module, this might look like `return function (cfg) return cfg.language == "D" end`
This commit is contained in:
Jason Perkins 2015-06-17 15:48:47 -04:00
parent 8f6d8e1d61
commit 3714abefde
2 changed files with 37 additions and 9 deletions

View File

@ -25,6 +25,11 @@
local m = p.main
-- Keep a table of modules that have been preloaded, and their associated
-- "should load" test functions.
m._preloaded = {}
---
-- Add a new module loader that knows how to use the Premake paths like
@ -89,18 +94,24 @@
---
-- Load the required core modules that are shipped as part of Premake
-- and expected to be present at startup.
-- Load the required core modules that are shipped as part of Premake and
-- expected to be present at startup. If a _preload.lua script is present,
-- that script is run and the return value (a "should load" test) is cached
-- to be called after baking is complete. Otherwise the module's main script
-- is run immediately.
---
function m.preloadModules()
for i = 1, #modules do
local name = modules[i]
local preload = name .. "/_preload.lua"
local fn = os.locate("modules/" .. preload) or os.locate(preload)
if fn then
include(fn)
local preloader = name .. "/_preload.lua"
preloader = os.locate("modules/" .. preloader) or os.locate(preloader)
if preloader then
m._preloaded[name] = include(preloader)
-- leave off until existing core modules can catch up
-- if not m._preloaded[name] then
-- p.warn("module '%s' should return function from _preload.lua", name)
-- end
else
require(name)
end
@ -256,6 +267,24 @@
---
function m.postBake()
local function shouldLoad(func)
for sln in p.global.eachSolution() do
for prj in p.solution.eachproject(sln) do
for cfg in p.project.eachconfig(prj) do
if func(cfg) then
return true
end
end
end
end
end
-- any modules need to load to support this project?
for module, func in pairs(m._preloaded) do
if not package.loaded[module] and shouldLoad(func) then
require(module)
end
end
end
@ -303,7 +332,6 @@
--
-- Script-side program entry point.
--

View File

@ -52,6 +52,6 @@
fname = fullPath or fname
if not io._includedFiles[fname] then
io._includedFiles[fname] = true
dofile(fname)
return dofile(fname)
end
end