all repos — nifty @ 70d337f0a138dcc86fd37b5d6c9f6471c4b833f8

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
import 
  json,
  os,
  parseopt2,
  logging,
  strutils,
  sequtils

import
  lib/niftylogger

newNiftyLogger().addHandler()
setLogFilter(lvlInfo)

import
  lib/config,
  lib/project

type
  NiftyOption = object
    key: string
    val: JsonNode

let usage* = """  $1 v$2 - $3
  Commands:
    init [--storage:<dir>]                Initializes a project in the current directory.
    map <package> [--<property>:<value>]  Defines <package> with the specified properties.
    unmap <package>                       Unmaps a previously-mapped package <package>.
    remove <package>                      Removes <storage-dir>/<package> directory
                                          and all its contents.
    <command> [<package>]                 Executes <command> (on <package>).
  Options:
    --log, -l               Specifies the log level (default: info).
    --help, -h              Displays this message.
    --version, -h           Displays the version of the application.
    --storage, -s           Specifies what directory to use for storing packages.
""" % [appname, version, appdesc]

var storage = "packages"

var args = newSeq[string](0)
var opts = newSeq[NiftyOption](0)

proc `%`(opts: seq[NiftyOption]): JsonNode =
  result = newJObject()
  for o in opts:
    result[o.key] = o.val

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)
        of "storage", "s":
          storage = val
        else:
          var v: JsonNode
          if val == "true" or val == "":
            v = %true
          else:
            v = %val
          opts.add NiftyOption(key: key, val: v)
    else:
      discard

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)
    prj.init(storage)
    notice "Project initialized using '$1' as storage directory." % storage
  of "map":
    if args.len < 2:
      fatal "No alias specified."
      quit(3)
    prj.map(args[1], %opts) 
  of "unmap":
    if args.len < 2:
      fatal "No alias specified."
      quit(3)
    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])
  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])