-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequire.lua
51 lines (38 loc) · 1.31 KB
/
require.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local oldRequire = require
require = {}
setmetatable(require, {__call = function(_,path) return oldRequire(path) end})
-- require.tree private functions
--
local lfs = love.filesystem
local cache = {}
local function toFSPath(requirePath) return requirePath:gsub("%.", "/") end
local function toRequirePath(fsPath) return fsPath:gsub('/','.') end
local function noExtension(path) return path:gsub('%.lua$', '') end
local function noEndDot(str) return str:gsub('%.$', '') end
function require.tree(requirePath)
if not cache[requirePath] then
local result = {}
local fsPath = toFSPath(requirePath)
local entries = lfs.enumerate(fsPath)
for _,entry in ipairs(entries) do
fsPath = toFSPath(requirePath .. '.' .. entry)
if lfs.isDirectory(fsPath) then
result[entry] = require.tree(toRequirePath(fsPath))
else
entry = noExtension(entry)
result[entry] = require(toRequirePath(requirePath .. '/' .. entry))
end
end
cache[requirePath] = result
end
return cache[requirePath]
end
function require.path(filePath)
return noEndDot(noExtension(filePath):match("(.-)[^%.]*$"))
end
function require.relative(...)
local args = {...}
local first, last = args[1], args[#args]
local path = require.path(first)
return require(path .. '.' .. last)
end