all repos — nifty @ 3f99e7d4d3313becce1080bb25efc5d48593cfcb

A tiny (pseudo) package manager and script runner.

Improvements.
h3rald h3rald@h3rald.com
Sun, 18 Mar 2018 15:29:00 +0100
commit

3f99e7d4d3313becce1080bb25efc5d48593cfcb

parent

fba80408ca1898eed66e82b132b33fa8e9d95b74

4 files changed, 77 insertions(+), 42 deletions(-)

jump to
M lib/help.jsonlib/help.json

@@ -21,8 +21,8 @@ "_description": "Lists all dependencies (recursively) of the current project."

}, "map": { - "_syntax": "map <package> [--<property>_<value>]", - "_description": "Defines <package> with the specified properties." + "_syntax": "map <package>", + "_description": "Configures a new or existing package <package>." }, "remove": {

@@ -33,5 +33,10 @@ "unmap":

{ "_syntax": "unmap <package>", "_description": "Unmaps the previously-mapped package <package>." + }, + "update-commands": + { + "_syntax": "update-commands", + "_description": "Updates the command definitions for the current project." } }
M lib/project.nimlib/project.nim

@@ -43,16 +43,18 @@ prj.storage.createDir()

prj.commands = cfg["commands"] prj.packages = cfg["packages"] -proc help*(prj: NiftyProject): JsonNode = +proc help*(prj: var NiftyProject): JsonNode = result = systemHelp.parseJson - for k, v in prj.commands.pairs: - if v.hasKey("_syntax") and v.hasKey("_description"): - result[k] = %*(""" - { - "_syntax": "$1", - "_description": "$2" - } - """ % [v["_syntax"].getStr, v["_description"].getStr]) + if prj.configured: + prj.load + for k, v in prj.commands.pairs: + if v.hasKey("_syntax") and v.hasKey("_description"): + result[k] = (""" + { + "_syntax": "$1", + "_description": "$2" + } + """ % [v["_syntax"].getStr, v["_description"].getStr]).parseJson proc save*(prj: NiftyProject) = var o = newJObject()

@@ -61,28 +63,29 @@ o["commands"] = %prj.commands

o["packages"] = %prj.packages prj.configFile.writeFile(o.pretty) -proc map*(prj: var NiftyProject, alias: string, props: JsonNode) = +proc map*(prj: var NiftyProject, alias: string, props: var JsonNode) = + for k, v in props.mpairs: + if v == newJNull(): + props.delete(k) prj.load if not prj.packages.hasKey alias: - notice "Adding package definition '$1'..." % alias + notice "Adding package mapping '$1'..." % alias prj.packages[alias] = newJObject() prj.packages[alias]["name"] = %alias else: - notice "Updating package definition '$1'..." % alias + notice "Updating package mapping '$1'..." % alias + prj.packages[alias] = newJObject() for key, val in props.pairs: prj.packages[alias][key] = val notice " $1: $2" % [key, $val] prj.save - notice "Package definition '$1' saved." % alias + notice "Package mapping '$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 + notice "Package mapping '$1' removed." % alias proc lookupCommand(prj: NiftyProject, command: string, props: seq[string], cmd: var JsonNode): bool = if not prj.commands.hasKey command:

@@ -92,6 +95,8 @@ var cmds = prj.commands[command]

var score = 0 # Cycle through command definitions for key, val in cmds: + if key == "_syntax" or key == "_description": + continue var params = key.split("+") # Check if all params are available var match = params.all do (x: string) -> bool:
M nifty.nimnifty.nim

@@ -28,13 +28,21 @@ if answer.match(peg"^ i'y' / i'yes' $"):

return true return false -proc addProperty(parentObj: JsonNode): tuple[key: string, value: JsonNode] = +proc addProperty(parentObj: JsonNode, name = ""): tuple[key: string, value: JsonNode] = var done = false while (not done): - stdout.setForegroundColor(fgBlue) - stdout.write(" -> Name: ") - resetAttributes() - result.key = stdin.readLine + if name == "": + stdout.setForegroundColor(fgBlue) + stdout.write(" -> Name: ") + resetAttributes() + result.key = stdin.readLine + elif name == "name": + warn "Property identifier 'name' cannot be modified." + else: + stdout.setForegroundColor(fgBlue) + echo " -> Name: " & name + resetAttributes() + result.key = name var ok = false while (not ok): var label = " -> Value: "

@@ -61,9 +69,6 @@ while (not done):

let prop = addProperty(obj) obj[prop.key] = prop.value done = not confirm("Do you want to add/remove more properties?") - for k, v in obj.mpairs: - if v == newJNull(): - obj.delete(k) proc changeValue(oldv: tuple[label: string, value: JsonNode], newv: tuple[label: string, value: JsonNode]): bool = if oldv.value != newJNull():

@@ -197,19 +202,37 @@ prj.init(storage)

notice "Project initialized using '$1' as storage directory." % storage of "map": if args.len < 2: - fatal "No package alias specified." + fatal "No package specified." quit(3) - notice "Mapping package alias: " & args[1] - warn "Specify properties for alias '$1':" % [args[1]] + let alias = args[1] var props = newJObject() - addProperties(props) - prj.map(args[1], %props) + prj.load + if prj.packages.hasKey(alias): + notice "Remapping existing package: " & alias + warn "Specify properties for package '$1':" % alias + props = prj.packages[alias] + for k, v in props.mpairs: + if k == "name": + continue + let prop = addProperty(props, k) + props[prop.key] = prop.value + if confirm "Do you want to add/remove more properties?": + addProperties(props) + else: + notice "Mapping new package: " & alias + warn "Specify properties for package '$1':" % alias + addProperties(props) + prj.map(alias, props) of "unmap": if args.len < 2: - fatal "No package alias specified." + fatal "No package specified." quit(3) - if confirm("Remove mapping for package alias '$1'?" % args[1]): - prj.unmap(args[1]) + let alias = args[1] + if not prj.packages.hasKey(alias): + fatal "Package '$1' not defined." % [alias] + quit(4) + if confirm("Remove mapping for package '$1'?" % alias): + prj.unmap(alias) of "remove": prj.load if args.len < 2:

@@ -229,28 +252,30 @@ echo parts[parts.len-1]

walkPkgs(prj, pwd) of "info": if args.len < 2: - fatal "No package alias specified." + fatal "No package specified." quit(3) prj.load let alias = args[1] if not prj.packages.hasKey(alias): - fatal "Package alias '$1' not defined." % [alias] + fatal "Package '$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] + stdout.setForegroundColor(fgGreen) + echo "nifty $1" % v["_syntax"].getStr + resetAttributes() + echo " $1" % 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": + of "update-commands": prj.load if updateDefinitions(prj): prj.save
M nifty.nimblenifty.nimble

@@ -1,10 +1,10 @@

[Package] name = "nifty" -version = "0.1.0" +version = "1.0.0" author = "Fabio Cevasco" description = "A pseudo package manager and script runner." license = "MIT" bin = "nifty" [Deps] -requires: "nim >= 0.16.2" +requires: "nim >= 0.18.0"