lua-serve/projects/updater/install.lua

151 lines
4.2 KiB
Lua
Raw Permalink 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
2021-06-06 04:42:30 +00:00
local programToInstall = nil
2021-06-06 00:46:57 +00:00
if #args > 0 then
2021-06-06 04:42:30 +00:00
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
2021-06-06 04:42:30 +00:00
local customPath = nil
2021-06-06 00:46:57 +00:00
if #args > 1 then
2021-06-06 04:03:09 +00:00
print("WARNING: custom paths are currently not supported.")
2021-06-06 04:42:30 +00:00
customPath = shell.resolve(args[2])
2021-06-06 00:46:57 +00:00
end
2021-06-06 04:39:59 +00:00
local function downloadFile(program, filename)
2021-06-06 04:03:09 +00:00
print("downloading " .. filename .. "...")
2021-06-06 04:39:59 +00:00
local fileUrl = url .. "/files/" .. program .. "/" .. 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:39:59 +00:00
local function getAllFiles(program)
local indexesUrl = url .. "/indexes/" .. program
2021-06-06 04:03:09 +00:00
local result = http.get(indexesUrl)
2021-06-06 04:08:17 +00:00
if result == nil then
2021-06-06 04:39:59 +00:00
print("error! getting indexes for " .. program)
2021-06-06 04:33:47 +00:00
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")
2021-06-06 04:59:12 +00:00
local indexesFile = fs.open(path .. program .. "/indexes.dat", "w")
2021-06-06 04:03:09 +00:00
indexesFile.write(resultText)
indexesFile.close()
2021-06-06 04:59:12 +00:00
print("wrote indexes for " .. program)
2021-06-06 03:32:27 +00:00
2021-06-06 04:03:09 +00:00
local programsFile = fs.open(path .. "programs.dat", "a")
2021-06-06 04:39:59 +00:00
programsFile.write(program)
2021-06-06 04:03:09 +00:00
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:39:59 +00:00
local function finishInstall(program, files)
2021-06-06 04:56:56 +00:00
if files == nil then
print("files var is nil")
error()
end
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:39:59 +00:00
print("moving " .. file .. " to " .. path .. program .. "...")
if not fs.exists(path .. program) then
fs.makeDir(path .. program)
2021-06-06 04:33:47 +00:00
end
2021-06-06 04:39:59 +00:00
if fs.exists(path .. program .. "/" .. file) then
fs.delete(path .. program .. "/" .. file)
2021-06-06 04:31:19 +00:00
end
2021-06-06 04:39:59 +00:00
fs.move(temp .. file, path .. program .. "/" .. 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 04:39:59 +00:00
file = fs.open(path .. program .. "/run.lua")
2021-06-06 00:46:57 +00:00
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
2021-06-06 04:39:59 +00:00
local files = getAllFiles(programToInstall)
2021-06-06 04:03:09 +00:00
for _, value in ipairs(files) do
2021-06-06 04:53:11 +00:00
if value ~= nil then
downloadFile(programToInstall, value)
end
2021-06-06 04:03:09 +00:00
end
2021-06-06 04:39:59 +00:00
finishInstall(programToInstall, files)
2021-06-06 04:03:09 +00:00
elseif fs.exists(path .. "updater/update.lua") then
shell.run(path .. "updater/update.lua " .. programToInstall)
2021-06-06 03:32:27 +00:00
end