lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Another solution to this problem:

-- make a wrapper

local function wrap(filename)
  local chunk = assert(io.open(filename)):read "*a"
  return string.format("_FILES['%s'] = function() %s end",
                              filename,          chunk)
end

local pfx = [[
local _FILES = {}
local _dofile = dofile
local function dofile = function(fn)
  if _FILES[fn] then return _FILES[fn]() end
  return _dofile(fn)
end
]]

local chunks = {pfx}
for _, fn in arg do table.insert(chunks, wrap(fn)) end

-- the first file must be the "main":

table.insert(chunks, string.format("dofile '%s'", arg[1]))

-- for testing, just dump the stuff to stdout

for _, fn in chunks do print(fn) end