Fixes #606. Build before/after hook executed every time package is built.
This commit is contained in:
parent
4a2aaa07dc
commit
137bb1ed07
4 changed files with 40 additions and 15 deletions
|
|
@ -216,15 +216,21 @@ proc processDeps(pkginfo: PackageInfo, options: Options): seq[PackageInfo] =
|
||||||
|
|
||||||
proc buildFromDir(
|
proc buildFromDir(
|
||||||
pkgInfo: PackageInfo, paths, args: seq[string],
|
pkgInfo: PackageInfo, paths, args: seq[string],
|
||||||
binToBuild: Option[string] = none[string]()
|
options: Options
|
||||||
) =
|
) =
|
||||||
## Builds a package as specified by ``pkgInfo``.
|
## Builds a package as specified by ``pkgInfo``.
|
||||||
|
let binToBuild = options.getCompilationBinary()
|
||||||
|
# Handle pre-`build` hook.
|
||||||
|
let realDir = pkgInfo.getRealDir()
|
||||||
|
cd realDir: # Make sure `execHook` executes the correct .nimble file.
|
||||||
|
if not execHook(options, actionBuild, true):
|
||||||
|
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
||||||
|
|
||||||
if pkgInfo.bin.len == 0:
|
if pkgInfo.bin.len == 0:
|
||||||
raise newException(NimbleError,
|
raise newException(NimbleError,
|
||||||
"Nothing to build. Did you specify a module to build using the" &
|
"Nothing to build. Did you specify a module to build using the" &
|
||||||
" `bin` key in your .nimble file?")
|
" `bin` key in your .nimble file?")
|
||||||
var args = args
|
var args = args
|
||||||
let realDir = pkgInfo.getRealDir()
|
|
||||||
let nimblePkgVersion = "-d:NimblePkgVersion=" & pkgInfo.version
|
let nimblePkgVersion = "-d:NimblePkgVersion=" & pkgInfo.version
|
||||||
for path in paths: args.add("--path:\"" & path & "\" ")
|
for path in paths: args.add("--path:\"" & path & "\" ")
|
||||||
for bin in pkgInfo.bin:
|
for bin in pkgInfo.bin:
|
||||||
|
|
@ -255,6 +261,10 @@ proc buildFromDir(
|
||||||
exc.hint = hint
|
exc.hint = hint
|
||||||
raise exc
|
raise exc
|
||||||
|
|
||||||
|
# Handle post-`build` hook.
|
||||||
|
cd realDir: # Make sure `execHook` executes the correct .nimble file.
|
||||||
|
discard execHook(options, actionBuild, false)
|
||||||
|
|
||||||
proc removePkgDir(dir: string, options: Options) =
|
proc removePkgDir(dir: string, options: Options) =
|
||||||
## Removes files belonging to the package in ``dir``.
|
## Removes files belonging to the package in ``dir``.
|
||||||
try:
|
try:
|
||||||
|
|
@ -332,7 +342,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
|
||||||
# Handle pre-`install` hook.
|
# Handle pre-`install` hook.
|
||||||
if not options.depsOnly:
|
if not options.depsOnly:
|
||||||
cd dir: # Make sure `execHook` executes the correct .nimble file.
|
cd dir: # Make sure `execHook` executes the correct .nimble file.
|
||||||
if not execHook(options, true):
|
if not execHook(options, actionInstall, true):
|
||||||
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
||||||
|
|
||||||
var pkgInfo = getPkgInfo(dir, options)
|
var pkgInfo = getPkgInfo(dir, options)
|
||||||
|
|
@ -363,7 +373,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
|
||||||
options.action.passNimFlags
|
options.action.passNimFlags
|
||||||
else:
|
else:
|
||||||
@[]
|
@[]
|
||||||
buildFromDir(pkgInfo, paths, flags & "-d:release")
|
buildFromDir(pkgInfo, paths, flags & "-d:release", options)
|
||||||
|
|
||||||
let pkgDestDir = pkgInfo.getPkgDest(options)
|
let pkgDestDir = pkgInfo.getPkgDest(options)
|
||||||
if existsDir(pkgDestDir) and existsFile(pkgDestDir / "nimblemeta.json"):
|
if existsDir(pkgDestDir) and existsFile(pkgDestDir / "nimblemeta.json"):
|
||||||
|
|
@ -446,7 +456,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
|
||||||
# executes the hook defined in the CWD, so we set it to where the package
|
# executes the hook defined in the CWD, so we set it to where the package
|
||||||
# has been installed.
|
# has been installed.
|
||||||
cd dest.splitFile.dir:
|
cd dest.splitFile.dir:
|
||||||
discard execHook(options, false)
|
discard execHook(options, actionInstall, false)
|
||||||
|
|
||||||
proc getDownloadInfo*(pv: PkgTuple, options: Options,
|
proc getDownloadInfo*(pv: PkgTuple, options: Options,
|
||||||
doPrompt: bool): (DownloadMethod, string,
|
doPrompt: bool): (DownloadMethod, string,
|
||||||
|
|
@ -514,7 +524,7 @@ proc build(options: Options) =
|
||||||
let deps = processDeps(pkginfo, options)
|
let deps = processDeps(pkginfo, options)
|
||||||
let paths = deps.map(dep => dep.getRealDir())
|
let paths = deps.map(dep => dep.getRealDir())
|
||||||
var args = options.getCompilationFlags()
|
var args = options.getCompilationFlags()
|
||||||
buildFromDir(pkgInfo, paths, args, options.getCompilationBinary())
|
buildFromDir(pkgInfo, paths, args, options)
|
||||||
|
|
||||||
proc execBackend(options: Options) =
|
proc execBackend(options: Options) =
|
||||||
let
|
let
|
||||||
|
|
@ -917,7 +927,7 @@ proc developFromDir(dir: string, options: Options) =
|
||||||
raiseNimbleError("Cannot develop dependencies only.")
|
raiseNimbleError("Cannot develop dependencies only.")
|
||||||
|
|
||||||
cd dir: # Make sure `execHook` executes the correct .nimble file.
|
cd dir: # Make sure `execHook` executes the correct .nimble file.
|
||||||
if not execHook(options, true):
|
if not execHook(options, actionDevelop, true):
|
||||||
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
||||||
|
|
||||||
var pkgInfo = getPkgInfo(dir, options)
|
var pkgInfo = getPkgInfo(dir, options)
|
||||||
|
|
@ -970,7 +980,7 @@ proc developFromDir(dir: string, options: Options) =
|
||||||
|
|
||||||
# Execute the post-develop hook.
|
# Execute the post-develop hook.
|
||||||
cd dir:
|
cd dir:
|
||||||
discard execHook(options, false)
|
discard execHook(options, actionDevelop, false)
|
||||||
|
|
||||||
proc develop(options: Options) =
|
proc develop(options: Options) =
|
||||||
if options.action.packages == @[]:
|
if options.action.packages == @[]:
|
||||||
|
|
@ -1150,7 +1160,7 @@ proc doAction(options: Options) =
|
||||||
of actionNil:
|
of actionNil:
|
||||||
assert false
|
assert false
|
||||||
of actionCustom:
|
of actionCustom:
|
||||||
if not execHook(options, true):
|
if not execHook(options, actionCustom, true):
|
||||||
display("Warning", "Pre-hook prevented further execution.", Warning,
|
display("Warning", "Pre-hook prevented further execution.", Warning,
|
||||||
HighPriority)
|
HighPriority)
|
||||||
return
|
return
|
||||||
|
|
@ -1166,7 +1176,7 @@ proc doAction(options: Options) =
|
||||||
if isPreDefined:
|
if isPreDefined:
|
||||||
test(options)
|
test(options)
|
||||||
# Run the post hook for `test` in case it exists.
|
# Run the post hook for `test` in case it exists.
|
||||||
discard execHook(options, false)
|
discard execHook(options, actionCustom, false)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var error = ""
|
var error = ""
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,12 @@ import os, strutils, sets
|
||||||
import packageparser, common, packageinfo, options, nimscriptwrapper, cli,
|
import packageparser, common, packageinfo, options, nimscriptwrapper, cli,
|
||||||
version
|
version
|
||||||
|
|
||||||
proc execHook*(options: Options, before: bool): bool =
|
proc execHook*(options: Options, hookAction: ActionType, before: bool): bool =
|
||||||
## Returns whether to continue.
|
## Returns whether to continue.
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
# For certain commands hooks should not be evaluated.
|
# For certain commands hooks should not be evaluated.
|
||||||
if options.action.typ in noHookActions:
|
if hookAction in noHookActions:
|
||||||
return
|
return
|
||||||
|
|
||||||
var nimbleFile = ""
|
var nimbleFile = ""
|
||||||
|
|
@ -21,8 +21,8 @@ proc execHook*(options: Options, before: bool): bool =
|
||||||
# PackageInfos are cached so we can read them as many times as we want.
|
# PackageInfos are cached so we can read them as many times as we want.
|
||||||
let pkgInfo = getPkgInfoFromFile(nimbleFile, options)
|
let pkgInfo = getPkgInfoFromFile(nimbleFile, options)
|
||||||
let actionName =
|
let actionName =
|
||||||
if options.action.typ == actionCustom: options.action.command
|
if hookAction == actionCustom: options.action.command
|
||||||
else: ($options.action.typ)[6 .. ^1]
|
else: ($hookAction)[6 .. ^1]
|
||||||
let hookExists =
|
let hookExists =
|
||||||
if before: actionName.normalize in pkgInfo.preHooks
|
if before: actionName.normalize in pkgInfo.preHooks
|
||||||
else: actionName.normalize in pkgInfo.postHooks
|
else: actionName.normalize in pkgInfo.postHooks
|
||||||
|
|
@ -58,7 +58,7 @@ proc execCustom*(options: Options,
|
||||||
HighPriority)
|
HighPriority)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not execHook(options, false):
|
if not execHook(options, actionCustom, false):
|
||||||
return
|
return
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
|
|
@ -54,3 +54,9 @@ before install:
|
||||||
|
|
||||||
after install:
|
after install:
|
||||||
echo("After PkgDir: ", getPkgDir())
|
echo("After PkgDir: ", getPkgDir())
|
||||||
|
|
||||||
|
before build:
|
||||||
|
echo("Before build")
|
||||||
|
|
||||||
|
after build:
|
||||||
|
echo("After build")
|
||||||
|
|
@ -286,12 +286,21 @@ suite "nimscript":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble(["install", "-y"])
|
let (output, exitCode) = execNimble(["install", "-y"])
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
|
check output.contains("Before build")
|
||||||
|
check output.contains("After build")
|
||||||
let lines = output.strip.processOutput()
|
let lines = output.strip.processOutput()
|
||||||
check lines[0].startsWith("Before PkgDir:")
|
check lines[0].startsWith("Before PkgDir:")
|
||||||
check lines[0].endsWith("tests" / "nimscript")
|
check lines[0].endsWith("tests" / "nimscript")
|
||||||
check lines[^1].startsWith("After PkgDir:")
|
check lines[^1].startsWith("After PkgDir:")
|
||||||
check lines[^1].endsWith("tests" / "nimbleDir" / "pkgs" / "nimscript-0.1.0")
|
check lines[^1].endsWith("tests" / "nimbleDir" / "pkgs" / "nimscript-0.1.0")
|
||||||
|
|
||||||
|
test "before/after on build":
|
||||||
|
cd "nimscript":
|
||||||
|
let (output, exitCode) = execNimble(["build"])
|
||||||
|
check exitCode == QuitSuccess
|
||||||
|
check output.contains("Before build")
|
||||||
|
check output.contains("After build")
|
||||||
|
|
||||||
test "can execute nimscript tasks":
|
test "can execute nimscript tasks":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("--verbose", "work")
|
let (output, exitCode) = execNimble("--verbose", "work")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue