-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkg.lua
145 lines (123 loc) · 2.64 KB
/
pkg.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---@type table?
local pkg = package.loaded.pkg
if type(pkg) == "table" then
return pkg
end
pkg = {}
package.loaded.pkg = pkg
---@alias pkg.Path string|table
local CWD = {} -- current working directory
local os_ext = {
Windows = {"dll"},
Linux = {"so"},
OSX = {"dylib", "so"},
}
local exts = os_ext[jit.os]
---@param a pkg.Path
---@param b string
---@return string
local function join(a, b)
if a == CWD then
return b
end
return ("%s/%s"):format(a, b)
end
function pkg.reset()
---@type pkg.Path[]
pkg.lua_paths = {}
---@type pkg.Path[]
pkg.c_paths = {}
end
pkg.reset()
---@param t pkg.Path[]
---@param v pkg.Path
---@return number?
local function indexof(t, v)
for i, _v in ipairs(t) do
if _v == v then
return i
end
end
end
---@param path string?
function pkg.add(path)
path = path or CWD
if not indexof(pkg.lua_paths, path) then
table.insert(pkg.lua_paths, path)
end
end
---@param path string?
function pkg.addc(path)
path = path or CWD
if not indexof(pkg.c_paths, path) then
table.insert(pkg.c_paths, path)
end
end
---@param path string?
function pkg.remove(path)
local index = indexof(pkg.lua_paths, path or CWD)
if index then
table.remove(pkg.lua_paths, index)
end
end
---@param path string?
function pkg.removec(path)
local index = indexof(pkg.c_paths, path or CWD)
if index then
table.remove(pkg.c_paths, index)
end
end
---@return string
function pkg.compile_path()
local out = {}
for i = 1, #pkg.lua_paths do
local p = pkg.lua_paths[i]
table.insert(out, join(p, "?.lua"))
table.insert(out, join(p, "?/init.lua"))
end
return table.concat(out, ";")
end
---@return string
function pkg.compile_cpath()
local out = {}
for i = 1, #pkg.c_paths do
local p = pkg.c_paths[i]
for _, ext in ipairs(exts) do
table.insert(out, join(p, ("?.%s"):format(ext)))
end
end
return table.concat(out, ";")
end
function pkg.export_lua()
package.path = pkg.compile_path()
package.cpath = pkg.compile_cpath()
end
function pkg.export_love()
love.filesystem.setRequirePath(pkg.compile_path())
love.filesystem.setCRequirePath(pkg.compile_cpath())
end
---@param package_path string
---@param package_cpath string
function pkg.import(package_path, package_cpath)
for path in package_path:gmatch("([^;]*)?") do
if path == "" then
pkg.add()
else
pkg.add(path:match("^(.-)/?$"))
end
end
for path in package_cpath:gmatch("([^;]*)?") do
if path == "" then
pkg.addc()
else
pkg.addc(path:match("^(.-)/?$"))
end
end
end
function pkg.import_lua()
pkg.import(package.path, package.cpath)
end
function pkg.import_love()
pkg.import(love.filesystem.getRequirePath(), love.filesystem.getCRequirePath())
end
return pkg