-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.lua
628 lines (548 loc) · 21.1 KB
/
install.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
local basalt
local releasePath = "https://raw.githubusercontent.com/Pyroxenium/Basalt2/refs/heads/main/release/basalt.lua"
local devPath = "https://raw.githubusercontent.com/Pyroxenium/Basalt2/refs/heads/main/src/"
local configPath = "https://raw.githubusercontent.com/Pyroxenium/Basalt2/refs/heads/main/config.lua"
local luaLSPath = "https://raw.githubusercontent.com/Pyroxenium/Basalt2/refs/heads/main/src/LuaLS.lua"
local basaltRequest = http.get(releasePath)
if not basaltRequest then
error("Failed to download Basalt")
end
basalt = load(basaltRequest.readAll(), "basalt", "bt", _ENV)()
local coloring = {foreground=colors.black, background=colors.white}
local currentScreen = 1
local screens = {}
local main = basalt.getMainFrame():setBackground(colors.black)
local config
local skipConfig = true
local function getConfig()
if not config then
local request = http.get(configPath)
if request then
local content = request.readAll()
config = load(content)()
request.close()
else
error("Failed to fetch config")
end
end
return config
end
local function getChildrenHeight(container)
local height = 0
for _, child in ipairs(container:getChildren()) do
if(child.get("visible"))then
local newHeight = child.get("y") + child.get("height")
if newHeight > height then
height = newHeight
end
end
end
return height
end
local function getScreenPosition(index)
if index <= 2 then
return (main:getWidth() * (index - 1)) + 2
end
if index == 5 then
if skipConfig then
return (main:getWidth() * 2) + 2
end
return (main:getWidth() * 4) + 2
end
if index == 3 or index == 4 then
if skipConfig then
return main:getWidth() * 10
else
return (main:getWidth() * (index - 1)) + 2
end
end
end
local function createScreen(index)
local screen = main:addFrame(coloring)
:onScroll(function(self, direction)
local height = getChildrenHeight(self)
local scrollOffset = self:getOffsetY()
local maxScroll = height - self:getHeight()
scrollOffset = math.max(0, math.min(maxScroll, scrollOffset + direction))
self:setOffsetY(scrollOffset)
end)
:setSize("{parent.width - 2}", "{parent.height - 4}")
screen:setPosition(function()
return getScreenPosition(index)
end, 2)
screens[index] = screen
return screen
end
local backButton
local nextButton
local function switchScreen(direction)
local newScreen = currentScreen + direction
if screens[newScreen] then
if(skipConfig)then
if(newScreen==4)then
return
end
end
main:animate():moveOffset((newScreen - 1) * main:getWidth(), 0, 0.5):start()
currentScreen = newScreen
end
basalt.schedule(function()
sleep(0.1)
backButton:setVisible(true)
nextButton:setVisible(true)
if(newScreen==1)then
backButton:setVisible(false)
nextButton:setVisible(true)
end
if(newScreen==5)then
nextButton:setVisible(false)
backButton:setVisible(true)
end
if(skipConfig)then
if(newScreen==3)then
nextButton:setVisible(false)
backButton:setVisible(true)
end
end
end)
end
nextButton = main:addButton()
:setBackground("{self.clicked and colors.black or colors.white}")
:setForeground("{self.clicked and colors.white or colors.black}")
:setSize(8, 1)
:setText("Next")
:setPosition("{parent.width - 9}", "{parent.height - 1}")
:setIgnoreOffset(true)
:onClick(function() switchScreen(1) end)
backButton = main:addButton()
:setBackground("{self.clicked and colors.black or colors.white}")
:setForeground("{self.clicked and colors.white or colors.black}")
:setSize(8, 1)
:setText("Back")
:setPosition(2, "{parent.height - 1}")
:setIgnoreOffset(true)
:onClick(function() switchScreen(-1) end)
:setVisible(false)
-- Screen 1: Welcome
local welcomeScreen = createScreen(1)
welcomeScreen:addLabel(coloring)
:setText("Welcome to Basalt!")
:setPosition(2, 2)
welcomeScreen:addLabel(coloring)
:setWidth("{parent.width - 2}")
:setAutoSize(false)
:setText([[Basalt is an open-source project created with passion for the ComputerCraft community. It provides you with all the tools you need to create beautiful and interactive user interfaces.
The project is actively maintained and continuously improving thanks to our amazing community. Whether you're a beginner or an experienced developer, you'll find Basalt easy to use yet powerful enough for complex applications.
Have ideas or want to get involved? Join our friendly community on Discord or GitHub - we'd love to hear from you and welcome contributions of any kind!
Let's start creating something awesome together!]])
:setPosition(2, 4)
-- Screen 2: Installation
local installScreen = createScreen(2)
installScreen:addLabel(coloring)
:setText("Choose Your Installation")
:setPosition(2, 2)
installScreen:addLabel(coloring)
:setText("Select Version:")
:setPosition(2, 4)
local versionDropdown = installScreen:addDropdown()
:setPosition("{parent.width - self.width - 1}", 4)
:setSize(15, 1)
:setBackground(colors.black)
:setForeground(colors.white)
:addItem("Release")
:addItem("Dev")
:addItem("Custom")
local versionDesc = installScreen:addLabel("versionDesc")
:setWidth("{parent.width - 2}")
:setAutoSize(false)
:setText("The Release version is the most stable and tested version of Basalt. It is recommended for production use.")
:setPosition(2, 7)
:setSize("{parent.width - 4}", 3)
:setBackground(colors.lightGray)
installScreen:addLabel(coloring)
:setText("Path:")
:setPosition(2, "{versionDesc.y + versionDesc.height + 1}")
local additionalComponents = installScreen:addLabel(coloring)
:setText("Additional Components:")
:setPosition(2, "{versionDesc.y + versionDesc.height + 3}")
:setVisible(false)
local luaLSCheckbox = installScreen:addCheckbox(coloring)
:setText("[ ] LLS definitions")
:setCheckedText("[x] LLS definitions")
:setPosition(2, "{versionDesc.y + versionDesc.height + 4}")
:setVisible(false)
local luaMinifyCheckbox = installScreen:addCheckbox(coloring)
:setText("[ ] Minify Project")
:setCheckedText("[x] Minify Project")
:setPosition(2, "{versionDesc.y + versionDesc.height + 5}")
:setVisible(false)
local singleFileProject = installScreen:addCheckbox(coloring)
:setText("[ ] Single File Project")
:setCheckedText("[x] Single File Project")
:setPosition(2, "{versionDesc.y + versionDesc.height + 6}")
:setVisible(false)
local installPathInput = installScreen:addInput()
:setPosition(8, "{versionDesc.y + versionDesc.height + 1}")
:setPlaceholder("basalt")
:setSize(12, 1)
:setBackground(colors.black)
:setForeground(colors.white)
versionDropdown:onSelect(function(self, index, item)
if(item.text == "Release") then
versionDesc:setText("The Release version is the most stable and tested version of Basalt. It is recommended for production use.")
additionalComponents:setVisible(false)
luaLSCheckbox:setVisible(false)
luaMinifyCheckbox:setVisible(false)
singleFileProject:setVisible(false)
elseif(item.text == "Custom") then
versionDesc:setText("The Custom version allows you to specify which elements or plugins you want to install.")
additionalComponents:setVisible(true)
luaLSCheckbox:setVisible(true)
luaMinifyCheckbox:setVisible(true)
singleFileProject:setVisible(true)
else
versionDesc:setText("The Dev version is the latest development version of Basalt. It may contain new features and improvements, but it may also have bugs and issues.")
additionalComponents:setVisible(false)
luaLSCheckbox:setVisible(false)
luaMinifyCheckbox:setVisible(false)
singleFileProject:setVisible(false)
end
-- skipConfig setzen basierend auf Version
skipConfig = (item.text ~= "Custom")
-- Screens neu positionieren
for i, screen in pairs(screens) do
screen:setPosition(getScreenPosition(i), 2)
end
end)
-- Screen 3: Elements
local elementsScreen = createScreen(3)
elementsScreen:addLabel(coloring)
:setText("Elements: (white = selected)")
:setPosition(2, 2)
local elementsList = elementsScreen:addList("elementsList")
:setMultiSelection(true)
:setSelectedBackground(colors.lightGray)
:setForeground(colors.gray)
:setPosition(2, 4)
:setSize("{parent.width - 30}", 8)
local elementDesc = elementsScreen:addLabel("elementDesc")
:setAutoSize(false)
:setWidth("{parent.width - (elementsList.x + elementsList.width) - 2}")
:setText("Select an element to see its description.")
:setPosition("{elementsList.x + elementsList.width + 1}", 4)
:setSize(28, 8)
:setBackground(colors.lightGray)
local eleScreenDesc = elementsScreen:addLabel()
:setAutoSize(false)
:setWidth("{parent.width - 2}")
:setText("This screen allows you to select which elements you want to install. You can select multiple elements.")
:setPosition(2, "{math.max(elementsList.y + elementsList.height, elementDesc.y + elementDesc.height) + 1}")
:setBackground(colors.lightGray)
local function addElements()
elementsList:clear()
for k,v in pairs(getConfig().categories.elements.files)do
elementsList:addItem({selected=v.default, text=k, item=v, callback=function()
if(v.description)and(v.description~="")then
elementDesc:setText(v.description)
else
elementDesc:setText("No description available.")
end
end})
end
end
addElements()
-- Screen 4 Plugins
local pluginScreen = createScreen(4)
pluginScreen:addLabel(coloring)
:setText("Plugins: (white = selected)")
:setPosition(2, 2)
local pluginList = pluginScreen:addList("pluginList")
:setMultiSelection(true)
:setSelectedBackground(colors.lightGray)
:setForeground(colors.gray)
:setPosition(2, 4)
:setSize("{parent.width - 30}", 8)
local pluginDesc = pluginScreen:addLabel("pluginDesc")
:setAutoSize(false)
:setWidth("{parent.width - (pluginList.x + pluginList.width) - 2}")
:setText("Select a plugin to see its description.")
:setPosition("{pluginList.x + pluginList.width + 1}", 4)
:setSize(28, 8)
:setBackground(colors.lightGray)
local pluScreenDesc = pluginScreen:addLabel()
:setAutoSize(false)
:setWidth("{parent.width - 2}")
:setText("This screen allows you to select which plugins you want to install. You can select multiple plugins.")
:setPosition(2, "{math.max(pluginList.y + pluginList.height, pluginDesc.y + pluginDesc.height) + 1}")
:setBackground(colors.lightGray)
local function addPlugins()
pluginList:clear()
for k,v in pairs(getConfig().categories.plugins.files)do
pluginList:addItem({selected = v.default, text=k, item=v, callback=function()
if(v.description)and(v.description~="")then
elementDesc:setText(v.description)
else
elementDesc:setText("No description available.")
end
end})
end
end
addPlugins()
-- Screen 5 Installation Progress
local progressScreen = createScreen(5)
local installButton
local currentlyInstalling = false
local progressBar = progressScreen:addProgressBar()
:setPosition(2, "{parent.height - 2}")
:setSize("{parent.width - 12}", 2)
local log = progressScreen:addList("log")
:setPosition(2, 2)
:setSize("{parent.width - 2}", "{parent.height - 6}")
:addItem("Starting installation...")
local function logMessage(log, message)
log:addItem(message)
log:scrollToBottom()
end
local function updateProgress(progressBar, current, total)
progressBar:setProgress(math.ceil((current / total) * 100))
end
local function installRelease(installPath, log, progressBar)
logMessage(log, "Installing Release version...")
local request = http.get(releasePath)
if not request then
logMessage(log, "Failed to download release version, aborting installation.")
return
end
local file = fs.open(installPath, "w")
file.write(request.readAll())
file.close()
request.close()
progressBar:setProgress(100)
logMessage(log, "Release installation complete!")
end
local function installDev(installPath, log, progressBar)
logMessage(log, "Installing Dev version...")
local config = getConfig()
if not config then
logMessage(log, "Failed to fetch config")
return
end
local function downloadFile(url, path, name, size)
logMessage(log, "Downloading " .. name..(size > 0 and " (" .. size/1000 .. " kb)" or ""))
local request = http.get(url)
if request then
local file = fs.open(path, "w")
file.write(request.readAll())
file.close()
request.close()
else
error("Failed to download " .. name)
end
end
local totalFiles = 0
for _, category in pairs(config.categories) do
totalFiles = totalFiles + #category.files
end
local currentFile = 0
for categoryName, category in pairs(config.categories) do
for fileName, fileInfo in pairs(category.files) do
downloadFile(devPath .. fileInfo.path, fs.combine(installPath, fileInfo.path), fileName, fileInfo.size or 0)
currentFile = currentFile + 1
updateProgress(progressBar, currentFile, totalFiles)
end
end
logMessage(log, "Dev installation complete!")
end
local function tableGet(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
local function installCustom(installPath, log, progressBar, selectedElements, selectedPlugins, includeLuaLS, minify, singleFile)
logMessage(log, "Installing Custom version...")
local config = getConfig()
if not config then
error("Failed to fetch config")
end
local min
local project = {}
local function downloadFile(url, name, size)
logMessage(log, "Downloading " .. name..(size > 0 and " (" .. size/1000 .. " kb)" or ""))
local request = http.get(url)
if request then
local content = request.readAll()
request.close()
return content
else
error("Failed to download " .. name)
end
end
if(minify)then
local request = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt2/refs/heads/main/tools/minify.lua")
if request then
min = load(request.readAll())()
request.close()
else
logMessage(log, "Failed to download minify.lua")
return
end
end
local totalFiles = #selectedElements + #selectedPlugins
for _, category in pairs({"core", "libraries"}) do
totalFiles = totalFiles + tableGet(config.categories[category].files)
end
local currentFile = 0
for fileName, fileInfo in pairs(config.categories.core.files) do
if fileName ~= "LuaLS" or includeLuaLS then
project[fileInfo.path] = downloadFile(devPath .. fileInfo.path, fileName, fileInfo.size or 0)
currentFile = currentFile + 1
updateProgress(progressBar, currentFile, totalFiles)
end
end
for fileName, fileInfo in pairs(config.categories.libraries.files) do
project[fileInfo.path] = downloadFile(devPath .. fileInfo.path, fileName, fileInfo.size or 0)
currentFile = currentFile + 1
updateProgress(progressBar, currentFile, totalFiles)
end
for _, element in ipairs(selectedElements) do
local fileInfo = config.categories.elements.files[element.text]
basalt.LOGGER.debug(element.text)
project[fileInfo.path] = downloadFile(devPath .. fileInfo.path, element.text, fileInfo.size or 0)
currentFile = currentFile + 1
updateProgress(progressBar, currentFile, totalFiles)
end
for _, plugin in ipairs(selectedPlugins) do
local fileInfo = config.categories.plugins.files[plugin.text]
project[fileInfo.path] = downloadFile(devPath .. fileInfo.path, plugin.text, fileInfo.size or 0)
currentFile = currentFile + 1
updateProgress(progressBar, currentFile, totalFiles)
end
if minify then
logMessage(log, "Minifying project...")
for path, content in pairs(project) do
local success, minifiedContent = min(content)
if(success)then
project[path] = minifiedContent
else
logMessage(log, "Failed to minify " .. path)
return
end
end
end
if(singleFile)then
installPath = installPath:gsub(".lua", "")..".lua"
local output = {
'local minified = true\n',
'local minified_elementDirectory = {}\n',
'local minified_pluginDirectory = {}\n',
'local project = {}\n',
'local loadedProject = {}\n',
'local baseRequire = require\n',
'require = function(path) if(project[path..".lua"])then if(loadedProject[path]==nil)then loadedProject[path] = project[path..".lua"]() end return loadedProject[path] end baseRequire(path) end\n'
}
for filePath, content in pairs(project) do
local elementName = filePath:match("^elements/(.+)%.lua$")
if elementName then
table.insert(output, string.format(
'minified_elementDirectory["%s"] = {}\n',
elementName
))
end
local pluginName = filePath:match("^plugins/(.+)%.lua$")
if pluginName then
table.insert(output, string.format(
'minified_pluginDirectory["%s"] = {}\n',
pluginName
))
end
end
for filePath, content in pairs(project) do
table.insert(output, string.format(
'project["%s"] = function(...) %s end\n',
filePath, content
))
end
table.insert(output, 'return project["main.lua"]()')
local out = fs.open(installPath, "w")
if(out)then
out.write(table.concat(output))
out.close()
if(includeLuaLS)then
local luaLS = downloadFile(luaLSPath, "LuaLS", 0)
local luaLSDir = fs.getDir(installPath)
local file = fs.open(fs.combine(luaLSDir, "LuaLS.lua"), "w")
file.write(luaLS)
file.close()
end
else
logMessage(log, "Failed to write to " .. installPath)
return
end
else
for filePath, content in pairs(project) do
local out = fs.open(fs.combine(installPath, filePath), "w")
if(out)then
out.write(content)
out.close()
else
logMessage(log, "Failed to write to " .. fs.combine(installPath, filePath))
return
end
end
if(includeLuaLS)then
local luaLS = downloadFile(luaLSPath, "LuaLS", 0)
local luaLSDir = fs.combine(installPath, "LuaLS.lua")
local file = fs.open(luaLSDir, "w")
file.write(luaLS)
file.close()
end
end
logMessage(log, "Custom installation complete!")
end
local function installBasalt()
currentlyInstalling = true
installButton:setVisible(false)
local selection = versionDropdown:getSelectedItems()[1]
if(selection==nil)then
selection = "Release"
else
selection = selection.text
end
local path = installPathInput:getText()
if(path=="")then
path = "basalt"
else
path = path:gsub(".lua", "")
end
if(selection == "Release")then
installRelease(path..".lua", log, progressBar)
elseif(selection == "Dev")then
installDev(path, log, progressBar)
else
installCustom(path, log, progressBar, elementsList:getSelectedItems(), pluginList:getSelectedItems(), luaLSCheckbox:getChecked(), luaMinifyCheckbox:getChecked(), singleFileProject:getChecked())
end
currentlyInstalling = false
installButton:setVisible(true)
end
installButton = progressScreen:addButton()
:setBackground("{self.clicked and colors.lightGray or colors.black}")
:setForeground("{self.clicked and colors.black or colors.lightGray}")
:setText("Install")
:setPosition("{parent.width - 9}", "{parent.height - 3}")
:setSize(9, 1)
:onClick(function(self)
if(currentlyInstalling)then
return
end
basalt.schedule(installBasalt)
end)
local closeButton = progressScreen:addButton()
:setBackground("{self.clicked and colors.lightGray or colors.black}")
:setForeground("{self.clicked and colors.black or colors.lightGray}")
:setText("Close")
:setPosition("{parent.width - 9}", "{parent.height - 1}")
:setSize(9, 1)
:onClick(function(self)
basalt.stop()
end)
basalt.run()