luapack/plug/lua.lua

109 lines
2.3 KiB
Lua

lp.insPath {
'?.lua',
'?/init.lua',
}
local env = {
name = lp.name,
ver = lp.version,
copy = lp.copyright,
cache = '_c_',
req = '_r_',
mreq = '_m_', -- require by map
mtemp = ' function($req) -- $file\n$src\n end, -- $file\n',
pre = '--[=[=]=]',
regex = '([^\n]-)' .. 'require%s*%(?["\'](.-)["\']%)?%s*',
}
env.require = lp.strf('$req(%d)', env)
local base = lp.strf([[
$pre -- $name $ver
$pre -- $copy
$pre (function(m)
$pre -- Module cache
$pre local $cache = {}
%s$pre
$pre -- Load main module
$pre $require
$pre
$pre end) {
%s
$pre }
]], env)
local main = lp.strf([[
$pre
$pre -- Require function
$pre local function $req(i)
$pre -- Check in cache
$pre local c = $cache[i]
$pre if not c then
$pre -- Execute module function
$pre c = m[i]($req)
$pre
$pre -- And put it to cache
$pre $cache[i] = c
$pre end
$pre
$pre -- Return cached returned value
$pre return c
$pre end
]], env)
local mreq = lp.strf([[
$pre
$pre -- Require by map
$pre local function $mreq(m, r)
$pre -- Check if module exists
$pre if not m[r]
$pre then error('Cannot find module "'..r..'"')
$pre end
$pre
$pre -- Using default require
$pre return $req(m[r])
$pre end
]], env)
return function(op)
return {
name = 'Base Lua plugin',
load = function(e, ext, oext)
if ext == 'lua'
then return true -- load every lua file
end
end,
deps = function(e, opts)
return e.src:gsub(env.regex, function(bef, name)
name = name:gsub('%.', '/') -- lua.lua => lua/lua
if bef:match '%-%-'
then return '' end -- under comment
local d = lp.get(name, e, opts)
if d then
return bef.. env.require:format(d.i)
end
end)
end,
transform = function(e, opts)
if not e.entry then return end -- change only entry files
if e.reqs > 0 then
local m = ''
for i = 1, #opts.ext do
local v = opts.ext[opts.ext[i]] -- [i] => [file] => object
m = m.. lp.strf(lp.strf(env.mtemp, env), v)
end
return base:format(main, opts.ext[e.file].i, m)
end
end,
postbuild = function(e, opts)
local f, err = load(e.src, 'lint')
if err then
print(e.src)
error(err)
end
end,
}
end