all repos — nifty @ a08d910d1b8a18aa6729a446472536138cd26798

A tiny (pseudo) package manager and script runner.

Implemented init and map commands.
h3rald h3rald@h3rald.com
Sat, 19 Nov 2016 11:13:43 +0100
commit

a08d910d1b8a18aa6729a446472536138cd26798

parent

c1289bed3ef6273bd5c6b3ccf69ddb1edf46d044

3 files changed, 132 insertions(+), 27 deletions(-)

jump to
M .gitignore.gitignore

@@ -1,3 +1,5 @@

nimcache/ nifty.exe nifty +nifty.json +packages/
A lib/project.nim

@@ -0,0 +1,62 @@

+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
M nifty.nimnifty.nim

@@ -6,34 +6,75 @@ logging,

strutils import - lib/config, lib/logger -when isMainModule: - let usage* = """ $1 v$2 - $3 - """ % [appname, version, appdesc] +newStyledConsoleLogger().addHandler() +setLogFilter(lvlNotice) + +import + lib/config, + lib/project + +type + NiftyOption = object + key: string + val: JsonNode + +let usage* = """ $1 v$2 - $3 +""" % [appname, version, appdesc] + +var command: string +var storage = "packages" + +var args = newSeq[string](0) +var opts = newSeq[NiftyOption](0) - var file, s: string = "" - setLogFilter(lvlNotice) - - for kind, key, val in getopt(): - case kind: - of cmdArgument: - file = key - of cmdLongOption, cmdShortOption: - case key: - of "log", "l": - var val = val - setLogLevel(val) - #of "evaluate", "e": - # s = val - of "help", "h": - echo usage - quit(0) - of "version", "v": - echo version - quit(0) +proc `%`(opts: seq[NiftyOption]): JsonNode = + result = newJObject() + for o in opts: + result[o.key] = o.val + +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: - discard - else: - discard+ v = %val + opts.add NiftyOption(key: key, val: v) + else: + discard + +var prj = newNiftyProject(getCurrentDir()) +case args[0]: + of "init": + if prj.configured: + fatal "Project already configured." + quit(1) + prj.init(storage) + notice "Project initialized using '$1' as storage directory." % storage + of "map": + if args.len < 2: + fatal "No alias specified." + quit(2) + prj.map(args[1], %opts) + else: + #TODO + echo usage + discard