Skip to content

Commit

Permalink
Return bool from hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
genotrance committed Apr 13, 2019
1 parent ab38b81 commit 0ed53d6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 9 additions & 8 deletions src/nimblepkg/nimscriptapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/nimblepkg/nimscriptexecutor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
##
Expand Down
8 changes: 5 additions & 3 deletions src/nimblepkg/nimscriptwrapper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand All @@ -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.
##
Expand Down

0 comments on commit 0ed53d6

Please sign in to comment.