diff --git a/README.md b/README.md index a4b703e..50a3701 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ -## A package repository for [Mudlet](https://www.mudlet.org) ## +## A Package Repository for [Mudlet](https://www.mudlet.org) ## -This Github repository hosts packages made for the Mudlet MUD client. It is also home to the development of **mpkg**, a command line interface for managing packages used in [Mudlet](https://www.mudlet.org) which helps you install, remove, search the repository and update packages from the Mudlet command line. +This Github repository hosts packages made for the Mudlet MUD client. It is also home to the development of **mpkg**, a command line interface for managing packages used in [Mudlet](https://www.mudlet.org) which helps you install, remove, search and update packages from the Mudlet command line. ### Installation ### -To get started, install the `mpkg` client in Mudlet by issuing; +mpkg comes preinstalled on later (4.20+) versions of Mudlet. If you do not have it, consider upgrading or +issue this command on any profile you wish to use mpkg with. ```lua installPackage("https://mudlet.github.io/mudlet-package-repository/packages/mpkg.mpackage")``` @@ -12,20 +13,21 @@ Then on the command line you can issue any of the following commands. ``` Commands: - mpkg help -- show this help - mpkg install -- install a new package - mpkg list -- list all installed packages + mpkg install -- install/upgrade a package + mpkg list -- list all installed packages mpkg remove -- remove an existing package - mpkg search -- search for a package via name and description + mpkg search -- search for a package mpkg show -- show detailed information about a package - mpkg update -- update your package listing - mpkg upgradeable -- show packages that can be upgraded - mpkg debug -- turn on debugging messages + mpkg update -- update package listing from repository ``` ### Submit a Package ### -Have you created a package you wish to share? There are currently two methods to get your package added to this repository. +Have you created a package you wish to share? There are two methods to get your package added to this repository. + +### via the Package Website ### + +Visit the main package website (https://packages.mudlet.org) and follow the links to upload. #### via Github Method #### @@ -34,11 +36,8 @@ Contribute your mpackage using Github's "[fork and pull request](https://docs.gi - copy your mpackage to the cloned repository subdirectory `packages/` (no further files need to be changed, Github workflows take care of rebuilding the repository index), - commit, push and submit a pull request. -#### via The Repository Maintainer #### - -If you are having trouble submitting via Github, or do not yet have an account (and we really suggest you do to help support Mudlet!), you can contact Zooka on [Mudlet's Discord server](https://discordapp.com/invite/kuYvMQ9) and he will assist in getting your package submitted. - ### Development ### + Using Docker, run `./mpkg/muddle` or run Muddler [manually](https://github.com/demonnic/muddler/wiki/Installation) to generate the new `./mpkg/build/mpkg.mpackage` file can be loaded into Mudlet as a module. ### Report Issues ### diff --git a/mpkg/README.md b/mpkg/README.md index 8c59a0a..25c619a 100644 --- a/mpkg/README.md +++ b/mpkg/README.md @@ -5,17 +5,14 @@ mpkg is a command line interface for managing packages used in Mudlet. You can i ``` Commands: - mpkg help -- show this help mpkg install -- install a new package mpkg list -- list all installed packages mpkg remove -- remove an existing package mpkg search -- search for a package via name and description mpkg show -- show detailed information about a package mpkg update -- update your package listing - mpkg upgrade -- upgrade a specific package - mpkg upgradeable -- show packages that can be upgraded - mpkg debug -- turn on debugging messages ``` ### See Also +* https://packages.mudlet.org * https://mudlet.github.io/mudlet-package-repository/ diff --git a/mpkg/mfile b/mpkg/mfile index 54fb04d..c1ad227 100644 --- a/mpkg/mfile +++ b/mpkg/mfile @@ -2,9 +2,9 @@ "package": "mpkg", "title": "A command line package manager for Mudlet.", "description": "", - "version": "2.8", - "author": "Zooka", - "icon": "inventory-management.png", + "version": "3.0", + "author": "Mudlet Default Package", + "icon": "mudlet-package-repo.ico", "dependencies": "", "outputFile": false } diff --git a/mpkg/src/resources/.mudlet/Icon/inventory-management.png b/mpkg/src/resources/.mudlet/Icon/inventory-management.png deleted file mode 100644 index f774c81..0000000 Binary files a/mpkg/src/resources/.mudlet/Icon/inventory-management.png and /dev/null differ diff --git a/mpkg/src/resources/.mudlet/Icon/mudlet-package-repo.ico b/mpkg/src/resources/.mudlet/Icon/mudlet-package-repo.ico new file mode 100644 index 0000000..092ddbd Binary files /dev/null and b/mpkg/src/resources/.mudlet/Icon/mudlet-package-repo.ico differ diff --git a/mpkg/src/scripts/Mudlet_Package_Manager_CLI.lua b/mpkg/src/scripts/Mudlet_Package_Manager_CLI.lua index 3b40c54..107d665 100644 --- a/mpkg/src/scripts/Mudlet_Package_Manager_CLI.lua +++ b/mpkg/src/scripts/Mudlet_Package_Manager_CLI.lua @@ -1,34 +1,60 @@ --- Command line package manager for Mudlet. --- mpkg = mpkg or {} -mpkg.debug = mpkg.debug or false -mpkg.handlers = mpkg.handlers or {} mpkg.aliases = mpkg.aliases or {} mpkg.maintainer = "https://github.com/mudlet/mudlet-package-repository/issues" mpkg.repository = "https://mudlet.github.io/mudlet-package-repository/packages" +mpkg.website = "http://packages.mudlet.org" +mpkg.websiteUploads = f"{mpkg.website}/upload" mpkg.filename = "mpkg.packages.json" -mpkg.help = [[Mudlet Package Repository (mpkg) - -A command line package manager for Mudlet. - -Commands: - mpkg help -- show this help - mpkg install -- install a new package - mpkg list -- list all installed packages - mpkg remove -- remove an existing package - mpkg search -- search for a package via name and description - mpkg show -- show detailed information about a package - mpkg update -- update your package listing - mpkg upgrade -- upgrade a specific package - mpkg upgradeable -- show packages that can be upgraded - mpkg debug -- turn on debugging messages]] --- Entry point of script. --- If we switch to an event handler this will be the function that is to be called. function mpkg.initialise() + + -- clean up any old info + mpkg.uninstallSelf() + + registerNamedEventHandler("mpkg", "download", "sysDownloadDone", "mpkg.eventHandler") + registerNamedEventHandler("mpkg", "download-error", "sysDownloadError", "mpkg.eventHandler") + registerNamedEventHandler("mpkg", "installed", "sysInstallPackage", "mpkg.eventHandler") + registerNamedEventHandler("mpkg", "uninstalled", "sysUninstallPackage", "mpkg.eventHandler") + + mpkg.aliases = mpkg.aliases or {} + + table.insert(mpkg.aliases, tempAlias("^(mpkg|mp)( help)?$", mpkg.displayHelp)) + table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) install(?: (.+))?$", function() mpkg.install(matches[3]) end)) + table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) list$", mpkg.listInstalledPackages)) + table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) remove(?: (.+))?$", function() mpkg.remove(matches[3]) end)) + table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) show(?: (.+))?$", function() mpkg.show(matches[3]) end)) + table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) search(?: (.+))?$", function() mpkg.search(matches[3]) end)) + table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) update$", function() mpkg.updatePackageList(false) end)) + + -- Setup a named timer for automatic repository listing updates every 12 hours (60s*60m*12h=43200s) + registerNamedTimer("mpkg", "mpkg update package listing timer", 43200, function() mpkg.updatePackageList(true) end, true) + + --mpkg.reloadPackageListFromFile() + mpkg.updatePackageList(true) +end + + +--- Check if mpkg is ready for use. +-- External factors, e.g., no internet connection can cause +-- mpkg to fail. We'll call this in functions where failure +-- is likely. +function mpkg.ready(silent) + + if mpkg.packages and mpkg.packages['packages'] then return true end + + if not silent then + mpkg.echo("Aborting, unable to read repository information. Retrying package listing update.") + mpkg.updatePackageList(false) + else mpkg.updatePackageList(true) + end + + return false + end @@ -48,21 +74,38 @@ function mpkg.echoLink(args, ...) end +--- Load the local package list if available. +function mpkg.reloadPackageListFromFile() + + local filename = getMudletHomeDir() .. "/" .. mpkg.filename + + local file, error, content = io.open(filename) + + if not error then + content = file:read("*a") + mpkg.packages = json_to_value(content) + io.close(file) + mpkg.checkForUpgrades(true) + end + +end + --- Get which version of a package is installed on in Mudlet. -- @param args the package name as found in getPackageInfo() -- @return nil and error message if not found -- @return string containing a version if found function mpkg.getInstalledVersion(args) - local installedVersion = getPackageInfo(args, "version") - if installedVersion == "" then - return nil, "No version found." - else - return installedVersion - end + local installedVersion = getPackageInfo(args, "version") - return nil, "No package found." + if installedVersion == "" then + return nil, "No version found." + else + return installedVersion + end + + return nil, "No package found." end @@ -71,6 +114,9 @@ end -- @return nil and error message if not found -- @return string containing a version if found function mpkg.getRepositoryVersion(args) + + if not mpkg.ready() then return end + local packages = mpkg.packages["packages"] for i = 1, #packages do @@ -88,6 +134,9 @@ end -- @return nil and error message if not found -- @return an empty table (if no dependencies) or table containing package names if found function mpkg.getDependencies(args) + + if not mpkg.ready() then return end + local packages = mpkg.packages["packages"] for i = 1, #packages do @@ -109,39 +158,68 @@ end -- version using semantic versioning methods. -- @param silent if true, do not display update messages function mpkg.checkForUpgrades(silent) + + if not mpkg.ready(silent) then return end + local packages = mpkg.packages["packages"] local requireUpgrade = {} - for k,v in pairs(getPackages()) do - local installedVersion, iError = mpkg.getInstalledVersion(v) - local repoVersion, rError = mpkg.getRepositoryVersion(v) + for _, pkg in pairs(getPackages()) do + local installedVersion = mpkg.getInstalledVersion(pkg) + local repoVersion = mpkg.getRepositoryVersion(pkg) - if mpkg.debug then - if iError then - mpkg.echo("Checking local package '" .. v .. "': version '" .. iError .. "'") - else - mpkg.echo("Checking local package '" .. v .. "': version '" .. installedVersion .. "'") + if repoVersion and installedVersion then + if semver(installedVersion) < semver(repoVersion) then + table.insert(requireUpgrade, pkg) end + end + end - if repoVersion then - mpkg.echo("Checking repo package '" .. v .. "': version '" .. repoVersion .. "'") - else - mpkg.echo("Package not in repository '" .. v .. "'") + if not silent then + if not table.is_empty(requireUpgrade) then + mpkg.echo("New package upgrades available. The following packages can be upgraded:") + mpkg.echo("") + for _, pkg in pairs(requireUpgrade) do + mpkg.echoLink(f"{pkg} v{mpkg.getInstalledVersion(pkg)} to v{mpkg.getRepositoryVersion(pkg)}", " (click to upgrade)\n", function() mpkg.upgrade(pkg) end, "Click to upgrade", true) end + else + mpkg.echo("No package upgrades are available.") end + end + +end + + +--- Upgrade all packages at once. +-- @param silent do not show mpkg messages, system messages will continue to show +function mpkg.performUpgradeAll(silent) + + if not mpkg.ready() then return end + + local packages = mpkg.packages["packages"] + local requireUpgrade = {} + + for _, pkg in pairs(getPackages()) do + local installedVersion = mpkg.getInstalledVersion(pkg) + local repoVersion = mpkg.getRepositoryVersion(pkg) if repoVersion and installedVersion then if semver(installedVersion) < semver(repoVersion) then - table.insert(requireUpgrade, v) + table.insert(requireUpgrade, pkg) end end end if not table.is_empty(requireUpgrade) then - mpkg.echo("New packages upgrades available. The following packages can be upgraded:") - mpkg.echo("") - for k,v in pairs(requireUpgrade) do - mpkg.echoLink("" .. v .. "" .. " v" .. mpkg.getInstalledVersion(v) .. " to v" .. mpkg.getRepositoryVersion(v), " (click to upgrade)\n", function() mpkg.upgrade(v) end, "Click to upgrade", true) + if not silent then + mpkg.echo("New package upgrades available. The following packages will be upgraded:") + mpkg.echo("") + for _, pkg in pairs(requireUpgrade) do + mpkg.echo(f"{pkg} v{mpkg.getInstalledVersion(pkg)} to v{mpkg.getRepositoryVersion(pkg)}") + end + end + for _, pkg in pairs(requireUpgrade) do + mpkg.upgrade(pkg) end else if not silent then @@ -155,12 +233,31 @@ end --- Print the help file. function mpkg.displayHelp() - local help = string.split(mpkg.help, "\n") + mpkg.echo("Mudlet Package Repository Client (mpkg)") + mpkg.echoLink("", f"[{mpkg.website}]\n", function() openUrl(mpkg.website) end, "open package website", true) + +local help = [[ + +mpkg is a command line interface for managing packages in Mudlet. +You can install, update, remove and search the package repository. + +Commands: + mpkg install -- install/upgrade a package + mpkg list -- list all installed packages + mpkg remove -- remove an existing package + mpkg search -- search for a package + mpkg show -- show detailed information about a package + mpkg update -- update package listing from repository +]] + + local help = string.split(help, "\n") for i = 1, #help do mpkg.echo(help[i]) end +mpkg.echoLink("", "Visit https://packages.mudlet.org/upload to share your package.\n", function() mpkg.openWebUploads() end, "open browser at upload website", true) + end @@ -176,25 +273,32 @@ function mpkg.install(args) return false end - if table.contains(getPackages(), args) then - mpkg.echo("" .. args .. "" .. " is already installed, use mpkg upgrade to install a newer version.") - return false + if not mpkg.ready() then return end + + local installedPackages = getPackages() + + for _, pkg in pairs(installedPackages) do + if string.lower(pkg) == string.lower(args) then + mpkg.echo(f"{pkg} package is already installed. Checking for updates.") + mpkg.upgrade(pkg) + return false + end end local packages = mpkg.packages["packages"] for i = 1, #packages do - if args == packages[i]["mpackage"] then + if string.lower(args) == string.lower(packages[i]["mpackage"]) then -- check for dependencies - local depends = mpkg.getDependencies(args) + local depends = mpkg.getDependencies(packages[i]["mpackage"]) if depends then local unmet = {} - for _,v in pairs(depends) do - if not table.contains(getPackages(), v) then - table.insert(unmet, v) + for _, dep in pairs(depends) do + if not table.contains(getPackages(), dep) then + table.insert(unmet, dep) end end @@ -204,7 +308,7 @@ function mpkg.install(args) mpkg.echo("Please install the following packages first.") mpkg.echo("") - for _,v in pairs(unmet) do + for _, v in pairs(unmet) do mpkg.echo(v) end @@ -212,14 +316,14 @@ function mpkg.install(args) end end - mpkg.echo("Installing " .. args .. " (version " .. packages[i]["version"] .. ").") - installPackage(mpkg.repository .. "/" .. args .. ".mpackage") + mpkg.echo(f"Installing {args} (v{packages[i]['version']}).") + installPackage(f"{mpkg.repository}/{packages[i]['filename']}") return true end end - mpkg.echo("Unable to locate " .. args .. " in repository.") + mpkg.echo(f"Unable to locate {args} package in repository.") return false end @@ -236,25 +340,19 @@ function mpkg.remove(args) return false end - if not table.contains(getPackages(), args) then - mpkg.echo("" .. args .. " package is not currently installed.") - return false - end - - local success = uninstallPackage(args) - mpkg.echo("" .. args .. " removed.") + local installedPackages = getPackages() - -- TODO: uninstallPackage doesn't currently provide a return value - -- is it necessary to perform a post check on installed packages and compare? - --[[ - if success then - mpkg.echo(args .. "uninstalled.") - else - mpkg.echo("Unable to uninstall.") + for _, pkg in pairs(installedPackages) do + if string.lower(pkg) == string.lower(args) then + uninstallPackage(pkg) + mpkg.echo(f"{pkg} package removed.") + return true + end end - ]]-- - return true + mpkg.echo(f"{args} package is not currently installed.") + return false + end @@ -264,15 +362,28 @@ end -- @return false if there was an error -- @return true if packages was successfully uninstalled/removed function mpkg.upgrade(args) + if not args then mpkg.echo("Missing package name.") mpkg.echo("Syntax: mpkg upgrade ") + mpkg.echo(" mpkg upgrade all") + return false + end + + if not table.contains(getPackages(), args) then + mpkg.echo(f"{args} package is not installed.") return false end - -- if no errors removing then install - if mpkg.remove(args) then - tempTimer(2, function() mpkg.install(args) end) + if not mpkg.ready() then return false end + + if semver(mpkg.getInstalledVersion(args)) < semver(mpkg.getRepositoryVersion(args)) then + -- if no errors removing then install + if mpkg.remove(args) then + tempTimer(2, function() mpkg.install(args) end) + end + else + mpkg.echo(f"Currently installed, {args} v{mpkg.getInstalledVersion(args)} is the latest version.") end end @@ -284,6 +395,8 @@ function mpkg.updatePackageList(silent) downloadFile(saveto, mpkg.repository .. "/" .. mpkg.filename) if not silent then mpkg.echo("Updating package listing from repository.") + mpkg.displayUpdateMessage = true + mpkg.silentFailures = nil end end @@ -291,24 +404,29 @@ end --- Print a list of locally installed packages in Mudlet. function mpkg.listInstalledPackages() - mpkg.echo("Listing locally installed packages:") + if not mpkg.ready() then return end - if mpkg.debug then - mpkg.echo("DEBUG:") - display(getPackages()) - end + local sf = string.format + + mpkg.echo("Listing installed packages:") for _,v in pairs(getPackages()) do local version, error = mpkg.getInstalledVersion(v) if error then - mpkg.echo(" " .. v .. " (unknown version)") + mpkg.echoLink(" ", sf("%-30s %-20s\n", v, "(unknown version)"), function() mpkg.show(v) end, "show details", true) else - mpkg.echo(" " .. v .. " (version: " .. mpkg.getInstalledVersion(v) .. ")") + mpkg.echoLink(" ", sf("%-30s %-20s", v, f"(v{mpkg.getInstalledVersion(v)})"), function() mpkg.show(v) end, "show details", true) + + if semver(mpkg.getInstalledVersion(v)) < semver(mpkg.getRepositoryVersion(v)) then + echoLink("", "[update available]\n", function() mpkg.upgrade(v) end, "click to update package", true) + else + echo("\n") + end end end local count = table.size(getPackages()) - mpkg.echo(count == 1 and count .. " package installed." or count .. " packages installed.") + mpkg.echo(count == 1 and f"{count} package installed." or f"{count} packages installed.") end @@ -327,7 +445,9 @@ function mpkg.search(args) return end - mpkg.echo("Searching for " .. args .. " in repository.") + if not mpkg.ready() then return end + + mpkg.echo(f"Searching repository for {args}.") local count = 0 @@ -338,8 +458,13 @@ function mpkg.search(args) for i = 1, #packages do if string.find(string.lower(packages[i]["mpackage"]), args, 1, true) or string.find(string.lower(packages[i]["title"]), args, 1, true) then mpkg.echo("") - mpkg.echo("Package: " .. packages[i]["mpackage"] .. " (version: " .. packages[i]["version"] .. ")") - mpkg.echo(" " .. packages[i]["title"] .. "") + mpkg.echoLink(" ", f"{packages[i]['mpackage']} (v{packages[i]['version']}) ", function() mpkg.show(packages[i]["mpackage"], true) end, "show details", true) + if table.contains(getPackages(), packages[i]["mpackage"]) then + echo("[installed]\n") + else + echoLink("", "[install now]\n", function() mpkg.install(packages[i]["mpackage"]) end, "install now", true) + end + mpkg.echo(f" {packages[i]['title']}") count = count + 1 end end @@ -357,32 +482,83 @@ end --- Print out detailed package information. -- Display; title, version, author, installation status, description. -- @param args the package name as listed in the repository +-- @param repoOnly skip local details, show only repository info -- @return false if error or no matching package was found -- @return true if information was displayed -function mpkg.show(args) +function mpkg.show(args, repoOnly) if not args then mpkg.echo("Missing package name.") - mpkg.echo("Syntax: mpkg show ") + if repoOnly then + mpkg.echo("Syntax: mpkg show-repo ") + else + mpkg.echo("Syntax: mpkg show ") + end return false end - local packages = mpkg.packages["packages"] + if not mpkg.ready() then return end - for i = 1, #packages do - if args == packages[i]["mpackage"] then + local packages + + if not repoOnly then + -- search locally first, then the repository if nothing was found + packages = getPackages() + + for _, pkg in pairs(packages) do + if string.lower(args) == string.lower(pkg) then + local name = getPackageInfo(pkg, "mpackage") + local title = getPackageInfo(pkg, "title") + local version = getPackageInfo(pkg, "version") - mpkg.echo("Package: " .. packages[i]["mpackage"] .. " (version: " .. packages[i]["version"] .. ") by " .. packages[i]["author"]) - mpkg.echo(" " .. packages[i]["title"]) + if name == "" then + mpkg.echo("This package does not contain any further details. It was likely installed from a XML import and not an mpackage.") + else + mpkg.echo(f"Package: {name}") + mpkg.echo(f" {title}") + mpkg.echo("") + mpkg.echo(f"Status: installed (version: {version})") + mpkg.echo("") + mpkg.echo("Description:") + + local description = string.split(getPackageInfo(pkg, "description"), "\n") + + for i = 1, #description do + mpkg.echo(description[i]) + end + end + + -- check it's not a non-versioned XML/package first + local installedVersion = mpkg.getInstalledVersion(pkg) + if installedVersion and (semver(installedVersion) < semver(mpkg.getRepositoryVersion(pkg))) then + mpkg.echo("") + mpkg.echoLink("There is a ", "newer version available.\n", function() mpkg.show(pkg, true) end, "view details", true) + end + + return true + end + end + + mpkg.echo(f"No package matching {args} found locally, search the repository.") + + end + + -- now search the repository + packages = mpkg.packages["packages"] + + for i = 1, #packages do + if string.lower(args) == string.lower(packages[i]["mpackage"]) then + mpkg.echo(f"Package: {packages[i]['mpackage']} (version: {packages[i]['version']}) by {packages[i]['author']}") + mpkg.echo(f" {packages[i]['title']}") mpkg.echo("") - local installedVersion = mpkg.getInstalledVersion(packages[i]["mpackage"]) - if installedVersion then - mpkg.echo("Status: installed (version: " .. installedVersion .. ")") + local version = getPackageInfo(args, "version") + + if version == "" then + mpkg.echoLink("Status: not installed ", "[install now]\n", function() mpkg.install(packages[i]["mpackage"]) end, "install now", true) else - mpkg.echo("Status: not installed") + mpkg.echo(f"Status: installed (version: {version})") end - mpkg.echo("") local description = string.split(packages[i]["description"], "\n") @@ -395,50 +571,36 @@ function mpkg.show(args) end end - mpkg.echo("No package matching " .. args .. " found in repository. Try mpkg search.") + mpkg.echo(f"No package matching {args} found in the repository. Try mpkg search.") return false end ---- Toggles debugging information for program diagnostics. --- Prints out further information when debug is true (default: false) when calling; --- checkForUpgrades(), listInstalledPackages(), whenever the eventHandler is fired. -function mpkg.toggleDebug() - - if mpkg.debug then - mpkg.debug = false - mpkg.echo("mpackage debugging disabled.") - else - mpkg.debug = true - mpkg.echo("mpackage debugging ENABLED.") - end - -end - - ---- Reacts to downloading of repository files. +--- Reacts to downloading of repository files and self install/uninstall events. -- @param event the event which called this handler; sysDownloadError, sysDownloadDone -- @param arg all event args, including the filename associated with the download function mpkg.eventHandler(event, ...) - if mpkg.debug then - display(event) - display(arg) - end - if event == "sysDownloadError" and string.ends(arg[2], mpkg.filename) then - mpkg.echo("Failed to download package listing.") + if not mpkg.silentFailures then + mpkg.echo("Failed to download package listing.") + mpkg.silentFailures = true + end return end if event == "sysDownloadDone" and arg[1] == getMudletHomeDir() .. "/" .. mpkg.filename then - --mpkg.echo("Package listing downloaded.") + + if mpkg.displayUpdateMessage then + mpkg.echo("Package listing downloaded.") + mpkg.displayUpdateMessage = nil + end local file, error, content = io.open(arg[1]) if error then - mpkg.echo("Error reading package listing file. Please file a bug report at " .. mpkg.maintainer) + mpkg.echo(f"Error reading package listing file. Please file a bug report at {mpkg.maintainer}") else content = file:read("*a") mpkg.packages = json_to_value(content) @@ -447,45 +609,52 @@ function mpkg.eventHandler(event, ...) end if semver(mpkg.getInstalledVersion("mpkg")) < semver(mpkg.getRepositoryVersion("mpkg")) then - mpkg.echo("New version of mpkg found. Automatically upgrading to " .. mpkg.getRepositoryVersion("mpkg")) + mpkg.echo(f"New version of mpkg found. Automatically upgrading to {mpkg.getRepositoryVersion('mpkg')}") mpkg.remove("mpkg") tempTimer(2, function() mpkg.install("mpkg") end) end end -end + if event == "sysUninstallPackage" and arg[1] == "mpkg" then + mpkg.uninstallSelf() + return + end --- Setup a named timer for automatic repository listing updates every 12 hours (60s*60m*12h=43200s) -deleteNamedTimer("mpkg", "mpkg update package listing timer") -registerNamedTimer("mpkg", "mpkg update package listing timer", 43200, function() mpkg.updatePackageList(true) end, true) + if event == "sysInstallPackage" and arg[1] == "mpkg" then + mpkg.displayHelp() + return + end --- Setup the event handlers, removing any previous ones. -for _,v in pairs(mpkg.handlers) do - killAnonymousEventHandler(v) end -mpkg.handlers = {} +--- Open a browser at the uploads URL. +function mpkg.openWebUploads() -table.insert(mpkg.handlers, registerAnonymousEventHandler("sysDownloadDone", "mpkg.eventHandler")) -table.insert(mpkg.handlers, registerAnonymousEventHandler("sysDownloadError", "mpkg.eventHandler")) + mpkg.echo("Redirecting to the package repository website.") + openUrl(mpkg.websiteUploads) --- Setup the command line aliases, removing any previous ones. -for _,v in pairs(mpkg.aliases) do - killAlias(v) end -mpkg.aliases = {} +-- clean up after uninstallation of mpkg +function mpkg.uninstallSelf() + + deleteNamedTimer("mpkg", "mpkg update package listing timer") -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp)( help)?$", mpkg.displayHelp)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) debug$", mpkg.toggleDebug)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) install(?: (.+))?$", function() mpkg.install(matches[3]) end)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) remove(?: (.+))?$", function() mpkg.remove(matches[3]) end)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) list$", mpkg.listInstalledPackages)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) update$", function() mpkg.updatePackageList(false) end)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) upgrade(?: (.+))?$", function() mpkg.upgrade(matches[3]) end)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) upgradeable$", function() mpkg.checkForUpgrades(false) end)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) show(?: (.+))?$", function() mpkg.show(matches[3]) end)) -table.insert(mpkg.aliases, tempAlias("^(mpkg|mp) search(?: (.+))?$", function() mpkg.search(matches[3]) end)) + deleteNamedEventHandler("mpkg", "download") + deleteNamedEventHandler("mpkg", "download-error") + deleteNamedEventHandler("mpkg", "installed") + deleteNamedEventHandler("mpkg", "uninstalled") + + if mpkg and mpkg.aliases then + for _, v in pairs(mpkg.aliases) do + killAlias(v) + end + end + + mpkg.aliases = nil + +end -- call the script entry point function -mpkg.initialise() +-- delay this because we need semantic versioning to be loaded as well +tempTimer(0, function() mpkg.initialise() end, false)