lua-serve/projects/updater/install.lua

141 lines
4.0 KiB
Lua
Raw Normal View History

2021-06-06 04:03:09 +00:00
local args = {...}
2021-06-06 00:46:57 +00:00
2021-06-06 04:03:09 +00:00
local url = "http://lua.gaywine.org"
2021-06-06 02:22:49 +00:00
2021-06-06 04:03:09 +00:00
local path = "bin/"
2021-06-06 00:46:57 +00:00
if not fs.exists(path) then
fs.makeDir(path)
end
2021-06-06 04:03:09 +00:00
local temp = "temp/"
2021-06-06 00:46:57 +00:00
if not fs.exists(temp) then
fs.makeDir(temp)
end
if #args > 0 then
programToInstall = args[1]
2021-06-06 04:03:09 +00:00
else
print("usage:")
print("install.lua <project>")
print("install.lua <project> <custom install path>")
2021-06-06 00:46:57 +00:00
end
if #args > 1 then
2021-06-06 04:03:09 +00:00
print("WARNING: custom paths are currently not supported.")
2021-06-06 00:46:57 +00:00
customPath = shell.resolve(args[2])
end
2021-06-06 04:03:09 +00:00
local function downloadFile(filename)
print("downloading " .. filename .. "...")
local fileUrl = url .. "/files/" .. programToInstall .. "/" .. filename
2021-06-06 03:32:27 +00:00
2021-06-06 04:03:09 +00:00
local result = http.get(fileUrl)
local resultText = result.readAll()
2021-06-06 03:32:27 +00:00
2021-06-06 04:03:09 +00:00
local file = fs.open(temp .. filename, "w")
file.write(resultText)
file.close()
2021-06-06 03:32:27 +00:00
end
2021-06-06 04:03:09 +00:00
local function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table, cap)
end
last_end = e + 1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
2021-06-06 03:32:27 +00:00
end
2021-06-06 04:03:09 +00:00
local function getAllFiles()
local indexesUrl = url .. "/indexes/" .. programToInstall
local result = http.get(indexesUrl)
2021-06-06 04:08:17 +00:00
if result == nil then
2021-06-06 04:33:47 +00:00
print("error! getting indexes for " .. programToInstall)
error()
2021-06-06 04:08:17 +00:00
end
2021-06-06 04:03:09 +00:00
local resultText = result.readAll()
local resultTable = split(resultText, "\n")
local indexesFile = fs.open("bin/indexes.dat", "w")
indexesFile.write(resultText)
indexesFile.close()
2021-06-06 03:32:27 +00:00
2021-06-06 04:03:09 +00:00
local programsFile = fs.open(path .. "programs.dat", "a")
programsFile.write(programToInstall)
programsFile.close()
2021-06-06 03:32:27 +00:00
2021-06-06 04:03:09 +00:00
local finalResult = {}
for key, value in ipairs(resultTable) do
finalResult[key] = split(value, " ")[1]
end
2021-06-06 03:32:27 +00:00
2021-06-06 04:03:09 +00:00
return finalResult
2021-06-06 00:46:57 +00:00
end
2021-06-06 04:03:09 +00:00
local function finishInstall(files)
2021-06-06 00:46:57 +00:00
for key, file in ipairs(files) do
if customPath ~= nil then
2021-06-06 04:03:09 +00:00
print("moving " .. file .. " to " .. customPath .. "...")
2021-06-06 04:33:47 +00:00
if not fs.exists(customPath) then
fs.makeDir(customPath)
end
2021-06-06 04:31:19 +00:00
if fs.exists(customPath .. "/" .. file) then
2021-06-06 04:33:47 +00:00
fs.delete(customPath .. "/" .. file)
2021-06-06 04:31:19 +00:00
end
2021-06-06 00:46:57 +00:00
fs.move(temp .. file, customPath .. "/" .. file)
else
2021-06-06 04:03:09 +00:00
print("moving " .. file .. " to " .. path .. programToInstall .. "...")
2021-06-06 04:33:47 +00:00
if not fs.exists(path .. programToInstall) then
fs.makeDir(path .. programToInstall)
end
2021-06-06 04:31:19 +00:00
if fs.exists(path .. programToInstall .. "/" .. file) then
2021-06-06 04:33:47 +00:00
fs.delete(path .. programToInstall .. "/" .. file)
2021-06-06 04:31:19 +00:00
end
2021-06-06 04:03:09 +00:00
fs.move(temp .. file, path .. programToInstall .. "/" .. file)
2021-06-06 00:46:57 +00:00
end
end
if customPath ~= nil then
2021-06-06 04:03:09 +00:00
local program = "shell.run(" .. customPath .. "/run.lua)"
2021-06-06 00:46:57 +00:00
file = fs.open(path .. programToInstall .. "/run.lua")
file.write(program)
file.close()
end
fs.delete(temp)
end
2021-06-06 03:32:27 +00:00
2021-06-06 04:03:09 +00:00
if not fs.exists(path .. "programs.dat") then
2021-06-06 04:33:47 +00:00
local file = fs.open(path .. "programs.dat", "w")
file.write("")
file.close()
2021-06-06 04:03:09 +00:00
end
local programsFile = fs.open(path .. "programs.dat", "r")
local programsText = programsFile.readAll()
local programsTable = split(programsText, "\n")
local shouldInstall = true
for _, value in ipairs(programsTable) do
if value == programToInstall then
print(programToInstall .. " is already installed. checking for updates instead...")
shouldInstall = false
end
end
if shouldInstall then
local files = getAllFiles()
for _, value in ipairs(files) do
downloadFile(value)
end
finishInstall(files)
elseif fs.exists(path .. "updater/update.lua") then
shell.run(path .. "updater/update.lua " .. programToInstall)
2021-06-06 03:32:27 +00:00
end