Skip to content

Instantly share code, notes, and snippets.

@gesslar
Last active October 29, 2024 18:38
Show Gist options
  • Save gesslar/3da41e52afea6c17a966f3d7b11a71a8 to your computer and use it in GitHub Desktop.
Save gesslar/3da41e52afea6c17a966f3d7b11a71a8 to your computer and use it in GitHub Desktop.
Glu demo - Queue and Dependency
-- Instance of Glu
local glu = Glu.new("queue_dependency_demo")
-- The package names we will be uninstalling and installing
local packageNames = {
"ThreshBeep", "ThreshColorPercent", "ThreshCopy", "ThreshKeepalive",
"ThreshURL",
}
-- Convenience function to filter out the packages which are already
-- installed.
local function getInstalled()
local packages = packageNames
packages = table.n_filter(packages, function(element, index, tbl)
return table.index_of(getPackages(), element) ~= nil
end)
return packages
end
-- -----------------------------------------------------------
-- Installation using Dependency, which uses Queue.
-- -----------------------------------------------------------
-- Using the packageNames, create a table consisting of tables of name and
-- URL for installation.
local packages = glu.table:map(packageNames, function(_, p)
return {
name = p,
url=f"https://github.com/gesslar/{p}/releases/latest/download/{p}.mpackage"
}
end)
-- The dependency instance
local dep
local function startInstall()
local list = table.concat(packageNames, ", ")
cecho("\n<gold>Demonstration of Dependency\n")
cecho("<gold>Installing the following packages: " .. list .. "\n\n")
-- Using the table we created earlier, instantiate a new dependency object
-- with the table of packages and a call back function.
dep = glu.dependency:new(packages, function(status, err )
if e then
cecho(err)
else
cecho("<yellow_green>Dependency installation complete.\n")
end
end)
-- Start the dependency installation.
local ok, a = dep:start()
end
-- -----------------------------------------------------------
-- Uninstallation using Queue
-- -----------------------------------------------------------
local uninstallHandler, queue
-- Process the uninstallation event for each package. Identify if
-- we have any remaining and process the next one, if so; otherwise
-- start the installation!
local function uninstallEvent(event, package)
-- If this is not one of our packages that we are concerned about
-- then nvm.
if not table.index_of(packageNames, package) then return end
local installed = getInstalled()
-- The event handler appears to be called before the package is
-- removed from the list of installed packages is updated, which
-- is why we test for 1 instead of empty.
if table.size(installed) > 1 then
tempTimer(1, function() queue:execute() end)
return
end
cecho("<yellow_green>Uninstallation complete. Starting installation.\n")
killAnonymousEventHandler(uninstallHandler)
startInstall()
end
uninstallHandler = registerAnonymousEventHandler("sysUninstall", uninstallEvent)
-- Queue takes functions as its tasks. So, we take the
-- installed packages we have and create a new table of
-- functions to pass for it to process.
local uninstallFuncs = glu.table:map(getInstalled(),
function(_, p)
return function()
cecho("Uninstalling <b>"..p.."</b>\n")
uninstallPackage(p)
end
end
)
-- If we have some to uninstall, do it.
if table.size(uninstallFuncs) > 0 then
cecho("<gold>Demonstration of Queue\n")
cecho("<gold>Uninstalling the following packages: " .. table.concat(getInstalled(), ", ") .. "\n\n")
queue = glu.queue:new(uninstallFuncs)
queue:execute()
-- Otherwise, just install them.
else
killAnonymousEventHandler(uninstallHandler)
startInstall()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment