From d0fc8e65a1b133662ab68d133cf8fcb44619c516 Mon Sep 17 00:00:00 2001 From: ChristophLHR <11349102+ChristophLHR@users.noreply.github.com> Date: Mon, 4 Dec 2023 21:42:16 +0100 Subject: [PATCH] HotFixes Co-authored-by: Josh --- libs/scm/net.lua | 57 +++----- libs/scm/scriptManager.lua | 281 ++++++++++++++++++------------------- libs/scm/ui.lua | 4 +- 3 files changed, 162 insertions(+), 180 deletions(-) diff --git a/libs/scm/net.lua b/libs/scm/net.lua index cd75326..19bc869 100644 --- a/libs/scm/net.lua +++ b/libs/scm/net.lua @@ -6,7 +6,7 @@ do local config = function() return SCM.Config:getAll() end - local log= function(...) SCM.Log:log(...) end + local log = function(...) SCM.Log:log(...) end ---@param target string ---@param fileType string @@ -18,22 +18,22 @@ do --@TODO: Error handling return false end - local sourceObject = { name = nil, source = { - ["default"] = target + ["default"] = target, }, + sourceName = "default", type = fileType } - if updateObj then sourceObject.name = updateObj.name end -- Check for Pastebin local name, code = SCM.ScriptManager:splitNameCode(target) if name and code then sourceObject.name = name - return SCM.ScriptManager:addScript(self:downloadPastebin(sourceObject, code, config()[fileType .. "Directory"], + return SCM.ScriptManager:addScript(self:downloadPastebin(sourceObject, code, + config()[fileType .. "Directory"], updateObj)) end @@ -47,6 +47,7 @@ do end local repository = target .. suffix sourceObject.name = target + return SCM.ScriptManager:addScript(self:downloadGit(sourceObject, repository, config()[fileType .. "Directory"], updateObj)) end @@ -118,29 +119,15 @@ do file.close() local filePaths = {} - if fs then - file = fs.open(targetDirectory .. sourceObject.name - .. config()[sourceObject.type .. "Suffix"] - .. "/" .. config()["infoFile"], "r") - - local line = file.read() - while line do - filePaths[#filePaths + 1] = line - line = file.read() - end - file.close() - else - file = fs.open(targetDirectory .. sourceObject.name - .. config()[sourceObject.type .. "Suffix"] - .. "/" .. config()["infoFile"], "r") - -- print(file.read()) - local line = file.read() - while line do - filePaths[#filePaths + 1] = line - line = file.read() - end - file.close() + file = fs.open(targetDirectory .. sourceObject.name + .. config()[sourceObject.type .. "Suffix"] + .. "/" .. config()["infoFile"], "r") + local line = file.readLine() + while line do + filePaths[#filePaths + 1] = line + line = file.readLine() end + file.close() for i = 1, #filePaths, 1 do local success = true local tmpRequest = http.get(baseUrl .. filePaths[i]) @@ -175,7 +162,7 @@ do -- create a link that calls the file within the program directory if sourceObject.type == "program" then - local progamLink = fs.open(sourceObject.name, "w") + local progamLink = fs.open(sourceObject.name .. ".lua", "w") progamLink.write("shell.execute(\"" .. targetDirectory .. sourceObject.name .. config()[sourceObject.type .. "Suffix"] .. "/" .. sourceObject.name .. ".lua" .. "\", ...)") @@ -219,7 +206,6 @@ do if not sourceObject.name then sourceObject.name = self:getNameFromURL(sourceObject.source[sourceName]) end - local request = http.get(sourceObject.source[sourceName]) if request then @@ -234,6 +220,7 @@ do end file.write(content) file.close() + return sourceObject, true end end @@ -344,20 +331,20 @@ do if not file then os.execute("mkdir " .. config()["configDirectory"]) file = fs.open(config()["configDirectory"] .. config()["repoScriptsFile"], "w") - end + end if file then - file.write(""..textutils.serializeJSON(repoScripts)) + file.write("" .. textutils.serializeJSON(repoScripts)) file.close() end end - function Net:getNewestVersion () + function Net:getNewestVersion() local githubAPIgetTags = config()["apiGithubGetTags"] githubAPIgetTags = githubAPIgetTags:gsub("", config()["user"]) githubAPIgetTags = githubAPIgetTags:gsub("", config()["repository"]) - + local request = http.get(githubAPIgetTags) - + if request then local content = request.readAll() request.close() @@ -370,4 +357,4 @@ do end end -return Net \ No newline at end of file +return Net diff --git a/libs/scm/scriptManager.lua b/libs/scm/scriptManager.lua index 0d9593c..e9a9c41 100644 --- a/libs/scm/scriptManager.lua +++ b/libs/scm/scriptManager.lua @@ -5,9 +5,10 @@ ---@field source table ---@class SCMScriptManager -local ScriptManager = { +local ScriptManager = { ---@type SCMScript[] - scripts = {} } + scripts = {} +} SCM.ScriptManager = ScriptManager do local config = function() @@ -36,7 +37,7 @@ do os.execute("mkdir " .. config["configDirectory"]) file = fs.open(config["configDirectory"] .. config["scriptFile"], "w") end - file.write(""..textutils.serializeJSON(self.scripts)) + file.write("" .. textutils.serializeJSON(self.scripts)) file.close() end @@ -60,7 +61,6 @@ do end end end - if not scriptExists then log("Script added: " .. script.name) table.insert(self.scripts, script) @@ -122,40 +122,37 @@ do local o = {} local scriptType = nil - if keepScriptConfig ~= true then - for i = 1, #self.scripts, 1 do - if self.scripts[i].name ~= name then - table.insert(o, self.scripts[i]) - else - scriptType = self.scripts[i].type - end + for i = 1, #self.scripts, 1 do + if self.scripts[i].name ~= name and keepScriptConfig ~= true then + table.insert(o, self.scripts[i]) + else + scriptType = self.scripts[i].type end - + end + if keepScriptConfig ~= true then self.scripts = o self:saveScripts() end - -- delete file local scriptDir = config()[scriptType .. "Directory"] if scriptType and ( - fs.exists(scriptDir .. name .. ".lua") or - fs.exists(scriptDir .. name .. config()[scriptType .. "Suffix"]) - ) then + fs.exists(scriptDir .. name .. ".lua") or + fs.exists(scriptDir .. name .. config()[scriptType .. "Suffix"]) + ) then fs.delete(scriptDir .. name .. config()[scriptType .. "Suffix"]) if scriptType == "library" then fs.delete(scriptDir .. name .. ".lua") end end - if scriptType == "program" then - fs.delete(name) + fs.delete(name .. ".lua") end -- update autocomplete SCM.Autocomplete:prepareAutocomplete() SCM.Autocomplete:updateAutocomplete() end - + --- removes all scripts function ScriptManager:removeAllScripts() local tmpScripts = {} @@ -168,157 +165,155 @@ do end end ----@param name string ----@param localPath string | nil | unknown -function ScriptManager:checkRequirements(name, localPath) - log("Checking requirements of " .. (localPath or name) .. "...") - local file - if localPath then - file = fs.open(localPath, "r") - if not file then - file = fs.open('./' .. localPath .. ".lua", "r") - end - elseif fs.open("./" .. config()["libraryDirectory"] .. name .. config()["librarySuffix"] .. "/" .. name .. ".lua", "r") then - file = fs.open("./" .. config()["libraryDirectory"] - .. name .. config()["librarySuffix"] - .. "/" .. name .. ".lua", "r") - else - file = fs.open("./" .. config()["libraryDirectory"] .. name .. ".lua", "r") - end - if not file then log('File ' .. name .. ' not found') end - -- Find requirements by searching for comment --@requires name - local requires = {} - while true do - local line = file.read() - if not line then break end - - local find = string.find(line, "--@requires") - if find then - line = string.sub(line, find + 12) - local lineEnd = string.find(line, " ") - - local scriptName = nil - if lineEnd then - scriptName = string.sub(line, 0, lineEnd - 1) - else - scriptName = string.sub(line, 0) + ---@param name string + ---@param localPath string | nil | unknown + function ScriptManager:checkRequirements(name, localPath) + log("Checking requirements of " .. (localPath or name) .. "...") + local file + if localPath then + file = fs.open(localPath, "r") + if not file then + file = fs.open('./' .. localPath .. ".lua", "r") end + elseif fs.open("./" .. config()["libraryDirectory"] .. name .. config()["librarySuffix"] .. "/" .. name .. ".lua", "r") then + file = fs.open("./" .. config()["libraryDirectory"] + .. name .. config()["librarySuffix"] + .. "/" .. name .. ".lua", "r") + else + file = fs.open("./" .. config()["libraryDirectory"] .. name .. ".lua", "r") + end + if not file then log('File ' .. name .. ' not found') end + -- Find requirements by searching for comment --@requires name + local requires = {} + while true do + local line = file.read() + if not line then break end + + local find = string.find(line, "--@requires") + if find then + line = string.sub(line, find + 12) + local lineEnd = string.find(line, " ") + + local scriptName = nil + if lineEnd then + scriptName = string.sub(line, 0, lineEnd - 1) + else + scriptName = string.sub(line, 0) + end - requires[#requires + 1] = scriptName + requires[#requires + 1] = scriptName + end end - end - file.close() + file.close() - -- Install missing requirements - for i = 1, #requires do - local n = requires[i] --[[@as string]] - local tmpName, tmpCode = self:splitNameCode(n) - if tmpCode then n = tmpName--[[@as string]] end + -- Install missing requirements + for i = 1, #requires do + local n = requires[i] --[[@as string]] + local tmpName, tmpCode = self:splitNameCode(n) + if tmpCode then n = tmpName --[[@as string]] end - log("Trying to install " .. n .. "...") + log("Trying to install " .. n .. "...") - local scriptExists = false - for j = 1, #self.scripts, 1 do - if self.scripts[j].name == n then - scriptExists = true + local scriptExists = false + for j = 1, #self.scripts, 1 do + if self.scripts[j].name == n then + scriptExists = true + end end - end - if not scriptExists then - if tmpCode then - SCM.Net:download(tmpName .. "@" .. tmpCode, "library") + if not scriptExists then + if tmpCode then + SCM.Net:download(tmpName .. "@" .. tmpCode, "library") + else + SCM.Net:download(n, "library") + end else - SCM.Net:download(n, "library") + log(n .. " already exists.") end - else - log(n .. " already exists.") - end - self:checkRequirements(n --[[@as string]]) + self:checkRequirements(n --[[@as string]]) + end end -end ---- used when no script with the name was found online ---- searches locally for the script ----@param name string ----@return any | nil -local function fallbackRequire(name) - - log(name .. " not found online, try to find locally") - --- if script does not exist - local possiblePath = { - name, - config()["libraryDirectory"] .. name, - config()["libraryDirectory"] .. name .. "/" .. name, - config()["libraryDirectory"] .. name .. "/" .. "init.lua" - } - local script - local success - ---TryFunction for Require - ---@param path string - ---@return any - local function tryRequire(path) - return require(path) - end + --- used when no script with the name was found online + --- searches locally for the script + ---@param name string + ---@return any | nil + local function fallbackRequire(name) + log(name .. " not found online, try to find locally") + --- if script does not exist + local possiblePath = { + name, + config()["libraryDirectory"] .. name, + config()["libraryDirectory"] .. name .. "/" .. name, + config()["libraryDirectory"] .. name .. "/" .. "init.lua" + } + local script + local success + ---TryFunction for Require + ---@param path string + ---@return any + local function tryRequire(path) + return require(path) + end - for _, path in pairs(possiblePath) do - success, script = pcall(tryRequire, path) - if success then - ScriptManager:checkRequirements(name, path) - return script + for _, path in pairs(possiblePath) do + success, script = pcall(tryRequire, path) + if success then + ScriptManager:checkRequirements(name, path) + return script + end end + log("Could not load " .. name) + return nil end - log("Could not load " .. name) - return nil -end ---@param name string ----@return any -function ScriptManager:load(name) - log("Loading " .. name .. "...") - local scriptExists = false - for i = 1, #self.scripts, 1 do - if self.scripts[i].name == name then - scriptExists = true + ---@return any + function ScriptManager:load(name) + log("Loading " .. name .. "...") + local scriptExists = false + for i = 1, #self.scripts, 1 do + if self.scripts[i].name == name then + scriptExists = true + end + end + if not scriptExists then + SCM.Net:download(name, "library") end - end - if not scriptExists then - SCM.Net:download(name, "library") - end - scriptExists = false - for i = 1, #self.scripts, 1 do - if self.scripts[i].name == name then - scriptExists = true + scriptExists = false + for i = 1, #self.scripts, 1 do + if self.scripts[i].name == name then + scriptExists = true + end end - end - if scriptExists then - self:checkRequirements(name) - local path = "./" .. config()["libraryDirectory"] .. name - local script = require(path) - log("Done") - return script + if scriptExists then + self:checkRequirements(name) + local path = "./" .. config()["libraryDirectory"] .. name + local script = require(path) + log("Done") + return script + end + + return fallbackRequire(name) end - return fallbackRequire(name) -end + ---@param str string + ---@return string | nil + ---@return string | nil + function ScriptManager:splitNameCode(str) + local separator = string.find(str, "@") ----@param str string ----@return string | nil ----@return string | nil -function ScriptManager:splitNameCode(str) - local separator = string.find(str, "@") + if separator then + local name = string.sub(str, 1, separator - 1) + local code = string.sub(str, separator + 1) + return name, code + end - if separator then - local name = string.sub(str, 1, separator - 1) - local code = string.sub(str, separator + 1) - return name, code + return nil, nil end - - return nil, nil -end - end return ScriptManager diff --git a/libs/scm/ui.lua b/libs/scm/ui.lua index 3ea3fd4..a5cd3f9 100644 --- a/libs/scm/ui.lua +++ b/libs/scm/ui.lua @@ -2,7 +2,6 @@ local UI = {} SCM.UI = UI do - local scripts = SCM.ScriptManager.scripts --Redundant function UI:printUsage() print("Usage: scm [args]") @@ -15,6 +14,7 @@ do end function UI:listScripts() + local scripts = SCM.ScriptManager.scripts print("name", "type") print("----------------------") for i = 1, #scripts, 1 do @@ -75,4 +75,4 @@ do end end -return UI \ No newline at end of file +return UI