all repos — nifty @ 54b9c0c697439168d3e10d641df842c6ecf530bd

A tiny (pseudo) package manager and script runner.

lib/project.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
import
  os,
  json,
  logging,
  strutils

type
  NiftyProject* = object
    dir: string
    storage: string
    commands: JsonNode
    packages: JsonNode


proc newNiftyProject*(dir: string): NiftyProject =
  result.dir = dir

proc configFile*(prj: NiftyProject): string = 
  return prj.dir/"nifty.json"

proc configured*(prj: NiftyProject): bool =
  return fileExists(prj.configFile)

proc init*(prj: var NiftyProject, storage: string) =
  prj.storage = storage
  createDir(prj.dir/prj.storage)
  var o = newJObject()
  o["storage"] = %prj.storage
  o["commands"] = newJObject()
  o["commands"]["install"] = newJObject()
  o["commands"]["install"]["git+src"] = %"git clone {{src}} --depth 1"
  o["commands"]["install"]["git+src+tag"] = %"git clone --branch {{tag}} {{src}} --depth 1"
  o["commands"]["update"] = newJObject()
  o["commands"]["update"]["git+name"] = %"cd {{name}} && git pull; cd .."
  o["packages"] = newJObject()
  prj.configFile.writeFile(o.pretty)

proc load*(prj: var NiftyProject) =
  let cfg = prj.configFile.parseFile
  prj.commands = cfg["commands"]
  prj.packages = cfg["packages"]

proc save*(prj: NiftyProject) = 
  var o = newJObject()
  o["storage"] = %prj.storage
  o["commands"] = %prj.commands
  o["packages"] = %prj.packages
  prj.configFile.writeFile(o.pretty)

proc map*(prj: var NiftyProject, alias: string, props: JsonNode) =
  prj.load
  if not prj.packages.hasKey alias:
    notice "Adding package definition '$1'..." % alias
    prj.packages[alias] = newJObject()
    prj.packages[alias]["name"] = %alias
  else:
    notice "Updating package definition '$1'..." % alias
  for key, val in props.pairs:
    prj.packages[alias][key] = val
    notice "  $1 = $2" % [key, $val]
  prj.save
  notice "Package definition '$1' saved." % alias

proc unmap*(prj: var NiftyProject, alias: string) =
  prj.load
  if not prj.packages.hasKey alias:
    warn "Package definition '$1' not found. Nothing to do." % alias
    return
  prj.packages.delete(alias)
  prj.save
  notice "Package definition '$1' removed." % alias