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 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 -- 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 -- Load the required core modules that are shipped as part of Premake and
-- and expected to be present at startup. -- 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() function m.preloadModules()
for i = 1, #modules do for i = 1, #modules do
local name = modules[i] local name = modules[i]
local preload = name .. "/_preload.lua" local preloader = name .. "/_preload.lua"
local fn = os.locate("modules/" .. preload) or os.locate(preload) preloader = os.locate("modules/" .. preloader) or os.locate(preloader)
if preloader then
if fn then m._preloaded[name] = include(preloader)
include(fn) -- 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 else
require(name) require(name)
end end
@ -256,6 +267,24 @@
--- ---
function m.postBake() 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 end
@ -303,7 +332,6 @@
-- --
-- Script-side program entry point. -- Script-side program entry point.
-- --

View File

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