all repos — pls @ ee1e79693825888eea1886dc487c26799c10ec7a

A polite but determined task runner.

Implemented command definition.
Fabio Cevasco fabio.cevasco@it.abb.com
Fri, 01 Oct 2021 12:26:06 +0200
commit

ee1e79693825888eea1886dc487c26799c10ec7a

parent

a519f1cd60665b1238eb3a635212a1b4411f530b

3 files changed, 81 insertions(+), 18 deletions(-)

jump to
M src/pls.nimsrc/pls.nim

@@ -65,7 +65,7 @@ ok = true

except: warn("Please enter a valid JSON value.") done = done or confirm("OK?") - + proc addProperties(obj: var JsonNode) = var done = false while (not done):

@@ -73,6 +73,25 @@ let prop = addProperty(obj)

obj[prop.key] = prop.value done = not confirm("Do you want to add/remove more properties?") +proc addCommandDefinition(parentObj: JsonNode, name = ""): tuple[key: string, value: JsonNode] = + # TODO: validate name of command definition! (not _syntax or _description, etc.) + if name == "": + result.key = editValue("Command Definition Matcher") + else: + printValue(" Command Definition Matcher", name) + result.key = name + result.value = newJObject() + result.value["cmd"] = addProperty(parentObj[name], "cmd").value + result.value["pwd"] = addProperty(parentObj[name], "pwd").value + # TODO: delete command definition matcher if all properties are null. + +proc addCommandDefinitions(obj: var JsonNode) = + var done = false + while (not done): + let prop = addCommandDefinition(obj) + obj[prop.key] = prop.value + done = not confirm("Do you want to add/remove more command definitions?") + ### MAIN ### var args = newSeq[string](0)

@@ -86,7 +105,7 @@ case key:

of "force", "f": force = true of "global", "g": - force = true + global = true of "log", "l": var val = val setLogLevel(val)

@@ -132,7 +151,7 @@ prj.init()

notice "Project initialized." of "def": if args.len < 3: - fatal "No target specified." + fatal "No alias specified." quit(3) let kind = args[1] let alias = args[2]

@@ -157,14 +176,29 @@ else:

notice "Definining new target: " & alias warn "Specify properties for target '$1':" % alias addProperties(props) - prj.def(alias, props) + prj.defTarget(alias, props) else: # command - # TODO - fatal "Not implemented" - quit(10) + if prj.commands.hasKey(alias): + notice "Redefining existing command: " & alias + warn "Specify properties for command '$1':" % alias + props = prj.commands[alias] + for k, v in props.mpairs: + if ["_syntax", "_description"].contains(k): + let prop = addProperty(props, k) + props[prop.key] = prop.value + else: + let prop = addCommandDefinition(props, k) + props[prop.key] = prop.value + if confirm "Do you want to add/remove more command definitions?": + addCommandDefinitions(props) + else: + props["_syntax"] = addProperty(props, "_syntax").value + props["_description"] = addProperty(props, "_description").value + addCommandDefinitions(props) + prj.defCommand(alias, props) of "undef": if args.len < 3: - fatal "No target specified." + fatal "No alias specified." quit(3) let kind = args[1] let alias = args[2]

@@ -177,11 +211,13 @@ if not prj.targets.hasKey(alias):

fatal "Target '$1' not defined." % [alias] quit(4) if force or confirm("Remove definition for target '$1'?" % alias): - prj.undef(alias) + prj.undefTarget(alias) else: # command - # TODO - fatal "Not implemented" - quit(10) + if not prj.commands.hasKey(alias): + fatal "Command '$1' not defined." % [alias] + quit(4) + if force or confirm("Remove definition for command '$1'?" % alias): + prj.undefCommand(alias) of "info": prj.load if args.len < 2:
M src/plspkg/help.jsonsrc/plspkg/help.json

@@ -12,11 +12,11 @@ "_syntax": "init [<storage-dir>]",

"_description": "Initializes a project in the current directory (using <storage-dir> as storage directory)." }, "def": { - "_syntax": "def (command|target) <id>", - "_description": "Configures a new or existing command or target <id>." + "_syntax": "def (command|target) <alias>", + "_description": "Configures a new or existing command or target <alias>." }, "undef": { - "_syntax": "undef (command|target) <id>", - "_description": "Unmaps the previously-mapped command or target <id>." + "_syntax": "undef (command|target) <alias>", + "_description": "Unmaps the previously-mapped command or target <alias>." } }
M src/plspkg/project.nimsrc/plspkg/project.nim

@@ -72,7 +72,7 @@ o["commands"] = %prj.commands

o["targets"] = %prj.targets prj.configFile.writeFile(o.pretty) -proc def*(prj: var PlsProject, alias: string, props: var JsonNode) = +proc defTarget*(prj: var PlsProject, alias: string, props: var JsonNode) = for k, v in props.mpairs: if v == newJNull(): props.delete(k)

@@ -90,11 +90,38 @@ notice " $1: $2" % [key, $val]

prj.save notice "Target '$1' saved." % alias -proc undef*(prj: var PlsProject, alias: string) = +proc undefTarget*(prj: var PlsProject, alias: string) = prj.load prj.targets.delete(alias) prj.save notice "Target '$1' removed." % alias + +proc defCommand*(prj: var PlsProject, alias: string, props: var JsonNode) = + for k, v in props.mpairs: + if v == newJNull(): + props.delete(k): + elif v.kind == JObject: + for kk, vv in v.pairs: + if vv == newJNull(): + v.delete(kk) + prj.load + if not prj.commands.hasKey alias: + notice "Adding command '$1'..." % alias + prj.commands[alias] = newJObject() + else: + notice "Updating command '$1'..." % alias + prj.commands[alias] = newJObject() + for key, val in props.pairs: + prj.commands[alias][key] = val + notice " $1: $2" % [key, $val] + prj.save + notice "Command '$1' saved." % alias + +proc undefCommand*(prj: var PlsProject, alias: string) = + prj.load + prj.commands.delete(alias) + prj.save + notice "Command '$1' removed." % alias proc lookupCommand(prj: PlsProject, command: string, props: seq[string], cmd: var JsonNode): bool = if not prj.commands.hasKey command: