Repository navigation
Expand file tree
/
Copy pathpacman.lua
More file actions
384 lines (325 loc) · 8.45 KB
/
Copy pathpacman.lua
File metadata and controls
384 lines (325 loc) · 8.45 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
-- pacman.lua (GitHub mode package manager)
local REPO = "https://raw.githubusercontent.com/PB-Kronos/CcShell/main/pkg"
local DB_PATH = "/var/pacman.db"
local download = false
-- =========================
-- Utilities
-- =========================
local function fetch(url)
local h = http.get(url)
if not h then return nil, "Failed to fetch: " .. url end
local data = h.readAll()
h.close()
return data
end
local function ensureDB()
if not fs.exists(DB_PATH) then
local f = fs.open(DB_PATH, "w")
f.write("{}")
f.close()
end
end
local function loadDB()
ensureDB()
local f = fs.open(DB_PATH, "r")
local data = f.readAll()
f.close()
return textutils.unserialize(data) or {}
end
local function saveDB(db)
local f = fs.open(DB_PATH, "w")
f.write(textutils.serialize(db))
f.close()
end
local function isInstalled(pkg, db)
db = db or loadDB()
return db[pkg] ~= nil
end
local function readRemoteTable(path)
local code = fetch(REPO .. "/" .. path)
if not code then
return nil, "missing"
end
local fn, err = load(code, "@" .. path, "t", {})
if not fn then
return nil, err
end
local ok, result = pcall(fn)
if not ok then
return nil, result
end
if type(result) ~= "table" then
return nil, "expected table"
end
return result
end
local function getPackageManifest(pkg)
return readRemoteTable(pkg .. "/manifest.lua")
end
local function extractDependencies(manifest)
local deps = manifest and (manifest.dependencies or manifest.depends or manifest.deps)
if not deps then
return {}
end
local result = {}
if type(deps) == "string" then
result[1] = deps
elseif type(deps) == "table" then
for _, dep in ipairs(deps) do
if type(dep) == "string" then
result[#result + 1] = dep
elseif type(dep) == "table" then
local name = dep.name or dep.package or dep.pkg or dep[1]
if name then
result[#result + 1] = name
end
end
end
end
return result
end
local function installDependencies(pkg, manifest, seen)
local deps = extractDependencies(manifest)
if #deps == 0 then
return true
end
seen = seen or {}
seen[pkg] = true
for _, dep in ipairs(deps) do
if not isInstalled(dep) then
if seen[dep] then
return false, "dependency cycle detected: " .. dep
end
local ok, err = install(dep, seen)
if not ok then
return false, err
end
end
end
return true
end
-- =========================
-- Execution environment
-- =========================
local function runPackage(code, name, ...)
local env = {
fs = fs,
shell = shell,
os = os,
http = http,
textutils = textutils,
term = term,
paintutils = paintutils,
colors = colors,
vector = vector,
sleep = sleep,
print = print,
pairs = pairs,
ipairs = ipairs,
tostring = tostring,
tonumber = tonumber,
error = error,
table = table,
read = read,
io = io,
_G = _G,
rednet = rednet,
settings = settings,
turtle = turtle,
disk = disk,
redstone = redstone,
gps = gps,
parallel = parallel,
peripheral = peripheral,
pocket = pocket,
help = help,
multishell = multishell,
pkg = pkg,
downloader = download,
sys = sys,
}
local fn, err = load(code, "@" .. name, "t", env)
if not fn then
return false, err
end
return pcall(fn, ...)
end
local function runRemote(path, ...)
local code, err = fetch(REPO .. "/" .. path)
if not code then
return false, err
end
return runPackage(code, path, ...)
end
-- =========================
-- Package operations
-- =========================
function install(pkg, seen, ...)
if type(seen) ~= "table" then
seen = nil
end
print("Installing package:", pkg)
local manifest, manifestErr = getPackageManifest(pkg)
if not manifest then
print("Warning: could not load manifest for", pkg, ":", manifestErr)
end
local okDeps, depErr = installDependencies(pkg, manifest, seen)
if not okDeps then
print("Dependency install failed:", depErr)
return false, depErr
end
local ok, status, msg = runRemote(pkg .. "/install.lua", ...)
if not ok then
print("Installer crashed:", status)
return
end
local version = "unknown"
local desc = "no description"
version = manifest.version or version
desc = manifest.desc or desc
if status == "success" then
local db = loadDB()
db[pkg] = { version = version, desc = desc }
saveDB(db)
elseif status == "cancelled" then
print(msg or "Installation cancelled.")
return
elseif status == "failed" then
print(msg or "Installation failed.")
return
elseif status == "reboot" then
print("Installation completed. Rebooting...")
local db = loadDB()
db[pkg] = { version = version, desc = desc }
saveDB(db)
os.reboot()
else
print("Unknown installer status:", tostring(status))
return
end
return true
end
local function remove(pkg, force)
local db = loadDB()
if db[pkg] and not force then
print("Removing package:", pkg)
elseif not force then
print("Package not installed:", pkg)
return
end
local ok, err = runRemote(pkg .. "/remove.lua")
if not ok then
print("Remove failed:", err)
return
end
db[pkg] = nil
saveDB(db)
print("Removed:", pkg)
end
local function upgrade(pkg, ...)
local path = pkg .. "/upgrade.lua"
local code = fetch(REPO .. "/" .. path)
if code then
print("Running upgrade script for:", pkg)
local ok, err = runPackage(code, path, ...)
if not ok then
print("Upgrade failed:", err)
end
return
end
print("No upgrade script, reinstalling...")
remove(pkg, true)
install(pkg, ...)
end
local function list()
local db = loadDB()
print("Installed packages:")
for k, v in pairs(db) do
if type(v) == "table" then
print("-", k, v.version or "unknown", "-", v.desc or "")
else
print("-", k, v or "unknown")
end
end
end
local function query(pkg)
local code = fetch(REPO .. "/" .. pkg .. "/manifest.lua")
if not code then
return print("Package not found:", pkg)
end
local fn = load(code, "@manifest.lua", "t", {})
if not fn then
return print("Invalid manifest file")
end
local ok, v = pcall(fn)
if ok then
if type(v) == "table" then
print(pkg, v.version or "unknown", "-", v.desc or "")
else
print(pkg .. " version:", v)
end
else
print("Error reading version")
end
end
local function syncAll()
local db = loadDB()
for pkg in pairs(db) do
upgrade(pkg)
end
end
local function isFlag(arg)
return type(arg) == "string" and arg:sub(1, 2) == "--"
end
-- =========================
-- CLI
-- =========================
local args = {...}
local cmd = args[1]
if cmd == "-S" or cmd == "-D" then
if cmd == "-D" then download = true else download = false end
local baseFlags = {}
for i = 2, #args do
if isFlag(args[i]) then
baseFlags[#baseFlags + 1] = args[i]
end
end
for i = 2, #args do
if not isFlag(args[i]) then
if args[i] == "base" then
install(args[i], nil, table.unpack(baseFlags))
else
install(args[i])
end
end
end
elseif cmd == "-R" then
for i = 2, #args do
remove(args[i], false)
end
elseif cmd == "-Rf" then
for i = 2, #args do
remove(args[i], true)
end
elseif cmd == "-U" then
for i = 2, #args do
upgrade(args[i])
end
elseif cmd == "-Syu" then
syncAll()
elseif cmd == "-Q" then
for i = 2, #args do
query(args[i])
end
elseif cmd == "-L" then
list()
else
print("pacman usage:")
print("-S <pkg> [args] install")
print("-R <pkg> remove")
print("-Rf <pkg> force remove")
print("-U <pkg> upgrade")
print("-Syu upgrade all")
print("-Q <pkg> query version")
print("-L list installed")
end