-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.lua
More file actions
464 lines (398 loc) · 14.2 KB
/
Copy pathinstall.lua
File metadata and controls
464 lines (398 loc) · 14.2 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
-- Obsidian web installer.
--
-- Interactive:
-- install
-- Direct:
-- install <source|bundled|minified|compressed> [target]
-- install --url <repository-base-url> <variant> [target]
-- install --help
--
-- Run without arguments it fetches Basalt and presents a graphical installer.
-- If that download fails it falls back to a plain terminal prompt, so the
-- installer still works on a computer that cannot reach the Basalt repository.
local args = { ... }
local REPO_URL =
"https://raw.githubusercontent.com/Pyroxenium/Obsidian/refs/heads/master/"
-- Basalt renders the graphical installer. The minified bundle is the smallest
-- build that still starts instantly.
local BASALT_URL = "https://raw.githubusercontent.com/Pyroxenium/Basalt2/"
.. "refs/heads/basalt2.5/bundle/basalt.min.lua"
-- Obsidian's brand green, matching the documentation site.
local ACCENT = "#5fd7a4"
local BUNDLE_PATHS = {
bundled = "bundle/obsidian.lua",
minified = "bundle/obsidian.min.lua",
compressed = "bundle/obsidian.compressed.lua",
}
local VARIANTS = { "source", "bundled", "minified", "compressed" }
local VARIANT_LOOKUP = {
source = true,
bundled = true,
minified = true,
compressed = true,
}
local DESCRIPTIONS = {
source = "Editable folder with the original source files.",
bundled = "Readable single file, comments stripped.",
minified = "Smaller single file, immediate startup.",
compressed = "Smallest download, takes a moment to decompress.",
}
local DEFAULT_VARIANT = "minified"
-- The engine is loaded as require("obsidian"), which resolves both a folder
-- containing init.lua and a plain obsidian.lua next to the program.
local DEFAULT_TARGETS = {
source = "obsidian",
bundled = "obsidian.lua",
minified = "obsidian.lua",
compressed = "obsidian.lua",
}
-- ---------------------------------------------------------------------------
-- Fetching and writing
-- ---------------------------------------------------------------------------
local function withTrailingSlash(url)
return url:sub(-1) == "/" and url or (url .. "/")
end
local function bundleUrl(variant, baseUrl)
return withTrailingSlash(baseUrl or REPO_URL) .. BUNDLE_PATHS[variant]
end
local function ensureParent(path)
local parent = fs.getDir(path)
if parent ~= "" and not fs.exists(parent) then
fs.makeDir(parent)
end
end
local function removeIfPresent(path)
if fs.exists(path) then fs.delete(path) end
end
local function requireHttp()
if not http then
error("installer: the HTTP API is disabled. Enable it in the "
.. "CC:Tweaked configuration.", 0)
end
end
local function fetch(url)
requireHttp()
local response, requestError = http.get(url)
if not response then
error("installer: download failed: " .. url
.. " (" .. tostring(requestError) .. ")", 0)
end
if response.getResponseCode then
local code, message = response.getResponseCode()
if code < 200 or code >= 300 then
response.close()
error(("installer: HTTP %d %s: %s")
:format(code, tostring(message or ""), url), 0)
end
end
local content = response.readAll()
response.close()
return content
end
local function writeFile(path, content)
ensureParent(path)
local handle = fs.open(path, "w")
if not handle then
error("installer: cannot write " .. path, 0)
end
handle.write(content)
handle.close()
end
--- Writes through a .part file so a failed download never leaves a truncated
--- engine behind.
local function writeAtomic(target, content)
if fs.exists(target) then
error("installer: target already exists: " .. target, 0)
end
local temporary = target .. ".part"
removeIfPresent(temporary)
local ok, writeError = pcall(writeFile, temporary, content)
if not ok then
removeIfPresent(temporary)
error(writeError, 0)
end
ensureParent(target)
fs.move(temporary, target)
end
-- ---------------------------------------------------------------------------
-- Install variants
-- ---------------------------------------------------------------------------
--- manifest.txt lists every source file as a repository-relative path. It is
--- generated by CI from src/, so it cannot drift out of sync with the tree.
local function parseManifest(content)
local files = {}
for path in (content .. "\n"):gmatch("(.-)\n") do
path = path:gsub("\r", ""):gsub("%s+$", "")
local relative = path:match("^src/(.+)$")
if relative then files[#files + 1] = relative end
end
if #files == 0 then
error("installer: the source manifest is empty", 0)
end
return files
end
local function installSource(baseUrl, target, progress)
if fs.exists(target) then
error("installer: target already exists: " .. target, 0)
end
baseUrl = withTrailingSlash(baseUrl or REPO_URL)
local temporary = target .. ".part"
removeIfPresent(temporary)
ensureParent(target)
fs.makeDir(temporary)
local ok, result = pcall(function()
progress(0, 1, "Downloading source manifest...")
local files = parseManifest(fetch(baseUrl .. "manifest.txt"))
for index, relative in ipairs(files) do
progress(index, #files, relative)
writeFile(fs.combine(temporary, relative),
fetch(baseUrl .. "src/" .. relative))
end
return #files
end)
if not ok then
removeIfPresent(temporary)
error(result, 0)
end
local moved, moveError = pcall(fs.move, temporary, target)
if not moved then
removeIfPresent(temporary)
error(moveError, 0)
end
return ("Installed %d source files -> %s/"):format(result, target)
end
local function installBundle(variant, baseUrl, target, progress)
progress(0, 1, "Downloading " .. variant .. " bundle...")
local content = fetch(bundleUrl(variant, baseUrl))
writeAtomic(target, content)
progress(1, 1, "Done")
return ("Downloaded %s bundle -> %s (%d KB)")
:format(variant, target, math.floor(#content / 1024 + 0.5))
end
local function installVariant(variant, target, progress, baseUrl)
progress = progress or function() end
if variant == "source" then
return installSource(baseUrl, target, progress)
end
return installBundle(variant, baseUrl, target, progress)
end
-- ---------------------------------------------------------------------------
-- Command line
-- ---------------------------------------------------------------------------
local function printHelp()
print("Obsidian installer")
print("")
print("Usage:")
print(" install")
print(" install <variant> [target]")
print(" install --url <base-url> <variant> [target]")
print("")
print("Variants:")
for _, variant in ipairs(VARIANTS) do
print((" %-11s %s"):format(variant, DESCRIPTIONS[variant]))
end
end
local function parseArguments()
local variant, target, baseUrl
local index = 1
while index <= #args do
local argument = args[index]
if argument == "-h" or argument == "--help" then
return nil, nil, nil, true
elseif argument == "--url" then
index = index + 1
baseUrl = args[index]
if not baseUrl or baseUrl == "" then
error("installer: --url requires a base URL", 0)
end
elseif VARIANT_LOOKUP[argument] then
if variant then
error("installer: multiple variants specified", 0)
end
variant = argument
elseif argument:sub(1, 1) == "-" then
error("installer: unknown option " .. argument, 0)
elseif not target then
target = argument
else
error("installer: unexpected argument " .. argument, 0)
end
index = index + 1
end
if target and not variant then
error("installer: specify a variant before the target", 0)
end
return variant, target, baseUrl, false
end
-- ---------------------------------------------------------------------------
-- Interactive prompt
-- ---------------------------------------------------------------------------
local function prompt()
print("Obsidian installer")
print("")
for index, variant in ipairs(VARIANTS) do
local marker = variant == DEFAULT_VARIANT and " (recommended)" or ""
print((" %d) %-11s %s%s")
:format(index, variant, DESCRIPTIONS[variant], marker))
end
print("")
local variant
while not variant do
write("Variant [1-" .. #VARIANTS .. ", blank = "
.. DEFAULT_VARIANT .. "]: ")
local answer = read()
if answer == nil then
error("installer: cancelled", 0)
elseif answer == "" then
variant = DEFAULT_VARIANT
elseif VARIANT_LOOKUP[answer] then
variant = answer
else
local index = tonumber(answer)
variant = index and VARIANTS[index] or nil
if not variant then print(" Not a valid choice.") end
end
end
local default = DEFAULT_TARGETS[variant]
write(("Target [blank = %s]: "):format(default))
local target = read()
if target == nil or target == "" then target = default end
print("")
return variant, target
end
-- ---------------------------------------------------------------------------
-- Graphical installer, rendered with Basalt
-- ---------------------------------------------------------------------------
local function loadBasalt()
print("Fetching the installer interface...")
local source = fetch(BASALT_URL)
local chunk, loadError = load(source, "@basalt.lua", "t", _ENV)
if not chunk then
error("installer: cannot load Basalt: " .. tostring(loadError), 0)
end
return chunk()
end
local function runGui(basalt, baseUrl)
basalt.use("bigfont")
local palette = basalt.use("theme").applyPreset("basalt")
local background = palette.bg
local accent = basalt.rgb(ACCENT)
local main = basalt.getMainFrame()
local width, height = main:getSize()
local contentWidth = math.max(20, width - 4)
main:addBigFont({
x = 2, y = 2, text = "Obsidian",
foreground = accent, background = background,
})
local items, descriptions = {}, {}
for index, name in ipairs(VARIANTS) do
local label = name:sub(1, 1):upper() .. name:sub(2)
if name == DEFAULT_VARIANT then label = label .. " (recommended)" end
items[index] = label
descriptions[index] = DESCRIPTIONS[name]
end
local defaultIndex = 1
for index, name in ipairs(VARIANTS) do
if name == DEFAULT_VARIANT then defaultIndex = index end
end
local variantList = main:addList({
x = 2, y = 6, width = contentWidth, height = #VARIANTS,
items = items,
background = background,
})
local description = main:addLabel({
x = 2, y = 6 + #VARIANTS + 1, width = contentWidth, height = 2,
text = descriptions[defaultIndex],
foreground = palette.muted,
})
main:addLabel({ x = 2, y = 13, text = "Target:" })
local targetInput = main:addInput({
x = 10, y = 13, width = math.max(12, width - 12),
placeholder = DEFAULT_TARGETS[DEFAULT_VARIANT],
background = background,
})
variantList:onSelect(function(_, index)
description.text = descriptions[index]
targetInput.placeholder = DEFAULT_TARGETS[VARIANTS[index]]
end)
variantList:select(defaultIndex, false)
local status = main:addLabel({
x = 2, y = math.max(16, height - 3),
width = contentWidth, height = 2,
text = "Choose a variant and press Install.",
foreground = palette.muted,
})
local progressBar = main:addProgressBar({
x = 2, y = height - 1, width = contentWidth,
barColor = accent,
})
local installing = false
local installButton = main:addButton({
x = 2, y = 15, width = 11, height = 1,
text = "Install", background = accent,
foreground = background,
})
installButton:onClick(function()
if installing then return end
installing = true
installButton.text = "Working..."
status.foreground = palette.text
progressBar.progress = 0
local selectedIndex = variantList.selected or defaultIndex
local selected = VARIANTS[selectedIndex]
local chosenTarget = #targetInput.text > 0
and targetInput.text or DEFAULT_TARGETS[selected]
local ok, result = pcall(installVariant, selected, chosenTarget,
function(done, total, label)
progressBar.progress =
math.floor(done / math.max(1, total) * 100)
status.text = tostring(label or "")
basalt.update()
end, baseUrl)
if ok then
status.text = result
status.foreground = palette.success
progressBar.progress = 100
else
status.text = tostring(result)
status.foreground = palette.danger
end
installButton.text = "Install"
installing = false
end)
main:addButton({
x = 15, y = 15, width = 8, height = 1, text = "Exit",
}):onClick(function()
basalt.stop()
end)
basalt.run()
end
-- ---------------------------------------------------------------------------
-- Run
-- ---------------------------------------------------------------------------
local variant, target, baseUrl, help = parseArguments()
if help then
return printHelp()
end
if not variant then
-- Only a failure to obtain Basalt falls back to text; once the interface
-- is up it owns the rest of the session.
local ok, basalt = pcall(loadBasalt)
if ok then
return runGui(basalt, baseUrl)
end
print("installer: graphical installer unavailable.")
print(" " .. tostring(basalt))
print("")
variant, target = prompt()
end
target = target or DEFAULT_TARGETS[variant]
local summary = installVariant(variant, target, function(done, total, label)
if label then
print(("[%d/%d] %s"):format(done, total, label))
end
end, baseUrl)
print(summary)
print("")
print('Use it with: local Engine = require("'
.. (target:gsub("%.lua$", "")) .. '")')