Update 'ccptinstall.lua'

This commit is contained in:
andreas 2022-06-19 03:24:46 +02:00
parent f2536835f2
commit f76ae39bec

View File

@ -1,8 +1,8 @@
--[[ --[[
ComputerCraft Package Tool Installer ComputerCraft Package Tool Installer
Author: PentagonLP Author: PentagonLP, Hion-V
Version: 1.0 Version: 1.1
Lines of Code: 161; Characters: 5541 Lines of Code: 161; Characters: 5541
]] ]]
-- Read arguments -- Read arguments
@ -10,151 +10,151 @@ args = {...}
-- FILE MANIPULATION FUNCTIONS -- -- FILE MANIPULATION FUNCTIONS --
--[[ Checks if file exists --[[ Checks if file exists
@param String filepath: Filepath to check @param String filepath: Filepath to check
@return boolean: Does the file exist? @return boolean: Does the file exist?
--]] --]]
function file_exists(filepath) function file_exists(filepath)
local f=io.open(filepath,"r") local f=io.open(filepath,"r")
if f~=nil then if f~=nil then
io.close(f) io.close(f)
return true return true
else else
return false return false
end end
end end
--[[ Stores a file in a desired location --[[ Stores a file in a desired location
@param String filepath: Filepath where to create file (if file already exists, it gets overwritten) @param String filepath: Filepath where to create file (if file already exists, it gets overwritten)
@param String content: Content to store in file @param String content: Content to store in file
--]] --]]
function storeFile(filepath,content) function storeFile(filepath,content)
writefile = fs.open(filepath,"w") writefile = fs.open(filepath,"w")
writefile.write(content) writefile.write(content)
writefile.close() writefile.close()
end end
--[[ Reads a file from a desired location --[[ Reads a file from a desired location
@param String filepath: Filepath to the file to read @param String filepath: Filepath to the file to read
@param String createnew: (Optional) Content to store in new file and return if file does not exist. Can be nil. @param String createnew: (Optional) Content to store in new file and return if file does not exist. Can be nil.
@return String|boolean content|error: Content of the file; If createnew is nil and file doesn't exist boolean false is returned @return String|boolean content|error: Content of the file; If createnew is nil and file doesn't exist boolean false is returned
--]] --]]
function readFile(filepath,createnew) function readFile(filepath,createnew)
readfile = fs.open(filepath,"r") readfile = fs.open(filepath,"r")
if readfile == nil then if readfile == nil then
if not (createnew==nil) then if not (createnew==nil) then
storeFile(filepath,createnew) storeFile(filepath,createnew)
return createnew return createnew
else else
return false return false
end end
end end
content = readfile.readAll() content = readfile.readAll()
readfile.close() readfile.close()
return content return content
end end
--[[ Stores a table in a file --[[ Stores a table in a file
@param String filepath: Filepath where to create file (if file already exists, it gets overwritten) @param String filepath: Filepath where to create file (if file already exists, it gets overwritten)
@param Table data: Table to store in file @param Table data: Table to store in file
--]] --]]
function storeData(filepath,data) function storeData(filepath,data)
storeFile(filepath,textutils.serialize(data):gsub("\n","")) storeFile(filepath,textutils.serialize(data):gsub("\n",""))
end end
--[[ Reads a table from a file in a desired location --[[ Reads a table from a file in a desired location
@param String filepath: Filepath to the file to read @param String filepath: Filepath to the file to read
@param boolean createnew: If true, an empty table is stored in new file and returned if file does not exist. @param boolean createnew: If true, an empty table is stored in new file and returned if file does not exist.
@return Table|boolean content|error: Table thats stored in the file; If createnew is false and file doesn't exist boolean false is returned @return Table|boolean content|error: Table thats stored in the file; If createnew is false and file doesn't exist boolean false is returned
--]] --]]
function readData(filepath,createnew) function readData(filepath,createnew)
if createnew then if createnew then
return textutils.unserialize(readFile(filepath,textutils.serialize({}):gsub("\n",""))) return textutils.unserialize(readFile(filepath,textutils.serialize({}):gsub("\n","")))
else else
return textutils.unserialize(readFile(filepath,nil)) return textutils.unserialize(readFile(filepath,nil))
end end
end end
-- HTTP FETCH FUNCTIONS -- -- HTTP FETCH FUNCTIONS --
--[[ Gets result of HTTP URL --[[ Gets result of HTTP URL
@param String url: The desired URL @param String url: The desired URL
@return Table|boolean result|error: The result of the request; If the URL is not reachable, an error is printed in the terminal and boolean false is returned @return Table|boolean result|error: The result of the request; If the URL is not reachable, an error is printed in the terminal and boolean false is returned
--]] --]]
function gethttpresult(url) function gethttpresult(url)
if not http.checkURL(url) then if not http.checkURL(url) then
print("ERROR: Url '" .. url .. "' is blocked in config. Unable to fetch data.") print("ERROR: Url '" .. url .. "' is blocked in config. Unable to fetch data.")
return false return false
end end
result = http.get(url) result = http.get(url)
if result == nil then if result == nil then
print("ERROR: Unable to reach '" .. url .. "'") print("ERROR: Unable to reach '" .. url .. "'")
return false return false
end end
return result return result
end end
--[[ Download file HTTP URL --[[ Download file HTTP URL
@param String filepath: Filepath where to create file (if file already exists, it gets overwritten) @param String filepath: Filepath where to create file (if file already exists, it gets overwritten)
@param String url: The desired URL @param String url: The desired URL
@return nil|boolean nil|error: nil; If the URL is not reachable, an error is printed in the terminal and boolean false is returned @return nil|boolean nil|error: nil; If the URL is not reachable, an error is printed in the terminal and boolean false is returned
--]] --]]
function downloadfile(filepath,url) function downloadfile(filepath,url)
result = gethttpresult(url) result = gethttpresult(url)
if result == false then if result == false then
return false return false
end end
storeFile(filepath,result.readAll()) storeFile(filepath,result.readAll())
end end
-- MISC HELPER FUNCTIONS -- -- MISC HELPER FUNCTIONS --
--[[ Checks wether a String starts with another one --[[ Checks wether a String starts with another one
@param String haystack: String to check wether is starts with another one @param String haystack: String to check wether is starts with another one
@param String needle: String to check wether another one starts with it @param String needle: String to check wether another one starts with it
@return boolean result: Wether the firest String starts with the second one @return boolean result: Wether the firest String starts with the second one
]]-- ]]--
function startsWith(haystack,needle) function startsWith(haystack,needle)
return string.sub(haystack,1,string.len(needle))==needle return string.sub(haystack,1,string.len(needle))==needle
end end
-- MAIN PROGRAMM -- -- MAIN PROGRAMM --
if (args[1]=="install") or (args[1]==nil) then if (args[1]=="install") or (args[1]==nil) then
print("[Installer] Well, hello there!") print("[Installer] Well, hello there!")
print("[Installer] Thank you for downloading the ComputerCraft Package Tool! Installing...") print("[Installer] Thank you for downloading the ComputerCraft Package Tool! Installing...")
print("[Installer] Installing 'properprint' library...") print("[Installer] Installing 'properprint' library...")
if downloadfile("lib/properprint","https://raw.githubusercontent.com/PentagonLP/properprint/main/properprint")== false then if downloadfile("lib/properprint","https://raw.githubusercontent.com/Hion-V/properprint/main/properprint")== false then
return false return false
end end
print("[Installer] Successfully installed 'properprint'!") print("[Installer] Successfully installed 'properprint'!")
print("[Installer] Installing 'ccpt'...") print("[Installer] Installing 'ccpt'...")
if downloadfile("ccpt","https://raw.githubusercontent.com/PentagonLP/ccpt/main/ccpt")==false then if downloadfile("ccpt","https://git.subsonics.nl/pootercraft/ccpt/raw/branch/main/ccpt")==false then
return false return false
end end
print("[Installer] Successfully installed 'ccpt'!") print("[Installer] Successfully installed 'ccpt'!")
print("[Installer] Running 'ccpt update'...") print("[Installer] Running 'ccpt update'...")
shell.run("ccpt","update") shell.run("ccpt","update")
print("[Installer] Reading package data...") print("[Installer] Reading package data...")
packagedata = readData("/.ccpt/packagedata") packagedata = readData("/.ccpt/packagedata")
print("[Installer] Storing installed packages...") print("[Installer] Storing installed packages...")
storeData("/.ccpt/installedpackages",{ storeData("/.ccpt/installedpackages",{
ccpt = packagedata["ccpt"]["newestversion"], ccpt = packagedata["ccpt"]["newestversion"],
pprint = packagedata["pprint"]["newestversion"] pprint = packagedata["pprint"]["newestversion"]
}) })
print("[Installer] 'ccpt' successfully installed!") print("[Installer] 'ccpt' successfully installed!")
elseif args[1]=="update" then elseif args[1]=="update" then
print("[Installer] Updating 'ccpt'...") print("[Installer] Updating 'ccpt'...")
if downloadfile("ccpt","https://raw.githubusercontent.com/PentagonLP/ccpt/main/ccpt")==false then if downloadfile("ccpt","https://git.subsonics.nl/pootercraft/ccpt/raw/branch/main/ccpt")==false then
return false return false
end end
elseif args[1]=="remove" then elseif args[1]=="remove" then
print("[Installer] Uninstalling 'ccpt'...") print("[Installer] Uninstalling 'ccpt'...")
fs.delete("/ccpt") fs.delete("/ccpt")
fs.delete("/.ccpt") fs.delete("/.ccpt")
shell.setCompletionFunction("ccpt", nil) shell.setCompletionFunction("ccpt", nil)
if file_exists("startup") and startsWith(startup,"-- ccpt: Seach for updates\nshell.run(\"ccpt\",\"startup\")") then if file_exists("startup") and startsWith(startup,"-- ccpt: Seach for updates\nshell.run(\"ccpt\",\"startup\")") then
print("[Installer] Removing 'ccpt' from startup...") print("[Installer] Removing 'ccpt' from startup...")
startup = readFile("startup","") startup = readFile("startup","")
storeFile("startup",string.sub(startup,56)) storeFile("startup",string.sub(startup,56))
end end
print("[Installer] So long, and thanks for all the fish!") print("[Installer] So long, and thanks for all the fish!")
else else
print("[Installer] Invalid argument: " .. args[1]) print("[Installer] Invalid argument: " .. args[1])
end end