computercraft/lib/lum.lua

66 lines
1.8 KiB
Lua
Raw Normal View History

2023-10-09 12:54:35 +02:00
-- Desc: Lum Package Manager
-- Auth: Layla Manley
-- Link: https://gitea.layla.gg/layla/computercraft
2023-10-08 10:04:21 +02:00
local target_branch = "main"
local bin_path = "/bin/"
local lib_path = "/lib/"
local startup_path = "/startup/"
2023-10-08 10:10:24 +02:00
settings.define("lum.target_branch", {
description = "Target branch for updates",
type = "string",
default = "main"
})
settings.define("lum.log_level", {
description = "Log Level",
type = "string",
default = "debug"
})
2023-10-08 10:04:21 +02:00
function download_lua(remote_path, local_path)
2023-10-08 10:11:02 +02:00
local url = "https://gitea.layla.gg/layla/computercraft/raw/branch/" .. settings.get("lum.target_branch") .. "/" .. remote_path
2023-10-08 10:10:24 +02:00
if settings.get("lum.log_level") == "debug" then
io.write("Downloading " .. url .. " to " .. local_path .. "\n")
end
local response = http.get(url, nil)
2023-10-08 10:04:21 +02:00
if response.getResponseCode() == 200 then
file = fs.open(local_path, "w")
file.write(response.readAll())
file.close()
else
2023-10-08 10:07:33 +02:00
io.write("Failed to download " .. remote_path .. ".lua\n")
2023-10-08 10:04:21 +02:00
io.write(response)
end
end
function install(package)
io.write("Installing " .. package .. "\n")
2023-10-09 13:14:14 +02:00
local dir = bin_path
2023-10-09 13:22:18 +02:00
local package_dir = "packages/"
2023-10-09 13:14:14 +02:00
--if package starts with lib then remove lib
if string.sub(package, 1, 3) == "lib" then
2023-10-09 13:15:05 +02:00
package = string.sub(package, 4)
2023-10-09 13:14:14 +02:00
dir = lib_path
2023-10-09 13:22:18 +02:00
package_dir = "lib/"
2023-10-09 13:14:14 +02:00
end
2023-10-09 13:22:18 +02:00
download_lua(package_dir .. package .. ".lua", dir .. package .. ".lua")
2023-10-09 13:14:14 +02:00
if dir == lib_path then
os.loadAPI(lib_path .. package .. ".lua")
end
2023-10-08 10:14:52 +02:00
end
function remove(package)
io.write("Removing " .. package .. "\n")
2023-10-09 13:14:14 +02:00
local dir = bin_path
--if package starts with lib then remove lib
if string.sub(package, 1, 3) == "lib" then
package = string.sub(package, 5)
dir = lib_path
end
fs.delete(dir .. package .. ".lua")
2023-10-08 10:04:21 +02:00
end