all repos — nifty @ 7ee429d775f305e578d86f1c29fd2c86650f5f06

A tiny (pseudo) package manager and script runner.

nifty.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
import 
  json,
  os,
  ospaths,
  parseopt,
  logging,
  strutils,
  terminal,
  pegs,
  sequtils

import
  lib/niftylogger

newNiftyLogger().addHandler()
setLogFilter(lvlInfo)

import
  lib/config,
  lib/project

when defined(windows):
   proc putchr*(c: cint): cint {.discardable, header: "<conio.h>", importc: "_putch".}
else:
  proc putchr*(c: cint) =
    stdout.write(c.chr)

proc confirm(q: string): bool =
  stdout.setForegroundColor(fgYellow)
  stdout.write("(!) " & q & " [y/n]: ")
  resetAttributes()
  let answer = stdin.readLine
  if answer.match(peg"^ i'y' / i'yes' $"):
    return true
  return false

proc addProperty(): tuple[key: string, value: JsonNode] =
  var done = false
  while (not done):
    stdout.setForegroundColor(fgBlue)
    stdout.write("    -> Name: ")
    resetAttributes()
    result.key = stdin.readLine
    stdout.setForegroundColor(fgBlue)
    stdout.write("    -> Value: ")
    resetAttributes()
    try:
      result.value = %*stdin.readLine
    except:
      warn("Invalid value; please enter a valid JSON value.")
    done = confirm("OK?")

proc addProperties(obj: var JsonNode) =
  var done = false
  while (not done):
    let prop = addProperty()
    obj[prop.key] = prop.value
    done = not confirm("Do you want to add more properties?")

proc changeValue(oldv: tuple[label: string, value: JsonNode], newv: tuple[label: string, value: JsonNode]): bool =
  if oldv.value != newJNull():
    stdout.setForegroundColor(fgRed)
    stdout.write("--- ")
    resetAttributes()
    echo oldv.label & ": " & $oldv.value
  if newv.value != newJNull():
    stdout.setForegroundColor(fgGreen)
    stdout.write("+++ ")
    resetAttributes()
    echo newv.label & ": " & $newv.value 
  return confirm("Confirm change?")

let usage* = """  $1 v$2 - $3
  (c) 2017-2018 Fabio Cevasco

  Usage:
    nifty <command> [<package>]           Executes <command> (on <package>).

    For more information on available commands, run: nifty help

  Options:
    --log, -l               Specifies the log level (debug|info|notice|warn|error|fatal).
                            Default: info
    --help, -h              Displays this message.
    --version, -h           Displays the version of the application.
""" % [appname, version, appdesc]

var args = newSeq[string](0)

proc confirmAndRemoveDir(dir: string) =
  warn "Delete directory '$1' and all its contents? [y/n]" % dir
  let answer = stdin.readLine.toLowerAscii[0]
  if answer == 'y':
    dir.removeDir()

proc confirmAndRemoveFile(file: string) =
  warn "Delete file '$1'? [y/n]" % file 
  let answer = stdin.readLine.toLowerAscii[0]
  if answer == 'y':
    file.removeFile()

proc confirmAndRemovePackage(pkg: string) =
  if pkg.fileExists():
    pkg.confirmAndRemoveFile()
  elif pkg.dirExists():
    pkg.confirmAndRemoveDir()
  else:
    warn "Package '$1' not found." % pkg

for kind, key, val in getopt():
  case kind:
    of cmdArgument:
      args.add key 
    of cmdLongOption, cmdShortOption:
      case key:
        of "log", "l":
          var val = val
          setLogLevel(val)
        of "help", "h":
          echo usage
          quit(0)
        of "version", "v":
          echo version
          quit(0)
        else:
          discard
    else:
      discard

proc walkPkgs(prj: NiftyProject, dir: string, level = 1) =
  for k, v in prj.packages.pairs:
    echo " ".repeat(level*2) &  "-" & " " & k
    var d = dir / prj.storage / k
    var p = newNiftyProject(d)
    if p.configured:
      p.load
      walkPkgs(p, d, level+1)

proc updateDefinitions(prj: var NiftyProject): bool =
  result = false
  let sysCommands = niftyTpl.parseJson["commands"]
  for k, v in sysCommands.pairs:
    if prj.commands.hasKey(k):
      let sysCommand = sysCommands[k]
      var prjCommand = prj.commands[k]
      for prop, val in sysCommand.pairs:
        let sysProp = sysCommand[prop]
        var prjProp = newJNull()
        if prjCommand.hasKey(prop):
          prjProp = prjCommand[prop]
        if prjProp != newJNull():
          if prjProp != sysProp:
            let sysVal = (label: k & "." & prop, value: sysProp)
            let prjVal = (label: k & "." & prop, value: prjProp)
            if changeValue(prjVal, sysVal):
              prjCommand[prop] = sysProp
              result = true
        else:
          result = true
          # Adding new property
          stdout.setForegroundColor(fgGreen)
          stdout.write("+++ ")
          resetAttributes()
          echo "$1.$2: $3" % [k, prop, $sysProp] 
          prjCommand[prop] = sysProp
    else:
      result = true
      # Adding new command
      stdout.setForegroundColor(fgGreen)
      stdout.write("+++ ")
      resetAttributes()
      echo "$1: $2" % [k, $sysCommands[k]] 
      prj.commands[k] = sysCommands[k]

var prj = newNiftyProject(getCurrentDir())

if args.len == 0:
  echo usage
  quit(0)
case args[0]:
  of "init":
    if prj.configured:
      fatal "Project already configured."
      quit(2)
    var storage = "packages"
    if args.len > 2:
      storage = args[1]
    prj.init(storage)
    notice "Project initialized using '$1' as storage directory." % storage
  of "map":
    if args.len < 2:
      fatal "No package alias specified."
      quit(3)
    notice "Mapping package alias: " & args[1] 
    warn "Specify properties for alias '$1':" % [args[1]]
    var props = newJObject()
    addProperties(props)
    prj.map(args[1], %props) 
  of "unmap":
    if args.len < 2:
      fatal "No package alias specified."
      quit(3)
    if confirm("Remove mapping for package alias '$1'?" % args[1]):
      prj.unmap(args[1]) 
  of "remove":
    prj.load
    if args.len < 2:
      var packages = toSeq(prj.packages.pairs)
      if packages.len == 0:
        warn "No packages defined - nothing to do."
      else:
        for key, val in prj.packages.pairs:
          confirmAndRemovePackage(prj.storage/key)
    else:
      confirmAndRemovePackage(prj.storage/args[1])
  of "list":
    prj.load
    let pwd = getCurrentDir()
    let parts = pwd.split(DirSep)
    echo parts[parts.len-1]
    walkPkgs(prj, pwd)
  of "info":
    if args.len < 2:
      fatal "No package alias specified."
      quit(3)
    prj.load
    let alias = args[1]
    if not prj.packages.hasKey(alias):
      fatal "Package alias '$1' not defined." % [alias]
      quit(4)
    let data = prj.packages[alias]
    for k, v in data.pairs:
      echo "$1:\t$2" % [k, $v]
  of "help":
    prj.load
    if args.len < 2:
      for k, v in prj.help.pairs:
        echo "nifty $1\n    $2" % [v["_syntax"].getStr, v["_description"].getStr]
    else:
      let cmd = args[1]
      if not prj.help.hasKey(cmd):
        fatal "Command '$1' is not defined." % cmd
        quit(5)
      echo "nifty $1\n    $2" % [prj.help[cmd]["_syntax"].getStr, prj.help[cmd]["_description"].getStr]
  of "update-definitions":
    prj.load
    if updateDefinitions(prj):
      prj.save
  else:
    if args.len < 1:
      echo usage
      quit(1)
    if args.len < 2:
      prj.load
      var packages = toSeq(prj.packages.pairs)
      if packages.len == 0:
        warn "No packages defined - nothing to do."
      else:
        for key, val in prj.packages.pairs:
          prj.executeRec(args[0], key) 
    else:
      prj.executeRec(args[0], args[1])