From b05d9480281ebae7a0f5fd0331c8627bbf2a77d5 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Tue, 16 Apr 2019 13:29:37 -0500 Subject: [PATCH] Add switch support --- src/nimblepkg/nimscriptapi.nim | 29 +++++++++++++++++++++++++++-- src/nimblepkg/nimscriptwrapper.nim | 12 +++++++++--- src/nimblepkg/packageparser.nim | 5 +++-- 3 files changed, 39 insertions(+), 7 deletions(-) mode change 100644 => 100755 src/nimblepkg/packageparser.nim diff --git a/src/nimblepkg/nimscriptapi.nim b/src/nimblepkg/nimscriptapi.nim index 4cbe564a..987338e6 100755 --- a/src/nimblepkg/nimscriptapi.nim +++ b/src/nimblepkg/nimscriptapi.nim @@ -3,8 +3,8 @@ ## This module is implicitly imported in NimScript .nimble files. -import system except getCommand, setCommand -import strformat, strutils +import system except getCommand, setCommand, switch, `--` +import strformat, strutils, tables var packageName* = "" ## Set this to the package name. It @@ -28,6 +28,7 @@ var beforeHooks: seq[string] = @[] afterHooks: seq[string] = @[] commandLineParams: seq[string] = @[] + flags: TableRef[string, seq[string]] command = "e" project = "" @@ -50,6 +51,21 @@ proc setCommand(cmd: string, prj = "") = if prj.len != 0: project = prj +proc switch(key: string, value="") = + if flags.isNil: + flags = newTable[string, seq[string]]() + + if flags.hasKey(key): + flags[key].add(value) + else: + flags[key] = @[value] + +template `--`(key, val: untyped) = + switch(astToStr(key), strip astToStr(val)) + +template `--`(key: untyped) = + switch(astToStr(key), "") + template printIfLen(varName) = if varName.len != 0: iniOut &= astToStr(varName) & ": \"" & varName & "\"\n" @@ -98,6 +114,15 @@ proc onExit() = output &= "\"command\": \"" & command & "\", " if project.len != 0: output &= "\"project\": \"" & project & "\", " + if not flags.isNil and flags.len != 0: + output &= "\"flags\": {" + for key, val in flags.pairs: + output &= "\"" & key & "\": [" + for v in val: + output &= "\"" & v & "\", " + output = output[0 .. ^3] & "], " + output = output[0 .. ^3] & "}, " + output &= "\"retVal\": " & $retVal echo "{" & output & "}" diff --git a/src/nimblepkg/nimscriptwrapper.nim b/src/nimblepkg/nimscriptwrapper.nim index e6406217..6adda736 100755 --- a/src/nimblepkg/nimscriptwrapper.nim +++ b/src/nimblepkg/nimscriptwrapper.nim @@ -61,6 +61,9 @@ proc execScript*(scriptName, actionName: string, options: Options): ExecutionRes (output, exitCode) = nimsFile.execNimscript(actionName, options) + defer: + nimsFile.removeFile() + if exitCode != 0: raise newException(NimbleError, output) @@ -73,21 +76,24 @@ proc execScript*(scriptName, actionName: string, options: Options): ExecutionRes parseJson("{}") result.success = true + result.flags = newTable[string, seq[string]]() if "command" in j: result.command = j["command"].getStr() else: result.command = internalCmd if "project" in j: result.arguments.add j["project"].getStr() + if "flags" in j: + for flag, vals in j["flags"].pairs: + result.flags[flag] = @[] + for val in vals.items(): + result.flags[flag].add val.getStr() if "retVal" in j: result.retVal = j["retVal"].getBool() - result.flags = newTable[string, seq[string]]() if lines.len > 1: stdout.writeLine lines[0 .. ^2].join("\n") - nimsFile.removeFile() - proc execTask*(scriptName, taskName: string, options: Options): ExecutionResult[bool] = ## Executes the specified task in the specified script. diff --git a/src/nimblepkg/packageparser.nim b/src/nimblepkg/packageparser.nim old mode 100644 new mode 100755 index 30cd5866..00e7c775 --- a/src/nimblepkg/packageparser.nim +++ b/src/nimblepkg/packageparser.nim @@ -288,11 +288,12 @@ proc readPackageInfoFromNims(scriptName: string, options: Options, let (nimsFile, iniFile) = setupNimscript(scriptName, options) + defer: + nimsFile.removeFile() + if iniFile.fileExists(): readPackageInfoFromNimble(iniFile, result) - nimsFile.removeFile() - proc inferInstallRules(pkgInfo: var PackageInfo, options: Options) = # Binary packages shouldn't install .nim files by default. # (As long as the package info doesn't explicitly specify what should be