From 0ed53d60bcdc8bb11beddb965590ed3ee63349d4 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Sat, 13 Apr 2019 00:44:26 -0500 Subject: [PATCH] Return bool from hooks --- src/nimble.nim | 2 +- src/nimblepkg/nimscriptapi.nim | 17 +++++++++-------- src/nimblepkg/nimscriptexecutor.nim | 4 +++- src/nimblepkg/nimscriptwrapper.nim | 8 +++++--- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 694e2180..26f4a2a4 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -1103,7 +1103,7 @@ proc doAction(options: Options) = return let isPreDefined = options.action.command.normalize == "test" - var execResult: ExecutionResult[void] + var execResult: ExecutionResult[bool] if execCustom(options, execResult, failFast=not isPreDefined): if execResult.hasTaskRequestedCommand(): doAction(execResult.getOptionsForCommand(options)) diff --git a/src/nimblepkg/nimscriptapi.nim b/src/nimblepkg/nimscriptapi.nim index a43aae8f..35115c7a 100644 --- a/src/nimblepkg/nimscriptapi.nim +++ b/src/nimblepkg/nimscriptapi.nim @@ -28,6 +28,7 @@ var startCommand = "e" endCommand = startCommand endProject = "" + retVal = true proc requires*(deps: varargs[string]) = ## Call this to set the list of requirements of your Nimble @@ -92,11 +93,9 @@ proc onExit() = output &= "\"command\": \"" & endCommand & "\", " if endProject.len != 0: output &= "\"project\": \"" & endProject & "\", " + output &= "\"retVal\": " & $retVal - if output.len != 0: - echo "{" & output[0 .. ^3] & "}" - else: - echo "{}" + echo "{" & output & "}" # TODO: New release of Nim will move this `task` template under a # `when not defined(nimble)`. This will allow us to override it in the future. @@ -117,21 +116,23 @@ template task*(name: untyped; description: string; body: untyped): untyped = template before*(action: untyped, body: untyped): untyped = ## Defines a block of code which is evaluated before ``action`` is executed. - proc `action Before`*() = + proc `action Before`*(): bool = + result = true body let params = getParams() if astToStr(action) & "Before" in params: - `action Before`() + retVal = `action Before`() template after*(action: untyped, body: untyped): untyped = ## Defines a block of code which is evaluated after ``action`` is executed. - proc `action After`*() = + proc `action After`*(): bool = + result = true body let params = getParams() if astToStr(action) & "After" in params: - `action After`() + retVal = `action After`() template builtin = discard diff --git a/src/nimblepkg/nimscriptexecutor.nim b/src/nimblepkg/nimscriptexecutor.nim index ef5ac458..fad0d1e5 100644 --- a/src/nimblepkg/nimscriptexecutor.nim +++ b/src/nimblepkg/nimscriptexecutor.nim @@ -27,9 +27,11 @@ proc execHook*(options: Options, before: bool): bool = else: actionName.normalize in pkgInfo.postHooks if pkgInfo.isNimScript and hookExists: let res = execHook(nimbleFile, actionName, before, options) + if res.success: + result = res.retVal proc execCustom*(options: Options, - execResult: var ExecutionResult[void], + execResult: var ExecutionResult[bool], failFast = true): bool = ## Executes the custom command using the nimscript backend. ## diff --git a/src/nimblepkg/nimscriptwrapper.nim b/src/nimblepkg/nimscriptwrapper.nim index 728c5713..55a5b8de 100644 --- a/src/nimblepkg/nimscriptwrapper.nim +++ b/src/nimblepkg/nimscriptwrapper.nim @@ -49,7 +49,7 @@ proc setupNimscript*(scriptName: string, options: Options): tuple[nimsFile, iniF else: raise newException(NimbleError, "printPkgInfo() failed") -proc execScript*(scriptName, actionName: string, options: Options): ExecutionResult[void] = +proc execScript*(scriptName, actionName: string, options: Options): ExecutionResult[bool] = let (nimsFile, iniFile) = setupNimscript(scriptName, options) @@ -71,13 +71,15 @@ proc execScript*(scriptName, actionName: string, options: Options): ExecutionRes result.command = $j["command"] if "project" in j: result.arguments.add $j["project"] + 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") proc execTask*(scriptName, taskName: string, - options: Options): ExecutionResult[void] = + options: Options): ExecutionResult[bool] = ## Executes the specified task in the specified script. ## ## `scriptName` should be a filename pointing to the nimscript file. @@ -87,7 +89,7 @@ proc execTask*(scriptName, taskName: string, result = execScript(scriptName, taskName, options) proc execHook*(scriptName, actionName: string, before: bool, - options: Options): ExecutionResult[void] = + options: Options): ExecutionResult[bool] = ## Executes the specified action's hook. Depending on ``before``, either ## the "before" or the "after" hook. ##