all repos — nifty @ cad38c3a15ec76528846a76930a004688cb799a0

A tiny (pseudo) package manager and script runner.

Added update-definitions command.
h3rald h3rald@h3rald.com
Sun, 18 Mar 2018 13:16:38 +0100
commit

cad38c3a15ec76528846a76930a004688cb799a0

parent

6c8ae664ad9f238c68aec9b07d1ff057268292d1

2 files changed, 63 insertions(+), 1 deletions(-)

jump to
M lib/project.nimlib/project.nim

@@ -13,7 +13,7 @@ storage*: string

commands*: JsonNode packages*: JsonNode -const niftyTpl = "nifty.json".slurp +const niftyTpl* = "nifty.json".slurp const systemHelp = "help.json".slurp let placeholder = peg"'{{' {[^}]+} '}}'"
M nifty.nimnifty.nim

@@ -5,6 +5,8 @@ ospaths,

parseopt, logging, strutils, + terminal, + pegs, sequtils import

@@ -28,6 +30,27 @@ 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 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

@@ -106,6 +129,41 @@ 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())

@@ -172,6 +230,10 @@ 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