Implemented support for installing, uninstalling, and updating mmms without specifying a version explicitly.
h3rald h3rald@h3rald.com
Fri, 02 Feb 2024 13:08:02 +0100
3 files changed,
59 insertions(+),
17 deletions(-)
M
minpkg/core/mmm.nim
→
minpkg/core/mmm.nim
@@ -1,6 +1,7 @@
import std/[json, os, + osproc, httpclient, strutils, sequtils,@@ -28,6 +29,15 @@
proc raiseAlreadyInstalledError(msg: string) = raise MMMAlreadyInstalledError(msg: msg) +proc getDefaultGitBranch(repo: string): string = + let res = execCmdEx("git remote show $#" % [repo]) + return res.output.splitLines().filterIt(it.contains("HEAD branch:"))[0].split(":")[1].strip + +proc getModuleByName(MMM: var MinModuleManager, name: string): JsonNode = + try: + return MMM.modules.filterIt(it.hasKey("name") and it["name"] == %name)[0] + except CatchableError: + raiseError "Module '$#' not found." % [name] proc setup*(MMM: var MinModuleManager, check = true) = MMM.registry = MMMREGISTRY@@ -95,20 +105,29 @@ debug "Creating mmm directory"
createDir(pwd / "mmm") notice "Created a mmm.json file in the current directory" -proc uninstall*(MMM: var MinModuleManager, name, version: string, global = false) = +proc uninstall*(MMM: var MinModuleManager, name, v: string, global = false) = var dir: string + var version = v var versionLabel = version - if version == "": + if version == "*": versionLabel = "<all-versions>" if global: dir = MMM.globalDir / name else: dir = MMM.localDir / name else: + if version == "": + let url = MMM.getModuleByName(name)["url"].getStr + try: + version = getDefaultGitBranch(url) + versionLabel = version + except CatchableError: + raiseError "Unable to determine default branch for module '$#'" % [name] if global: dir = MMM.globalDir / name / version else: dir = MMM.localDir / name / version + debug "Directory: $#" % [dir] let pwd = getCurrentDir() if not global and not fileExists(pwd / "mmm.json"): raiseError "mmm.json not found in current directory. Please run min init to initialize your managed module."@@ -134,7 +153,8 @@
proc uninstall*(MMM: var MinModuleManager, nameAndVersion: string, global = false) = let parts = nameAndVersion.split("@") if parts.len != 2: - raiseError "Invalid module name and version: $#. Expected: <module-name>@<module-version>" % [nameAndVersion] + MMM.uninstall nameAndVersion, "", global + return let name = parts[0] let version = parts[1] MMM.uninstall name, version, global@@ -150,18 +170,10 @@ notice "Done."
except CatchableError: raiseError "Unable to uninstall local managed modules." -proc install*(MMM: var MinModuleManager, name, version: string, global = false) = +proc install*(MMM: var MinModuleManager, name, v: string, global = false) = + var version = v var dir: string let pwd = getCurrentDir() - if global: - dir = MMM.globalDir / name / version - else: - dir = MMM.localDir / name / version - if not fileExists(pwd / "mmm.json"): - raiseError "mmm.json not found in current directory. Please run min init to initialize your managed module." - if dir.dirExists(): - raiseAlreadyInstalledError "Module '$#' (version: $#) is already installed." % [name, version] - dir.createDir() let results = MMM.modules.filterIt(it.hasKey("name") and it["name"] == %name) if results.len == 0: raiseError "Unknown module '$#'." % [name]@@ -174,6 +186,20 @@ raiseError "Unable to install module '$#': Installation method '$#' is not supported" % [name, meth]
if not data.hasKey("url"): raiseError "URL not specified for module '$#'" % [name] let url = data["url"].getStr + if version == "": + try: + version = getDefaultGitBranch(url) + except CatchableError: + raiseError "Unable to determine default branch for module '$#'" % [name] + if global: + dir = MMM.globalDir / name / version + else: + dir = MMM.localDir / name / version + if not fileExists(pwd / "mmm.json"): + raiseError "mmm.json not found in current directory. Please run min init to initialize your managed module." + if dir.dirExists(): + raiseAlreadyInstalledError "Module '$#' (version: $#) is already installed." % [name, version] + dir.createDir() let cmd = "git clone $# -b $# --depth 1 \"$#\"" % [url, version, dir.replace("\\", "/")] debug cmd notice "Installing module $#@$#..." % [name, version]@@ -219,7 +245,8 @@
proc install*(MMM: var MinModuleManager, nameAndVersion: string, global = false) = let parts = nameAndVersion.split("@") if parts.len != 2: - raiseError "Invalid module name and version: $#. Expected: <module-name>@<module-version>" % [nameAndVersion] + MMM.install nameAndVersion, "", global + return let name = parts[0] let version = parts[1] MMM.install name, version, global@@ -251,8 +278,15 @@ warn "Rollback failed."
finally: raiseError "Installation failed." -proc update*(MMM: var MinModuleManager, name, version: string, global = false) = +proc update*(MMM: var MinModuleManager, name, v: string, global = false) = + var version = v var dir: string + if version == "": + try: + let url = MMM.getModuleByName(name)["url"].getStr + version = getDefaultGitBranch(url) + except CatchableError: + raiseError "Unable to determine default branch for module '$#'" % [name] if global: dir = MMM.globalDir / name / version else:@@ -298,7 +332,8 @@
proc update*(MMM: var MinModuleManager, nameAndVersion: string, global = false) = let parts = nameAndVersion.split("@") if parts.len != 2: - raiseError "Invalid module name and version: $#. Expected: <module-name>@<module-version>" % [nameAndVersion] + MMM.update nameAndVersion, "", global + return let name = parts[0] let version = parts[1] MMM.update name, version, global
M
next-release.md
→
next-release.md
@@ -5,7 +5,8 @@
### New Features - Upgraded OpenSSL to v3.2.0 -- It is now possible to install, uninstall, and update modules by specifying them via `<name>@<version>`. +- mmm: It is now possible to install, uninstall, and update modules by specifying them via `<name>@<version>`. +- mmm: The version is now optional when installing, uninstalling, and updating modules (the name of the HEAD branch will be used, e.g. "master" or "main", typically). ### Fixes and Improvements
M
site/contents/learn-mmm.md
→
site/contents/learn-mmm.md
@@ -46,6 +46,8 @@ ### min install [name version | name@version] [-g]
Install the specified managed module by specifying its name and version. By default, the module is installed in the `mmm/<name>/<version>` folder; if `-g` is specified, it is installed in `$HOME/mmm/<name>/<version>`. +If no version is specified, the version will be set to the HEAD branch of the git repository of the module. + If no name and no version are specified, all the managed modules (and their dependencies) specified as dependencies for the current managed module will be installed. If the installation of one dependency fails, the installation of the module will be rolled back.@@ -54,6 +56,8 @@ ### min uninstall [name version | name@version] [-g]
Uninstall the module specified by name and version either locally or globally (if `-g` is specified). +If no version is specified, the version will be set to the HEAD branch of the git repository of the module. + If no version is specified, all version of the module will be uninstalled (if the module is installed globally). If no name and no version are specified, all dependencies of the current managed module (and their dependencies) will be uninstalled.@@ -61,6 +65,8 @@
### min update [name version | name@version] [-g] Update the module specified by name and version either locally or globally (if `-g` is specified). + +If no version is specified, the version will be set to the HEAD branch of the git repository of the module. If no name and no version are specified, all dependencies of the current managed module (and their dependencies) will be updated.