Trying to set directory of current shell.
h3rald h3rald@h3rald.com
Mon, 04 Oct 2021 10:13:20 +0200
3 files changed,
41 insertions(+),
18 deletions(-)
M
src/pls.nim
→
src/pls.nim
@@ -5,7 +5,8 @@ parseopt,
logging, algorithm, strutils, - sequtils + sequtils, + pegs import plspkg/plslogger@@ -17,6 +18,15 @@ import
plspkg/config, plspkg/project, plspkg/messaging + +let pegTaskDef = peg""" + def <- (id)('+' id)* + id <- [$a-z0-9_][a-zA-Z0-9_-]+ +""" + +let pegPropName = peg""" + [a-z0-9_][a-zA-Z0-9_-]+ +""" let usage* = """ $1 v$2 - $3 (c) 2021 $4@@ -42,7 +52,13 @@ proc addProperty(parentObj: JsonNode, name = ""): tuple[key: string, value: JsonNode] =
var done = false while (not done): if name == "": - result.key = editValue("Name") + var ok = false + while not ok: + result.key = editValue("Name") + if not result.key.match(pegPropName): + warn "Property name contains invalid characters." + else: + ok = true elif name == "name": warn "Property identifier 'name' cannot be modified." else:@@ -74,9 +90,15 @@
proc addTaskDefinition(parentObj: JsonNode, name = ""): tuple[key: string, value: JsonNode] = # TODO: validate name of task definition! (not $syntax or $description, etc.) if name == "": - result.key = editValue("Task Definition Matcher") + var ok = false + while not ok: + result.key = editValue("Task Definition") + if not result.key.match(pegTaskDef): + warn "Task definition contains invalid characters." + else: + ok = true else: - printValue(" Task Definition Matcher", name) + printValue(" Task Definition", name) result.key = name result.value = newJObject() result.value["cmd"] = addProperty(parentObj[name], "cmd").value
M
src/plspkg/pls.json
→
src/plspkg/pls.json
@@ -5,7 +5,7 @@ "go": {
"$syntax": "go <target>", "$description": "Goes to the folder identified by <target>.", "folder": { - "cmd": "cd \"{{folder}}\"" + "cmd": "$setCurrentDir {{folder}}" } } },
M
src/plspkg/project.nim
→
src/plspkg/project.nim
@@ -1,5 +1,6 @@
import os, + tables, json, logging, strutils,@@ -15,11 +16,15 @@ targets*: JsonNode
type PlsError = ref object of ValueError +type SystemTask = proc (params: string): void const plsTpl* = "pls.json".slurp const systemHelp = "help.json".slurp +let systemProps = @["$$os:$1" % hostOS, "$$cpu:$1" % hostCPU] let placeholder = peg"'{{' {[^}]+} '}}'" +var systemTasks = initTable[string, SystemTask]() +systemTasks["$setCurrentDir"] = proc (params: string) = setCurrentDir(params) proc newPlsProject*(dir: string): PlsProject = result.dir = dir@@ -42,8 +47,7 @@ let cfg = prj.configFile.parseFile
prj.version = cfg["version"].getInt prj.tasks = cfg["tasks"] prj.targets = cfg["targets"] - if cfg.hasKey("dir"): - prj.dir = cfg["dir"].getStr + # Set system properties proc help*(prj: var PlsProject): JsonNode = result = newJObject()@@ -115,7 +119,8 @@ prj.tasks.delete(alias)
prj.save notice "Task '$1' removed." % alias -proc lookupTask(prj: PlsProject, task: string, props: seq[string], cmd: var JsonNode): bool = +proc lookupTask(prj: PlsProject, task: string, ps: seq[string], cmd: var JsonNode): bool = + let props = ps.concat(systemProps); if not prj.tasks.hasKey task: warn "Task '$1' not found" % task return@@ -145,20 +150,16 @@ for key, val in target.pairs:
keys.add key var res: JsonNode var cmd: string - var pwd = prj.dir - if target.hasKey("dir"): - pwd = target["dir"].getStr if prj.lookupTask(task, keys, res): cmd = res["cmd"].getStr.replace(placeholder) do (m: int, n: int, c: openArray[string]) -> string: return target[c[0]].getStr - if res.hasKey("pwd"): - pwd = res["pwd"].getStr.replace(placeholder) do (m: int, n: int, c: openArray[string]) -> string: - return target[c[0]].getStr - pwd = prj.dir/pwd notice "Executing: $1" % cmd - pwd.createDir() - pwd.setCurrentDir() - result = execShellCmd cmd + if cmd[0] == '$': + let parts = cmd.split(" ") + if systemTasks.hasKey(parts[0]): + systemTasks[parts[0]](parts[1..parts.len-1].join(" ")) + else: + result = execShellCmd cmd else: debug "Task '$1' not available for target '$2'" % [task, alias] setCurrentDir(prj.dir)