funktioniert nimmer aber heut is gut

This commit is contained in:
2024-02-26 02:25:25 +01:00
parent 6165808cc4
commit 9253f00eef
2 changed files with 123 additions and 46 deletions

View File

@@ -19,7 +19,6 @@ function clearScreen()
drawLine(1, 1, w, h, 0x000000) drawLine(1, 1, w, h, 0x000000)
end end
function drawLine(startX, startY, stopX, stopY, colorOfLine) function drawLine(startX, startY, stopX, stopY, colorOfLine)
local oldColor = gpu.getBackground(false) local oldColor = gpu.getBackground(false)
gpu.setBackground(colorOfLine, false) gpu.setBackground(colorOfLine, false)

View File

@@ -9,6 +9,7 @@ NO = {"n","no","N","No","NO"}
SupportedRemotes = { SupportedRemotes = {
Github = { Github = {
BaseUrl = "https://github.com/", BaseUrl = "https://github.com/",
RawApiUrl = "https://raw.githubusercontent.com/",
Implemented = true Implemented = true
}, },
Gitea = { Gitea = {
@@ -17,7 +18,15 @@ SupportedRemotes = {
} }
} }
Repository = {} EmptyRepository = {
Owner = nil,
Name = nil,
ShortName = nil,
RepoIdentifier = nil,
Remote = nil,
CurrentBranch = nil,
CurrentLocalPath = nil
}
---default repo to update ---default repo to update
DefaultRepository = { DefaultRepository = {
@@ -67,8 +76,17 @@ local function askTextQuestion(question, defaultAnswerOnEnter, allowOnly)
if userInput == "" then return defaultAnswerOnEnter else return userInput end if userInput == "" then return defaultAnswerOnEnter else return userInput end
end end
local function makeDirIfNotExists(target)
if filesystem.exists(target) then
if not filesystem.isDirectory(target) then error("target directory already exists and is not a directory.") end
if filesystem.get(target).isReadOnly() then error("target directory is read-only.") end
else
if not filesystem.makeDirectory(target) then error("directory creation failed") end
end
end
--- todo: pcall and catch errors --- todo: pcall and catch errors
local function downloadRepo(repository, remoteType, autoOverride) local function downloadRepo(repository, remote, autoOverride)
local function validateRepositoryIdentifier(repository) local function validateRepositoryIdentifier(repository)
if not repository.RepoIdentifier:match("^[%w-.]*/[%w-.]*$") then if not repository.RepoIdentifier:match("^[%w-.]*/[%w-.]*$") then
@@ -79,24 +97,15 @@ local function downloadRepo(repository, remoteType, autoOverride)
validateRepositoryIdentifier(repository) validateRepositoryIdentifier(repository)
local function makeDirIfNotExists(target)
if filesystem.exists(target) then
if not filesystem.isDirectory(target) then error("target directory already exists and is not a directory.") end
if filesystem.get(target).isReadOnly() then error("target directory is read-only.") end
else
if not filesystem.makeDirectory(target) then error("directory creation failed") end
end
end
--- FIXME: If download only mode is enabled, set this to /home. still todo --- FIXME: If download only mode is enabled, set this to /home. still todo
local targetDownloadPath = DefaultTemporaryDownloadPath..repository.RepoIdentifier local targetDownloadPath = DefaultTemporaryDownloadPath..repository.RepoIdentifier
repository.CurrentLocalPath = targetDownloadPath repository.CurrentLocalPath = targetDownloadPath.."/"
local success, err = pcall(makeDirIfNotExists, targetDownloadPath) local success, res = pcall(makeDirIfNotExists, targetDownloadPath)
if not success then error("the download failed because of filesystem errors.") end if not success then error("the download failed because of filesystem errors.") end
local function fetchFilesAndSubdirs(repository, remoteType, dir) local function fetchFilesAndSubdirs(repository, remote, dir)
dir = dir or "" -- default value, start at root dir dir = dir or "" -- default value, start at root dir
if remoteType == SupportedRemotes.Github then if remote == SupportedRemotes.Github then
print("fetching contents for "..repository.RepoIdentifier..dir) print("fetching contents for "..repository.RepoIdentifier..dir)
local githubApiUrl="https://api.github.com/repos/"..repository.RepoIdentifier.."/contents"..dir local githubApiUrl="https://api.github.com/repos/"..repository.RepoIdentifier.."/contents"..dir
local success,chunks=pcall(internet.request,githubApiUrl) local success,chunks=pcall(internet.request,githubApiUrl)
@@ -116,7 +125,7 @@ local function downloadRepo(repository, remoteType, autoOverride)
if t[i].type=="dir" then if t[i].type=="dir" then
table.insert(directories,dir.."/"..t[i].name) table.insert(directories,dir.."/"..t[i].name)
local subfiles,subdirs=fetchFilesAndSubdirs(repository,SupportedRemotes.Github,dir.."/"..t[i].name) local subfiles,subdirs=fetchFilesAndSubdirs(repository,remote,dir.."/"..t[i].name)
for i=1,#subfiles do for i=1,#subfiles do
table.insert(files,subfiles[i]) table.insert(files,subfiles[i])
end end
@@ -133,7 +142,7 @@ local function downloadRepo(repository, remoteType, autoOverride)
end end
--- fetch and make dirs in the target download path recursively --- fetch and make dirs in the target download path recursively
local files,dirs=fetchFilesAndSubdirs(repository.RepoIdentifier, remoteType, "") local files,dirs=fetchFilesAndSubdirs(repository, repository.Remote, "")
for i=1,#dirs do for i=1,#dirs do
local success, err = pcall(makeDirIfNotExists, targetDownloadPath..dirs[i]) local success, err = pcall(makeDirIfNotExists, targetDownloadPath..dirs[i])
if not success then error(("the download failed because of filesystem errors. %x"):format(err)) end if not success then error(("the download failed because of filesystem errors. %x"):format(err)) end
@@ -143,80 +152,141 @@ local function downloadRepo(repository, remoteType, autoOverride)
if autoOverride == true then replaceMode = "always" end if autoOverride == true then replaceMode = "always" end
local function downloadFiles(files, targetDownloadPath, replaceMode) local function downloadFiles(files, targetDownloadPath, replaceMode)
for i=1,#files do
local replace=nil local replace=nil
if filesystem.exists(targetDownloadPath..files[i]) then local downloadedFileTargets = {}
for i=1,#files do
if replaceMode == "always" then replace = true end
local fileExists = filesystem.exists(targetDownloadPath..files[i])
if fileExists then
--- FIXME dir löschen statt error --- FIXME dir löschen statt error
if filesystem.isDirectory(targetDownloadPath..files[i]) then error("file "..targetDownloadPath..files[i].." blocked by directory with same name!") end if filesystem.isDirectory(targetDownloadPath..files[i]) then error("file "..targetDownloadPath..files[i].." blocked by directory with same name!") end
if not replaceMode == "ask" then
replace = (replaceMode=="always")
replace = not (replaceMode=="never")
end
else print("\nFile "..targetDownloadPath..files[i].." already exists.\nReplace with new version?") end
local response=""
if replace == nil then if replace == nil then
--- FIXME: ggf falsch und buggy
local userInput = askTextQuestion("\nFile "..targetDownloadPath..files[i].." already exists.\nReplace with new version?","n",{"y","n","A","S"}) local userInput = askTextQuestion("\nFile "..targetDownloadPath..files[i].." already exists.\nReplace with new version?","n",{"y","n","A","S"})
--- FIXME: replaceMode ist broken. A wird immer wieder gefragt.
if userInput=="A" then if userInput=="A" then
replaceMode="always" replaceMode="always"
userInput="y" userInput="y"
elseif userInput=="S" then elseif userInput=="S" then
replaceMode="never" replaceMode="never"
userInput="n" userInput="n"
elseif userInput == "y" then replace=true end
elseif userInput == "n" then replace=false if userInput == "y" then replace=true end
if userInput == "n" then replace=false end
end end
end end
if replace then filesystem.remove(targetDownloadPath..files[i]) end if fileExists and replace then filesystem.remove(targetDownloadPath..files[i]) end
if replace or replace == nil then if replace or (replace == nil) then
--- TODO @Freddy: Coole Animation hinzufügen --- TODO @Freddy: Coole Animation hinzufügen
print("downloading file "..files[i]) print("downloading file "..files[i])
local url=repository.Remote.BaseUrl..repository.Name.."/"..repository.CurrentBranch..files[i] --- HACK: RawApiUrl is for using Github Raw API. may have to be patched for Gitea Support
local url=repository.Remote.RawApiUrl..repository.RepoIdentifier.."/"..repository.CurrentBranch..files[i]
local success,response=pcall(internet.request,url) local success,response=pcall(internet.request,url)
if success then if success then
local raw="" local raw=""
for chunk in response do for chunk in response do
raw=raw..chunk raw=raw..chunk
end end
print("writing to "..targetDownloadPath..files[i]) local absoluteDownloadFileTargetPath = targetDownloadPath..files[i]
local file=io.open(targetDownloadPath..files[i],"w") print("writing to "..absoluteDownloadFileTargetPath)
local file=io.open(absoluteDownloadFileTargetPath,"w")
if file then -- might be nil under wierd circumstances if file then -- might be nil under wierd circumstances
file:write(raw) file:write(raw)
file:close() file:close()
end end
table.insert(downloadedFileTargets, files[i])
else error("a file did not download correctly. aborting") end else error("a file did not download correctly. aborting") end
else error("file not removed, but installation was cancelled - This might result in a broken install.") end
end end
end --- return list of all abolute paths the files were downloaded to
return downloadedFileTargets
end end
success, err = pcall(downloadFiles, files, targetDownloadPath, replaceMode) success, res = pcall(downloadFiles, files, targetDownloadPath, replaceMode)
if success then print("All files downloaded successfully.") else error(err) end if success then print("All files downloaded successfully.") return res else error(res) end
end end
local function installShortcut(currentRepoPath, shortcutName) local function installFiles(downloadedFiles, downloadTargetDir, installTargetDir)
local installedFiles = {}
--- FIXME: ask user about replacing files.
local replace = true
makeDirIfNotExists(installTargetDir)
for idx, file in pairs(downloadedFiles) do
print(idx..": Installing File "..downloadTargetDir..file:sub(2,-1).." to target directory "..installTargetDir..file)
local fileExists = filesystem.exists(installTargetDir..file)
if fileExists and replace then filesystem.remove(installTargetDir..file) end
if replace or (replace == nil) then
--- TODO @Freddy: Coole Animation hinzufügen
os.execute("cp "..downloadTargetDir..file:sub(2,-1).." "..installTargetDir..file)
table.insert(installedFiles, installTargetDir..file)
else error("file not removed, but installation was cancelled - This might result in a broken install.") end
end
return installedFiles
end
local function installShortcut(currentRepoPath, shortcutName, targetDir)
print("installing shortcut...") print("installing shortcut...")
os.execute("mv "..currentRepoPath.."shortcut.lua /usr/bin/"..shortcutName..".lua") makeDirIfNotExists(targetDir)
os.execute("mv "..currentRepoPath.."shortcut.lua "..targetDir..shortcutName..".lua")
end end
--- todo: automatic read of dependency list (txt file containing lines with <link> <dst> or somethink like it) --- todo: automatic read of dependency list (txt file containing lines with <link> <dst> or somethink like it)
--- removeme --- removeme
local function legacyInstallDependencies() local function legacyInstallDependencies()
print("Legacy Mode: Installing hardcoded dependencies...")
os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/shapes_default.lua /lib/shapes_default.lua") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/shapes_default.lua /lib/shapes_default.lua")
os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/GUI.lua /lib/GUI.lua") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/GUI.lua /lib/GUI.lua")
os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/term_mod.lua /lib/term_mod.lua") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/term_mod.lua /lib/term_mod.lua")
os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/tech_demo.lua /home/GUI_tech_demo.lua") os.execute("wget -f https://github.com/kevinkk525/OC-GUI-API/raw/master/tech_demo.lua /home/GUI_tech_demo.lua")
end end
local function removeDownloads(downloadTargetDir, downloadedFiles)
for idx, file in pairs(downloadedFiles) do
file = file:sub(2,-1) --- get "file" from previous string "/file"
print(idx..": Removing temporary file "..downloadTargetDir..file)
local fileExists = filesystem.exists(downloadTargetDir..file)
if fileExists then filesystem.remove(downloadTargetDir..file) end
end
print("cleaned temporary files.")
end
local function createManifest(installedFiles, installedShortcuts, manifestTarget, repoName)
makeDirIfNotExists(manifestTarget)
local manifest = ""
for idx, file in pairs(installedFiles) do
manifest = manifest..file.."\n"
end
for idy, shortcut in pairs(installedShortcuts) do
manifest = manifest..shortcut.."\n"
end
print("writing manifest to "..manifestTarget.."/"..repoName)
local file=io.open(manifestTarget.."/"..repoName,"w")
if file then -- might be nil under wierd circumstances
file:write(manifest)
file:close()
end
end
--- fix legacy shit --- fix legacy shit
local function runFullInstallTask(repository) local function runFullInstallTask(repository, shortcutName)
--- first, download the actual repo. --- first, download the actual repo.
--- then, find dependencies - if then exist, download and install them --- then, find dependencies - if then exist, download and install them
--- then, install the actual program --- then, install the actual program
local installTargetDir = "/usr/"..repository.Name
local shortcutTargetDir = "/usr/bin/"
local downloadTargetDir = DefaultTemporaryDownloadPath
local manifestTarget = "/etc/manifest/"
legacyInstallDependencies() --only for testing while new version not implemented yet legacyInstallDependencies() --only for testing while new version not implemented yet
print("downloading "..repository.RepoIdentifier) print("downloading "..repository.RepoIdentifier)
downloadRepo(repository, repository.Remote, false) --enable auto-overwrite in other situations still todo local downloadedFiles = downloadRepo(repository, repository.Remote, false) --enable auto-overwrite in other situations still todo
installShortcut(repository.CurrentLocalPath) local installedFiles = installFiles(downloadedFiles, downloadTargetDir, installTargetDir)
local installedShortcuts = installShortcut(repository.CurrentLocalPath, shortcutName, shortcutTargetDir)
removeDownloads(downloadTargetDir, downloadedFiles)
createManifest(installedFiles, installedShortcuts, manifestTarget, repository.Name)
end end
local function printHelpText() local function printHelpText()
@@ -229,6 +299,9 @@ local function printHelpText()
end end
local function run(cliArgs) local function run(cliArgs)
local repo = EmptyRepository
local shortcutName = nil
if #cliArgs<1 then if #cliArgs<1 then
print("No Arguments given. For help, please check -h or --help") print("No Arguments given. For help, please check -h or --help")
return return
@@ -238,8 +311,13 @@ local function run(cliArgs)
return return
elseif cliArgs[1] == "-d" then elseif cliArgs[1] == "-d" then
--- ask user about repo --- ask user about repo
Repository = DefaultRepository if askYesOrNoQuestion("Use default config? (github::seesberger/PowerManager)?",YES,NO,true) == true then
if askYesOrNoQuestion("Use default config? (github::seesberger/PowerManager)?",YES,NO,true) == true then runFullInstallTask(DefaultRepository) end repo = DefaultRepository
--- ask user about shortcutname
if askYesOrNoQuestion("Use default shortcut name \"powerman\"?",YES,NO,true) then shortcutName = "powerman" end
runFullInstallTask(repo, shortcutName)
--- TODO non-default config
else print("other things not implemented yet") end
else else
print('"'..cliArgs[1]..'" - Bad argument. Try --help') print('"'..cliArgs[1]..'" - Bad argument. Try --help')
print("Program exited.") print("Program exited.")