all repos — nifty @ 6c8ae664ad9f238c68aec9b07d1ff057268292d1

A tiny (pseudo) package manager and script runner.

Implemented dynamic help system.
h3rald h3rald@h3rald.com
Sun, 18 Mar 2018 11:55:23 +0100
commit

6c8ae664ad9f238c68aec9b07d1ff057268292d1

parent

997ddf6badec286bee2de87c6bd0408a707e21f4

3 files changed, 85 insertions(+), 16 deletions(-)

jump to
A lib/help.json

@@ -0,0 +1,37 @@

+{ + "help": + { + "_syntax": "help [<command>]", + "_description": "Display help on the specified command (or all commands)." + }, + "info": + { + "_syntax": "info <package>", + "_description": "Displays information on <package>" + }, + "init": + { + "_syntax": "init [<storage-dir>]", + "_description": "Initializes a project in the current directory (using <storage-dir> as storage directory)." + }, + "list": + { + "_syntax": "list", + "_description": "Lists all dependencies (recursively) of the current project." + }, + "map": + { + "_syntax": "map <package> [--<property>_<value>]", + "_description": "Defines <package> with the specified properties." + }, + "remove": + { + "_syntax": "remove [<package>]", + "_description": "Removes the specified package (or all packages) from the storage directory." + }, + "unmap": + { + "_syntax": "unmap <package>", + "_description": "Unmaps the previously-mapped package <package>." + } +}
M lib/project.nimlib/project.nim

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

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

@@ -42,6 +42,17 @@ prj.storage = cfg["storage"].getStr

prj.storage.createDir() prj.commands = cfg["commands"] prj.packages = cfg["packages"] + +proc help*(prj: 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]) proc save*(prj: NiftyProject) = var o = newJObject()
M nifty.nimnifty.nim

@@ -31,21 +31,18 @@

let usage* = """ $1 v$2 - $3 (c) 2017-2018 Fabio Cevasco - 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>). + + Usage: + nifty <command> [<package>] Executes <command> (on <package>). + + For more information on available commands, run: nifty help + Options: - --log, -l Specifies the log level (default: info). + --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. - --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)

@@ -90,8 +87,6 @@ quit(0)

of "version", "v": echo version quit(0) - of "storage", "s": - storage = val else: var v: JsonNode if val == "true" or val == "":

@@ -122,16 +117,19 @@ 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 alias specified." + fatal "No package alias specified." quit(3) prj.map(args[1], %opts) of "unmap": if args.len < 2: - fatal "No alias specified." + fatal "No package alias specified." quit(3) prj.unmap(args[1]) of "remove":

@@ -151,6 +149,29 @@ 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] else: if args.len < 1: echo usage