first pass updater

This commit is contained in:
jane 2021-06-06 00:03:09 -04:00
parent bdb3b39d4c
commit c262e983d4
2 changed files with 213 additions and 46 deletions

View file

@ -1,35 +1,41 @@
args = {...} local args = {...}
url = "http://lua.gaywine.org" local url = "http://lua.gaywine.org"
path = "bin/" local path = "bin/"
if not fs.exists(path) then if not fs.exists(path) then
fs.makeDir(path) fs.makeDir(path)
end end
temp = "temp/" local temp = "temp/"
if not fs.exists(temp) then if not fs.exists(temp) then
fs.makeDir(temp) fs.makeDir(temp)
end end
if #args > 0 then if #args > 0 then
programToInstall = args[1] programToInstall = args[1]
else
print("usage:")
print("install.lua <project>")
print("install.lua <project> <custom install path>")
end end
if #args > 1 then if #args > 1 then
print("WARNING: custom paths are currently not supported.")
customPath = shell.resolve(args[2]) customPath = shell.resolve(args[2])
end end
function downloadFile(filename) local function downloadFile(filename)
fileUrl = url .. "/files/" .. programToInstall .. "/" .. filename print("downloading " .. filename .. "...")
local fileUrl = url .. "/files/" .. programToInstall .. "/" .. filename
result = http.get(fileUrl) local result = http.get(fileUrl)
resultText = result.readAll() local resultText = result.readAll()
file = fs.open(temp .. filename, "w") local file = fs.open(temp .. filename, "w")
file.write(resultText) file.write(resultText)
file.close() file.close()
end end
function split(pString, pPattern) local function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0 local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern local fpat = "(.-)" .. pPattern
local last_end = 1 local last_end = 1
@ -48,14 +54,22 @@ function split(pString, pPattern)
return Table return Table
end end
function getAllFiles() local function getAllFiles()
indexesUrl = url .. "/indexes/" .. programToInstall local indexesUrl = url .. "/indexes/" .. programToInstall
result = http.get(indexesUrl) local result = http.get(indexesUrl)
resultText = result.readAll() local resultText = result.readAll()
resultTable = split(resultText, "\n") local resultTable = split(resultText, "\n")
finalResult = {} local indexesFile = fs.open("bin/indexes.dat", "w")
indexesFile.write(resultText)
indexesFile.close()
local programsFile = fs.open(path .. "programs.dat", "a")
programsFile.write(programToInstall)
programsFile.close()
local finalResult = {}
for key, value in ipairs(resultTable) do for key, value in ipairs(resultTable) do
finalResult[key] = split(value, " ")[1] finalResult[key] = split(value, " ")[1]
end end
@ -63,17 +77,19 @@ function getAllFiles()
return finalResult return finalResult
end end
function finishInstall(files) local function finishInstall(files)
for key, file in ipairs(files) do for key, file in ipairs(files) do
if customPath ~= nil then if customPath ~= nil then
print("moving " .. file .. " to " .. customPath .. "...")
fs.move(temp .. file, customPath .. "/" .. file) fs.move(temp .. file, customPath .. "/" .. file)
else else
fs.move(temp .. binary, path .. binary) print("moving " .. file .. " to " .. path .. programToInstall .. "...")
fs.move(temp .. file, path .. programToInstall .. "/" .. file)
end end
end end
if customPath ~= nil then if customPath ~= nil then
program = "shell.run(" .. customPath .. "/run.lua)" local program = "shell.run(" .. customPath .. "/run.lua)"
file = fs.open(path .. programToInstall .. "/run.lua") file = fs.open(path .. programToInstall .. "/run.lua")
file.write(program) file.write(program)
file.close() file.close()
@ -82,8 +98,27 @@ function finishInstall(files)
fs.delete(temp) fs.delete(temp)
end end
if not fs.exists(path .. "programs.dat") then
local file = fs.open(path .. "programs.dat", "w")
file.write("")
file.close()
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() local files = getAllFiles()
for _, value in ipairs(files) do for _, value in ipairs(files) do
downloadFile(value) downloadFile(value)
end end
finishInstall(files) finishInstall(files)
elseif fs.exists(path .. "updater/update.lua") then
shell.run(path .. "updater/update.lua " .. programToInstall)
end

View file

@ -1 +1,133 @@
print('fuck') args = {...}
url = "http://lua.gaywine.org"
path = "bin/"
if not fs.exists(path) then
fs.makeDir(path)
end
temp = "temp/"
if not fs.exists(temp) then
fs.makeDir(temp)
end
if #args > 0 then
programToInstall = args[1]
end
if #args > 1 then
print("WARNING: custom paths are currently not supported.")
customPath = shell.resolve(args[2])
end
local function downloadFile(filename)
print("downloading " .. filename .. "...")
local fileUrl = url .. "/files/" .. programToInstall .. "/" .. filename
local result = http.get(fileUrl)
local resultText = result.readAll()
local file = fs.open(temp .. filename, "w")
file.write(resultText)
file.close()
end
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
end
local function getAllFiles()
local indexesUrl = url .. "/indexes/" .. programToInstall
local updateUrl = url .. "/updates/" .. programToInstall
local currentFiles = ""
if fs.exists(path .. "indexes.dat") then
local indexesFile = fs.open(path .. "indexes.dat", "r")
currentFiles = indexesFile.readAll()
indexesFile.close()
end
local result = http.post(indexesUrl, currentFiles)
local resultText = result.readAll()
local resultTable = split(resultText, "\n")
local result2 = http.get(indexesUrl)
local resultText2 = result2.readAll()
local resultTable2 = split(resultText2, "\n")
local newIndexesFile = fs.open(path .. "indexes.dat", "w")
newIndexesFile.write(resultText)
newIndexesFile.close()
local finalResult = {}
for key, value in ipairs(resultTable2) do
local filename = split(value, " ")[1]
for key2, value2 in ipairs(resultTable) do
local valueTable = split(value2, " ")
local filePath = path .. programToInstall .. "/" .. valueTable[1]
if valueTable[2] == "delete" and fs.exists(filePath) then
fs.delete(filePath)
elseif valueTable[2] == "download" then
finalResult[#finalResult + 1] = valueTable[1]
end
end
end
return finalResult
end
local function finishInstall(files)
for key, file in ipairs(files) do
if customPath ~= nil then
print("moving " .. file .. " to " .. customPath .. "...")
fs.move(temp .. file, customPath .. "/" .. file)
else
print("moving " .. file .. " to " .. path .. programToInstall .. "...")
fs.move(temp .. file, path .. programToInstall .. "/" .. file)
end
end
if customPath ~= nil then
local program = "shell.run(" .. customPath .. "/run.lua)"
file = fs.open(path .. programToInstall .. "/run.lua")
file.write(program)
file.close()
end
fs.delete(temp)
end
if programToInstall == nil then
print("no program specified, updating all...")
local programsFile = fs.open(path .. "programs.dat", "r")
local programsText = programsFile.readAll()
local programsTable = split(programsText, "\n")
for _, value in ipairs(programsTable) do
programToInstall = value
local files = getAllFiles()
for _, value in ipairs(files) do
downloadFile(value)
end
finishInstall(files)
end
else
local files = getAllFiles()
for _, value in ipairs(files) do
downloadFile(value)
end
finishInstall(files)
end