#274 cleanup functionality added to installer

This commit is contained in:
Mikayla Fischler 2023-07-16 19:42:20 -04:00
parent e1b4d72ef8
commit 2a541ef3fe
2 changed files with 112 additions and 44 deletions

View File

@ -20,7 +20,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local function println(message) print(tostring(message)) end local function println(message) print(tostring(message)) end
local function print(message) term.write(tostring(message)) end local function print(message) term.write(tostring(message)) end
local CCMSI_VERSION = "v1.5a" local CCMSI_VERSION = "v1.6"
local install_dir = "/.install-cache" local install_dir = "/.install-cache"
local manifest_path = "https://mikaylafischler.github.io/cc-mek-scada/manifests/" local manifest_path = "https://mikaylafischler.github.io/cc-mek-scada/manifests/"
@ -93,9 +93,7 @@ local function get_remote_manifest()
end end
local ok, manifest = pcall(function () return textutils.unserializeJSON(response.readAll()) end) local ok, manifest = pcall(function () return textutils.unserializeJSON(response.readAll()) end)
if not ok then if not ok then red();println("error parsing remote installation manifest");white() end
red(); println("error parsing remote installation manifest"); white()
end
return ok, manifest return ok, manifest
end end
@ -120,6 +118,73 @@ local function write_install_manifest(manifest, dependencies)
imfile.close() imfile.close()
end end
-- recursively build a tree out of the file manifest
local function gen_tree(manifest)
local function _tree_add(tree, split)
if #split > 1 then
local name = table.remove(split, 1)
if tree[name] == nil then tree[name] = {} end
table.insert(tree[name], _tree_add(tree[name], split))
else return split[1] end
return nil
end
local list, tree = {}, {}
-- make a list of each and every file
for _, files in pairs(manifest.files) do for i = 1, #files do table.insert(list, files[i]) end end
for i = 1, #list do
local split = {}
string.gsub(list[i], "([^/]+)", function(c) split[#split + 1] = c end)
if #split == 1 then table.insert(tree, list[i])
else table.insert(tree, _tree_add(tree, split)) end
end
return tree
end
local function _in_array(val, array)
for _, v in pairs(array) do if v == val then return true end end
return false
end
local function _clean_dir(dir, tree)
local ls = fs.list(dir)
for _, val in pairs(ls) do
local path = dir.."/"..val
if fs.isDir(path) then
_clean_dir(path, tree[val])
if #fs.list(path) == 0 then
fs.delete(path);red();println("deleted dir " .. path);white()
end
elseif not _in_array(val, tree) then
fs.delete(val)
red();println("deleted " .. path);white()
end
end
end
-- go through app/common directories to delete unused files
local function clean(manifest)
local root_ext = false
local tree = gen_tree(manifest)
table.insert(tree, "install_manifest.json")
table.insert(tree, "ccmsi.lua")
table.insert(tree, "log.txt")
local ls = fs.list("/")
for _, val in pairs(ls) do
if fs.isDir(val) then
if tree[val] ~= nil then _clean_dir("/" .. val, tree[val]) end
if #fs.list(val) == 0 then fs.delete(val) end
elseif not _in_array(val, tree) then
root_ext = true
yellow();println(val .. " not used");white()
end
end
if root_ext then println("Files in root directory won't be automatically deleted.") end
end
-- get and validate command line options -- get and validate command line options
println("-- CC Mekanism SCADA Installer " .. CCMSI_VERSION .. " --") println("-- CC Mekanism SCADA Installer " .. CCMSI_VERSION .. " --")
@ -384,6 +449,8 @@ elseif mode == "install" or mode == "update" then
if mode == "install" then if mode == "install" then
println("Installation completed successfully.") println("Installation completed successfully.")
else println("Update completed successfully.") end else println("Update completed successfully.") end
println("Ready to clean up unused files, press enter to continue...")
read();clean(manifest)
else else
if mode == "install" then if mode == "install" then
red();println("Installation failed.") red();println("Installation failed.")
@ -424,6 +491,8 @@ elseif mode == "install" or mode == "update" then
if mode == "install" then if mode == "install" then
println("Installation completed successfully.") println("Installation completed successfully.")
else println("Update completed successfully.") end else println("Update completed successfully.") end
println("Ready to clean up unused files, press enter to continue...")
read();clean(manifest)
else else
red() red()
if mode == "install" then if mode == "install" then
@ -451,6 +520,9 @@ elseif mode == "remove" or mode == "purge" then
-- ask for confirmation -- ask for confirmation
if not ask_y_n("Continue?", false) then return end if not ask_y_n("Continue?", false) then return end
-- delete unused files first
clean(manifest)
local file_list = manifest.files local file_list = manifest.files
local dependencies = manifest.depends[app] local dependencies = manifest.depends[app]
local config_file = app .. "/config.lua" local config_file = app .. "/config.lua"
@ -480,10 +552,7 @@ elseif mode == "remove" or mode == "purge" then
local files = file_list[dependency] local files = file_list[dependency]
for _, file in pairs(files) do for _, file in pairs(files) do
if mode == "purge" or file ~= config_file then if mode == "purge" or file ~= config_file then
if fs.exists(file) then if fs.exists(file) then fs.delete(file);println("deleted " .. file) end
fs.delete(file)
println("deleted " .. file)
end
end end
end end
@ -508,8 +577,7 @@ elseif mode == "remove" or mode == "purge" then
end end
if folder ~= app and fs.isDir(folder) then if folder ~= app and fs.isDir(folder) then
fs.delete(folder) fs.delete(folder);println("deleted app subdirectory " .. folder)
println("deleted app subdirectory " .. folder)
end end
end end
end end

File diff suppressed because one or more lines are too long