From 83731a9b32fd8a1f41d5e780b1e2def0b4183a5c Mon Sep 17 00:00:00 2001 From: Andrea Ferretti Date: Fri, 10 Feb 2017 16:53:27 +0100 Subject: [PATCH] Fixed #329 --- src/nimble.nim | 5 +++-- src/nimblepkg/nimscriptsupport.nim | 23 ++++++++++++++--------- tests/nimscript/nimscript.nimble | 5 +++++ tests/tester.nim | 11 +++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 9edf446..1df791a 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -1063,8 +1063,9 @@ proc doAction(options: Options) = parseCommand(execResult.command, newOptions) for arg in execResult.arguments: parseArgument(arg, newOptions) - for flag, val in execResult.flags: - parseFlag(flag, val, newOptions) + for flag, vals in execResult.flags: + for val in vals: + parseFlag(flag, val, newOptions) doAction(newOptions) if options.action.typ != actionCustom: diff --git a/src/nimblepkg/nimscriptsupport.nim b/src/nimblepkg/nimscriptsupport.nim index a154891..5ef3ce3 100644 --- a/src/nimblepkg/nimscriptsupport.nim +++ b/src/nimblepkg/nimscriptsupport.nim @@ -16,17 +16,18 @@ from compiler/astalgo import strTableGet import compiler/options as compiler_options import common, version, options, packageinfo, cli -import os, strutils, strtabs, times, osproc, sets +import os, strutils, strtabs, tables, times, osproc, sets when not declared(resetAllModulesHard): import compiler/modulegraphs type + Flags = TableRef[string, seq[string]] ExecutionResult*[T] = object success*: bool command*: string arguments*: seq[string] - flags*: StringTableRef + flags*: Flags retVal*: T const @@ -74,8 +75,7 @@ proc extractRequires(ident: PSym, result: var seq[PkgTuple]) = when declared(newIdentCache): var identCache = newIdentCache() -proc setupVM(module: PSym; scriptName: string, - flags: StringTableRef): PEvalContext = +proc setupVM(module: PSym; scriptName: string, flags: Flags): PEvalContext = ## This procedure is exported in the compiler sources, but its implementation ## is too Nim-specific to be used by Nimble. ## Specifically, the implementation of ``switch`` is problematic. Sooo @@ -180,15 +180,20 @@ proc setupVM(module: PSym; scriptName: string, setResult(a, compiler_options.command) cbconf switch: if not flags.isNil: - flags[a.getString 0] = a.getString 1 + let + key = a.getString 0 + value = a.getString 1 + if flags.hasKey(key): + flags[key].add(value) + else: + flags[key] = @[value] proc getNimPrefixDir(): string = splitPath(findExe("nim")).head.parentDir when declared(ModuleGraph): var graph: ModuleGraph -proc execScript(scriptName: string, flags: StringTableRef, - options: Options): PSym = +proc execScript(scriptName: string, flags: Flags, options: Options): PSym = ## Executes the specified script. Returns the script's module symbol. ## ## No clean up is performed and must be done manually! @@ -364,7 +369,7 @@ proc execTask*(scriptName, taskName: string, ## ## `scriptName` should be a filename pointing to the nimscript file. result.success = true - result.flags = newStringTable() + result.flags = newTable[string, seq[string]]() compiler_options.command = internalCmd display("Executing", "task $# in $#" % [taskName, scriptName], priority = HighPriority) @@ -392,7 +397,7 @@ proc execHook*(scriptName, actionName: string, before: bool, ## ## `scriptName` should be a filename pointing to the nimscript file. result.success = true - result.flags = newStringTable() + result.flags = newTable[string, seq[string]]() compiler_options.command = internalCmd let hookName = if before: actionName.toLowerAscii & "Before" diff --git a/tests/nimscript/nimscript.nimble b/tests/nimscript/nimscript.nimble index bfa0405..ff8c5c6 100644 --- a/tests/nimscript/nimscript.nimble +++ b/tests/nimscript/nimscript.nimble @@ -21,6 +21,11 @@ task cr, "Testing `nimble c -r nimscript.nim` via setCommand": --r setCommand "c", "nimscript.nim" +task repeated, "Testing `nimble c nimscript.nim` with repeated flags": + --define: foo + --define: bar + setCommand "c", "nimscript.nim" + task api, "Testing nimscriptapi module functionality": echo(getPkgDir()) diff --git a/tests/tester.nim b/tests/tester.nim index 007572f..ca1bd1b 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -192,6 +192,17 @@ test "can use nimscript's setCommand with flags": check exitCode == QuitSuccess check "Hello World".normalize in lines[^2].normalize +test "can use nimscript with repeated flags (issue #329)": + cd "nimscript": + let (output, exitCode) = execNimble("--debug", "repeated") + let lines = output.strip.splitLines() + check exitCode == QuitSuccess + var found = false + for line in lines: + if line.contains("--define:foo"): + found = true + check found == true + test "can list nimscript tasks": cd "nimscript": let (output, exitCode) = execNimble("tasks")