all repos — min @ 3ec4aaa348867a52111e3efc64bf2ef0adf6c2a9

A small but practical concatenative programming language.

minpkg/core/mmm.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
import 
    std/[json,
    os,
    osproc,
    httpclient,
    strutils,
    sequtils,
    logging,
    algorithm
]
import
    env



type
    MinModuleManager* = object
        registry: string
        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 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
    MMM.globalDir = HOME / "mmm"
    MMM.localDir = getCurrentDir() / "mmm"
    var updatedLocal = 0
    let mmmJson = MMM.globalDir / "mmm.json"
    if not dirExists(MMM.globalDir):
        createDir(MMM.globalDir)
    if not dirExists(MMM.localDir):
        createDir(MMM.localDir)
    if fileExists(mmmJson):
        try:
            let data = parseFile(mmmJson)
            updatedLocal = data["updated"].getInt
            MMM.modules = data["modules"]
        except CatchableError:
            debug getCurrentExceptionMsg()
            raiseError "Invalid local registry data ($#)" % [mmmJson]
    if check:
        let client = newHttpClient()
        # Check remote data
        var updatedRemote = 0
        try:
            debug "Checking remote registry"
            updatedRemote = client.getContent(MMMREGISTRY & "/mmm.timestamp").parseInt
            debug "Remote registry timestamp retrieved."
        except CatchableError:
            debug getCurrentExceptionMsg()
            warn "Unable to connect to remote registry ($#)" % [MMMREGISTRY]
        if updatedRemote > updatedLocal:
            notice "Updating local module registry"
            try:
                client.downloadFile(MMMREGISTRY & "/mmm.json", mmmJson)
            except CatchableError:
                warn "Unable to download remote registry data ($#)" % [MMMREGISTRY & "/mmm.json"]
                debug getCurrentExceptionMsg()
            try:
                let data = parseFile(mmmJson)
                MMM.modules = data["modules"]
            except CatchableError:
                debug getCurrentExceptionMsg()
                raiseError "Invalid local registry data ($#)" % [mmmJson]
        else:
            debug "Local registry up-to-date: $#" % [$updatedLocal] 

proc init*(MMM: var MinModuleManager) =
    let pwd = getCurrentDir()
    if fileExists(pwd / "mmm.json"):
        raiseError "The current directory already contains a managed module (mmm.json already exists)"
    debug "Creating mmm.json file"
    let json = """
{
    "name": "$#",
    "author": "TBD",
    "description": "TBD",
    "license": "MIT",
    "private": true,
    "deps": {}
}
    """ % [pwd.lastPathPart]
    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, v: string, global = false) =
    var dir: string
    var version = v
    var versionLabel = 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."
    if not dir.dirExists():
        raiseError "Module '$#' (version: $#) is not installed." % [name, versionLabel]
    notice "Uninstalling module $#@$#..." % [name, versionLabel]
    try:
        dir.removeDir()
        if version != "" and dir.parentDir().walkDir().toSeq().len == 0:
            # Remove parent directory if no versions are installed
            dir.parentDir().removeDir()
    except CatchableError:
        debug getCurrentExceptionMsg()
        raiseError "Unable to uninstall module $#@$#" % [name, versionLabel]
    if not global:
        let mmmJson = pwd/"mmm.json"
        var data = mmmJson.parseFile
        if data["deps"].hasKey name:
            data["deps"].delete(name)
        mmmJson.writeFile(data.pretty)
    notice "Uninstall complete."

proc uninstall*(MMM: var MinModuleManager, nameAndVersion: string, global = false) =
    let   parts = nameAndVersion.split("@")
    if parts.len != 2:
        MMM.uninstall nameAndVersion, "", global
        return
    let name = parts[0]
    let version = parts[1]
    MMM.uninstall name, version, global

proc uninstall*(MMM: var MinModuleManager) =
    let pwd = getCurrentDir()
    if not fileExists(pwd / "mmm.json"):
        raiseError "mmm.json not found in current directory. Please run min init to initialize your managed module."
    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, v: string, global = false) =
    var version = v
    var dir: string
    let pwd = getCurrentDir()
    let results = MMM.modules.filterIt(it.hasKey("name") and it["name"] == %name)
    if results.len == 0:
        raiseError "Unknown module '$#'." % [name]
    let data = results[0]
    if not data.hasKey("method"):
        raiseError "Installation method not specified for module '$#'" % [name]
    let meth = data["method"].getStr 
    if meth != "git":
        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]
    if not data.hasKey("deps"):
        raiseError "Dependencies not specified for module '$#'" % [name]
    var result = execShellCmd(cmd)
    if result == 0:
        # Go to directory and install dependencies
        let originalDir = getCurrentDir()
        dir.setCurrentDir()
        MMM.setup(false)
        if data["deps"].pairs.toSeq().len > 0:
            notice "Installing dependencies..."
        for depName, depVersion in data["deps"].pairs:
            try:
                MMM.install depName, depVersion.getStr, global
            except CatchableError:
                warn getCurrentExceptionMsg()
                originalDir.setCurrentDir()
                result = 1
                break
    # re-check if dependency installation failed.
    if result == 0:
            if not global:
                # Add dependency to current dir's mmm.json file.
                let mmmJson = dir/"mmm.json"
                var data = mmmJson.parseFile
                data["deps"][name] = %($version)
                mmmJson.writeFile(data.pretty)
    else:
        # Rollback
        warn "Installation failed - Rolling back..."
        try:
            MMM.setup(false)
            MMM.uninstall(name, version, global)
            notice "Rollback completed."
        except:
            debug getCurrentExceptionMsg()
            warn "Rollback failed."
        finally:
            raiseError "Installation failed."

proc install*(MMM: var MinModuleManager, nameAndVersion: string, global = false) =
    let   parts = nameAndVersion.split("@")
    if parts.len != 2:
        MMM.install nameAndVersion, "", global
        return
    let name = parts[0]
    let version = parts[1]
    MMM.install name, version, global

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 in the current directory."
    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 module $#@$# 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."

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:
        dir = MMM.localDir / name / version
    if not dir.dirExists():
        raiseError "Module '$#' (version: $#) is not installed." % [name, version]
    # Read local mmm.json
    let mmmJson = dir / "mmm.json"
    if not mmmJson.fileExists:
        raiseError "No mmm.json file found for managed module $#@$#" % [name, version]
    var data: JsonNode
    try:
        data = mmmJson.parseFile
    except CatchableError:
        raiseError "Unable to parse mmm.json file for managed module $#@$#" % [name, version]
    if not data.hasKey("method"):
        raiseError "Installation method not specified for module '$#'" % [name]
    let meth = data["method"].getStr 
    if meth != "git":
        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
    let cmd = "git -C \"$#\" pull" % [dir.replace("\\", "/"), url, version]
    debug cmd
    notice "Updating module $#@$#..." % [name, version]
    if not data.hasKey("deps"):
        raiseError "Dependencies not specified for module '$#'" % [name]
    var result = execShellCmd(cmd)
    if result == 0:
        # Go to directory and install dependencies
        dir.setCurrentDir()
        MMM.setup(false)
        if data["deps"].pairs.toSeq().len > 0:
            notice "Updating dependencies..."
        for depName, depVersion in data["deps"].pairs:
            try:
                MMM.update depName, depVersion.getStr, global
            except CatchableError:
                warn getCurrentExceptionMsg()
                raiseError "Update of module '$#@$#' failed." % [name, version]

proc update*(MMM: var MinModuleManager, nameAndVersion: string, global = false) =
    let   parts = nameAndVersion.split("@")
    if parts.len != 2:
        MMM.update nameAndVersion, "", global
        return
    let name = parts[0]
    let version = parts[1]
    MMM.update name, version, global

proc update*(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 in the current directory."
    for name, v in data["deps"].pairs:
        let version = v.getStr
        try:
            MMM.update name, version
        except CatchableError:
            debug getCurrentExceptionMsg()
            warn "Update of module '$#@$#' failed." % [name, version]

proc search*(MMM: var MinModuleManager, search="") = 
    let rateModule = proc(it: JsonNode): JsonNode =
        var score = 0
        if it["name"].getStr.contains(search):
            score += 4
        if it["description"].getStr.contains(search):
            score += 2
        if it["author"].getStr.contains(search):
            score += 1
        it["score"] = %score
        return it
    let sortModules = proc(x, y: JsonNode): int = 
        cmp(x["score"].getInt, y["score"].getInt)
    let formatDeps = proc(deps: JsonNode): string =
        result = deps.pairs.toSeq().mapIt("$#@$#" % [it.key, it.val.getStr]).join(", ")
        if result == "":
            result = "n/a"
    var results = MMM.modules.getElems().map(rateModule).filterIt(it["score"].getInt > 0)
    results.sort(sortModules)
    var msg = "$# results found:"
    if results.len == 1:
        msg = "$# result found:"
    elif results.len == 0:
        msg = "No results found."
    notice msg % [$results.len]
    for m in results:
       notice "-> $#" % [m["name"].getStr] 
       notice "   Description: $#" % [m["description"].getStr] 
       notice "   Author: $#" % [m["author"].getStr] 
       notice "   License: $#" % [m["license"].getStr] 
       notice "   Dependencies: $#" % [m["deps"].formatDeps] 

proc list*(MMM: var MinModuleManager, dir: string, level = 0) =
    debug "Directory: " & dir
    if not dir.dirExists:
        return
    for name in dir.walkDir:
        debug "Module directory: " & name.path
        if name.kind == pcDir or name.kind == pcLinkToDir:
            for version in (name.path).walkDir:
                debug "Module version directory: " & version.path
                if name.kind == pcDir or name.kind == pcLinkToDir:
                    notice " ".repeat(level) & "$#@$#" % [name.path.lastPathPart, version.path.lastPathPart]
                    MMM.list version.path/"mmm", level+1