all repos — min @ c117e1e523c2c549128c8ed21e4124433787ee8e

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
import 
    std/[json,
    os,
    httpclient,
    strutils,
    logging
]
import
    env



type
    MinModuleManager* = object
        registry: string
        modules = %[]
        globalDir: string
        localDir: string


proc setup*(MMM: var MinModuleManager) =
    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:
            logging.debug getCurrentExceptionMsg()
            error "Invalid local registry data ($#)" % [mmmJson]
    let client = newHttpClient()
    # Check remote data
    var updatedRemote = 0
    try:
        logging.debug "Checking remote registry..."
        updatedRemote = client.getContent(MMMREGISTRY & "/mmm.timestamp").parseInt
        logging.debug "Remote registry timestamp retrieved."
    except CatchableError:
        logging.debug getCurrentExceptionMsg()
        logging.warn "Unable to connect to remote registry ($#)" % [MMMREGISTRY]
    if updatedRemote > updatedLocal:
        logging.notice "Updating local module registry..."
        try:
            client.downloadFile(MMMREGISTRY & "/mmm.json", mmmJson)
        except CatchableError:
            logging.debug getCurrentExceptionMsg()
            logging.warn "Unable to download remote registry data ($#)" % [MMMREGISTRY & "/mmm.json"]
        try:
            let data = parseFile(mmmJson)
            MMM.modules = data["modules"]
        except CatchableError:
            logging.debug getCurrentExceptionMsg()
            error "Invalid local registry data ($#)" % [mmmJson]
    else:
        logging.debug "Local registry up-to-date: $#" % [$updatedLocal] 

proc init*(MMM: var MinModuleManager) =
    let pwd = getCurrentDir()
    if dirExists(pwd / "mmm.json"):
        error "The current directory already contains a managed module (mmm.json already exists)"
    logging.debug "Creating mmm.json file"
    let json = """
{
    "name": "$#",
    "method": "git",
    "url": "",
    "author": "",
    "description": "",
    "license": "",
    "deps": {}
}
    """ % [pwd.lastPathPart]
    writeFile(pwd / "mmm.json", json)
    if not dirExists(pwd / "mmm"):
        logging.debug "Creating mmm directory"
        createDir(pwd / "mmm")