-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminilde.lua
More file actions
245 lines (208 loc) · 7.12 KB
/
minilde.lua
File metadata and controls
245 lines (208 loc) · 7.12 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
-- This file implements a 'mini' lde for the sake of bootstrapping an initial lde binary for a platform without requiring lde.
-- Dependencies:
-- - All: curl, tar
-- - Windows: Developer Mode or Administrator
local separator = package.config:sub(1, 1)
local function join(...)
return table.concat({ ... }, separator)
end
local isWindows = separator == '\\'
---@param path string
local function exists(path)
local ok, _, code = os.rename(path, path)
if not ok then
return code == 13 -- permission denied means it exists
end
return true
end
---@param dir string
local function mkdir(dir)
if exists(dir) then return end
os.execute(isWindows and ('mkdir "' .. dir .. '"') or ('mkdir -p "' .. dir .. '"'))
end
---@type fun(src: string, dest: string)
local function mklink(src, dest)
if exists(dest) then return end
os.execute(
isWindows and ('mklink /D "' .. dest .. '" "' .. src .. '"')
or ("ln -sf '" .. src .. "' '" .. dest .. "'")
)
end
---@type fun(handle: file*?): string?
local function readhandle(handle)
if not handle then return end
local content = handle:read("*a")
handle:close()
return content
end
---@type fun(path: string): string?
local function read(path)
return readhandle(io.open(path, "r"))
end
---@type fun(path: string, content: string)
local function write(path, content)
local file = io.open(path, "w")
if not file then return end
file:write(content)
file:close()
end
---@type fun(path: string)
local function rm(path)
os.execute(isWindows and ('rmdir /S /Q "' .. path .. '"') or ('rm -rf "' .. path .. '"'))
end
---@type fun(src: string, dest: string) # Recursive copy
local function copy(src, dest)
if not exists(src) then return end
os.execute(
isWindows and ('xcopy /E /I /Y "' .. src .. '" "' .. dest .. '"')
or ('cp -rL "' .. src .. '" "' .. dest .. '"')
)
end
---@type fun(b: string): any? # Tiny json decoder with very basic support for what we will use in lde.json files
local function jsonDecode(b)
local c = 0; local function d(e)
local f, g, m = b:find("^%s*", c)
if f then c = g + 1 end; f, g, m = b:find(e, c)
if f then
c = g + 1; return m or true
end
end; local h, i; local function j()
local n = d("^(%d+%.?%d*)"); if n then return tonumber(n) end
return d("^\"([^\"]*)\"") or h() or i() or d("^true") or (d("^false") and false)
end; function h()
if not d("^{") then return end; local k = {}
while not d("^}") do
local l = d("^\"([^\"]*)\"")
d("^:"); k[l] = j(); d("^,")
end; return k
end; function i()
if not d("^%[") then return end; local k = {}
while not d("^%]") do
k[#k + 1] = j()
d("^,")
end; return k
end; return h()
end
local args = { ... }
local function pop() return table.remove(args, 1) end
---@alias minilde.dep
--- | { path: string }
--- | { git: string }
local tmpBase = os.getenv("TEMP") or os.getenv("TMPDIR") or "/tmp"
local tmpLDEDir = join(tmpBase, "lde")
local ffi = require("ffi")
local setenv ---@type fun(name: string, value: string)
local chdir ---@type fun(dir: string)
local getcwd ---@type fun(): string
if isWindows then
ffi.cdef [[int _putenv_s(const char *name, const char *value);]]
setenv = function(name, value) ffi.C._putenv_s(name, value) end
ffi.cdef [[int _chdir(const char *dirname);]]
chdir = function(dir) ffi.C._chdir(dir) end
ffi.cdef [[int _getcwd(char *buffer, size_t size);]]
getcwd = function()
local buffer = ffi.new("char[?]", 1024)
ffi.C._getcwd(buffer, 1024)
return ffi.string(buffer)
end
else
ffi.cdef [[int setenv(const char *name, const char *value, int overwrite);]]
setenv = function(name, value) ffi.C.setenv(name, value, 1) end
ffi.cdef [[int chdir(const char *path);]]
chdir = function(dir) ffi.C.chdir(dir) end
ffi.cdef [[char *getcwd(char *buf, size_t size);]]
getcwd = function()
local buffer = ffi.new("char[?]", 1024)
ffi.C.getcwd(buffer, 1024)
return ffi.string(buffer)
end
end
---@param packagePath string
---@param targetDir string
local function buildPackage(packagePath, targetDir)
local config = jsonDecode(assert(read(join(packagePath, "lde.json")) or read(join(packagePath, "lpm.json")),
"No lde.json at " .. packagePath)) --[[@as { name: string, dependencies: { [string]: minilde.dep } }]]
mkdir(targetDir)
if exists(join(packagePath, "build.lua")) then
local outputDir = join(targetDir, config.name)
copy(join(packagePath, "src"), outputDir)
setenv("LDE_OUTPUT_DIR", outputDir)
setenv("LPM_OUTPUT_DIR", outputDir)
---@alias minilde.build { outDir: string }
---@class minilde.build
local build = {}
build.__index = build
---@format disable-next
do
function build:fetch(url) return assert(readhandle(io.popen("curl -sL " .. url)), "failed to fetch " .. url) end
function build:write(rel, content) write(join(outputDir, rel), content) end
function build:read(rel) return read(join(outputDir, rel)) end
function build:extract(rel, dest) mkdir(join(outputDir, dest)); os.execute('tar -xzf "' .. join(outputDir, rel) .. '" -C "' .. join(outputDir, dest) .. '"') end
function build:copy(rel, dest) copy(join(outputDir, rel), join(outputDir, dest)) end
function build:delete(rel) rm(join(outputDir, rel)) end
function build:move(rel, dest) os.rename(join(outputDir, rel), join(outputDir, dest)) end
function build:exists(rel) return exists(join(outputDir, rel)) end
function build:sh(cmd)
local res = os.execute(cmd)
assert(res == 0 or res == true, "failed to execute " .. cmd)
end
end
package.loaded["lde-build"] = setmetatable({ outDir = outputDir }, build)
local oldDir = getcwd()
chdir(packagePath)
dofile(join(packagePath, "build.lua"))
chdir(oldDir)
else
mklink(join(packagePath, "src"), join(targetDir, config.name))
end
if not config.dependencies then return end
for name, dep in pairs(config.dependencies) do
---@format disable-next
if dep.path then
buildPackage(join(packagePath, dep.path), targetDir)
elseif dep.git then -- downloads to tmpLDEDir/<name> then build to target
local tarballUrl = dep.git .. "/archive/master.tar.gz"
os.execute("curl -s -L " .. tarballUrl .. " -o " .. join(tmpLDEDir, "tar", name))
mkdir(join(tmpLDEDir, "git", name))
os.execute("tar -xzf " .. join(tmpLDEDir, "tar", name) .. " --strip-components=1 -C " .. join(tmpLDEDir, "git", name))
buildPackage(join(tmpLDEDir, "git", name), targetDir)
else
error("Unknown dependency type: " .. name)
end
end
return config
end
local function build()
mkdir(tmpLDEDir)
mkdir(join(tmpLDEDir, "tar"))
mkdir(join(tmpLDEDir, "git"))
local cwd = getcwd()
return buildPackage(cwd, join(cwd, "target"))
end
if #args == 0 then
print("Usage: minilde <command>")
print("Commands:")
print(" run: build and run the package")
return
end
if pop() == "run" then
local config = assert(build())
local cwd = getcwd()
package.path = join(cwd, "target", "?.lua") .. ";" ..
join(cwd, "target", "?", "init.lua") .. ";" ..
package.path
local extraArgs = {}
local foundSep = false
for _, v in ipairs(args) do
if foundSep then
extraArgs[#extraArgs + 1] = v
elseif v == "--" then
foundSep = true
end
end
_G.arg = extraArgs
local chunk = loadfile(join(cwd, "target", config.name, "init.lua"))
if chunk then
chunk(unpack(extraArgs))
end
end