Implemented install/uninstall without arguments.
h3rald h3rald@h3rald.com
Thu, 30 Nov 2023 11:31:21 +0100
3 files changed,
70 insertions(+),
41 deletions(-)
M
min.nim
→
min.nim
@@ -133,6 +133,16 @@ elif fileExists(file&".mn"):
return file&".mn" return "" + proc executeMmmCmd(cmd: proc (): void) = + try: + MMM.setup() + cmd() + quit(0) + except CatchableError: + error getCurrentExceptionMsg() + debug getCurrentException().getStackTrace() + quit(10) + let usage* = """ $exe v$version - a small but practical concatenative programming language (c) 2014-$year Fabio Cevasco@@ -147,8 +157,8 @@ compile <file>.min Compile <file>.min.
eval <string> Evaluate <string> as a min program. help <symbol|sigil> Print the help contents related to <symbol|sigil>. init Sets up the current directory as a managed min module. - install <module> <version> Install the specified managed min module. - uninstall <module> [<version>] Uninstall the specified managed min module. + install [<module> <version>] Install the specified managed min module. + uninstall [<module> <version>] Uninstall the specified managed min module. Options: -a, --asset-path Specify a directory containing the asset files to include in the compiled executable (if -c is set)@@ -252,47 +262,24 @@ quit(9)
minStr("\"$#\" help" % [args[1]]) quit(0) elif file == "init": - try: - MMM.setup() - MMM.init() - quit(0) - except CatchableError: - error getCurrentExceptionMsg() - quit(10) + executeMmmCmd(proc () = MMM.init()) elif file == "install": if args.len < 2: - logging.error "Module name not specified." - quit(10) + executeMmmCmd(proc () = MMM.install()) if args.len < 3: logging.error "Module version not specified." - debug getCurrentException().getStackTrace() quit(11) let name = args[1] let version = args[2] - try: - MMM.setup() - MMM.install(name, version, GLOBAL) - quit(0) - except CatchableError: - error getCurrentExceptionMsg() - debug getCurrentException().getStackTrace() - quit(10) + executeMmmCmd(proc () = MMM.install(name, version, GLOBAL)) elif file == "uninstall": if args.len < 2: - logging.error "Module name not specified." - quit(10) + executeMmmCmd(proc () = MMM.uninstall()) let name = args[1] var version = "" if args.len > 2: version = args[2] - try: - MMM.setup() - MMM.uninstall(name, version, GLOBAL) - quit(0) - except CatchableError: - error getCurrentExceptionMsg() - debug getCurrentException().getStackTrace() - quit(10) + executeMmmCmd(proc () = MMM.uninstall(name, version, GLOBAL)) elif file == "update": logging.error "[update] Not implemented." quit(100)
M
minpkg/core/mmm.nim
→
minpkg/core/mmm.nim
@@ -18,10 +18,15 @@ modules = %[]
globalDir: string localDir: string MMMError = ref object of CatchableError + MMMAlreadyInstalledError = ref object of MMMError proc raiseError(msg: string) = raise MMMError(msg: msg) + +proc raiseAlreadyInstalledError(msg: string) = + raise MMMAlreadyInstalledError(msg: msg) + proc setup*(MMM: var MinModuleManager, check = true) = MMM.registry = MMMREGISTRY@@ -76,11 +81,10 @@ debug "Creating mmm.json file"
let json = """ { "name": "$#", - "method": "git", - "url": "TBD", "author": "TBD", "description": "TBD", "license": "MIT", + "private": true, "deps": {} } """ % [pwd.lastPathPart]@@ -88,6 +92,7 @@ writeFile(pwd / "mmm.json", json)
if not dirExists(pwd / "mmm"): 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) = var dir: string@@ -116,6 +121,14 @@ debug getCurrentExceptionMsg()
raiseError "Unable to uninstall module $#@$#" % [name, versionLabel] notice "Uninstall complete." +proc uninstall*(MMM: var MinModuleManager) = + try: + notice "Uninstalling all local managed modules..." + MMM.localDir.removeDir() + notice "Done." + except CatchableError: + raiseError "Unable to uninstall local managed modules." + proc install*(MMM: var MinModuleManager, name, version: string, global = false) = var dir: string if global:@@ -123,7 +136,7 @@ dir = MMM.globalDir / name / version
else: dir = MMM.localDir / name / version if dir.dirExists(): - raiseError "Module '$#' (version: $#) is already installed." % [name, version] + 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:@@ -170,6 +183,35 @@ debug getCurrentExceptionMsg()
warn "Rollback failed." finally: raiseError "Installation failed." + +proc install*(MMM: var MinModuleManager) = + let mmmJson = getCurrentDir() / "mmm.json" + if not mmmJson.fileExists: + raiseError "No mmm.json file found in the current directory." + let data = mmmJson.parseFile + if not data.hasKey "deps": + raiseError "No 'deps' key present in the mmm.json file." + for name, v in data["deps"].pairs: + let version = v.getStr + try: + MMM.install name, version + except MMMAlreadyInstalledError: + warn getCurrentExceptionMsg() + continue + except CatchableError: + debug getCurrentExceptionMsg() + warn "Installation of '$#@$#' failed - Rolling back..." % [name, version] + try: + MMM.setup(false) + MMM.uninstall(name, version) + notice "Rollback completed." + except: + debug getCurrentExceptionMsg() + warn "Rollback failed." + finally: + raiseError "Installation failed." + +
M
site/mmm.json
→
site/mmm.json
@@ -1,10 +1,10 @@
{ - "name": "site", - "method": "git", - "url": "", - "author": "", - "description": "", - "license": "", - "deps": {} + "name": "min-site", + "author": "Fabio Cevasco", + "description": "The web site of the min programming language.", + "license": "MIT", + "private": true, + "deps": { + "min-highlight": "master" + } } -