DigsoftFixes/installer.lua
2024-09-17 03:53:12 -04:00

84 lines
2.3 KiB
Lua

-- Installer for the DigsoftFixes to a ComputerCraft Turtle
-- TODO:
-- Per file hashes..?
REPOSITORY = "https://git.l7y.media/api/v1/repos/Lucas7yoshi/DigsoftFixes"
local lastInstalledCommit = nil
if fs.exists("lastinstalledcommit.txt") then
local file = fs.open("lastinstalledcommit.txt", "r")
lastInstalledCommit = file.readLine()
file.close()
end
-- Check if we need to update or not
local needsUpdate = false
local latestCommit = nil
if lastInstalledCommit then
print("Checking git.l7y.media...")
local repoCommitsRaw = http.get(REPOSITORY.."/commits?stat=true&verification=false&files=false&limit=1")
local repoCommits = textutils.unserialiseJSON(repoCommitsRaw.readAll())
if not repoCommits then
printError("Failed to fetch repository commits")
return
end
latestCommit = repoCommits[1].sha
if lastInstalledCommit ~= repoCommits[1].sha then
needsUpdate = true
print("Update available")
-- trim the first 7 characters of the commit hash
local trimmedBefore = lastInstalledCommit:sub(1, 7)
local trimmedAfter = repoCommits[1].sha:sub(1, 7)
print(trimmedBefore .. " -> " .. trimmedAfter)
end
else
needsUpdate = true
end
if not needsUpdate then
print("Up to date")
print(lastInstalledCommit:sub(1, 7) .. " = " .. latestCommit:sub(1, 7))
return
end
local repoContentRaw = http.get(REPOSITORY.."/contents")
if not repoContentRaw then
printError("Failed to fetch repository contents")
return
end
local repoContent = textutils.unserialiseJSON(repoContentRaw.readAll())
print("Removing existing files...")
for i = 1, #repoContent do
shell.run("rm", repoContent[i].name)
end
print("Removed existing files")
print("Downloading new files...")
for i = 1, #repoContent do
local data = repoContent[i]
print("Downloading " .. data.name .. "...")
local file = http.get(data.download_url)
if not file then
printError("Failed to download " .. data.name)
else
local fileContent = file.readAll()
file.close()
local file = fs.open(data.name, "w")
file.write(fileContent)
file.close()
end
end
print("Downloaded " .. #repoContent .. " files!")
-- update lastinstalledcommit.txt
local file = fs.open("lastinstalledcommit.txt", "w")
file.write(latestCommit)
file.close()
print("Update complete!")