From 137bb1ed075708da3807c9e4b89f5c6e8d103210 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sun, 22 Sep 2019 10:45:26 +0100 Subject: [PATCH] Fixes #606. Build before/after hook executed every time package is built. --- src/nimble.nim | 30 +++++++++++++++++++---------- src/nimblepkg/nimscriptexecutor.nim | 10 +++++----- tests/nimscript/nimscript.nimble | 6 ++++++ tests/tester.nim | 9 +++++++++ 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 5cf9652..2185211 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -216,15 +216,21 @@ proc processDeps(pkginfo: PackageInfo, options: Options): seq[PackageInfo] = proc buildFromDir( pkgInfo: PackageInfo, paths, args: seq[string], - binToBuild: Option[string] = none[string]() + options: Options ) = ## 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: raise newException(NimbleError, "Nothing to build. Did you specify a module to build using the" & " `bin` key in your .nimble file?") var args = args - let realDir = pkgInfo.getRealDir() let nimblePkgVersion = "-d:NimblePkgVersion=" & pkgInfo.version for path in paths: args.add("--path:\"" & path & "\" ") for bin in pkgInfo.bin: @@ -255,6 +261,10 @@ proc buildFromDir( exc.hint = hint 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) = ## Removes files belonging to the package in ``dir``. try: @@ -332,7 +342,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options, # Handle pre-`install` hook. if not options.depsOnly: 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.") var pkgInfo = getPkgInfo(dir, options) @@ -363,7 +373,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options, options.action.passNimFlags else: @[] - buildFromDir(pkgInfo, paths, flags & "-d:release") + buildFromDir(pkgInfo, paths, flags & "-d:release", options) let pkgDestDir = pkgInfo.getPkgDest(options) 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 # has been installed. cd dest.splitFile.dir: - discard execHook(options, false) + discard execHook(options, actionInstall, false) proc getDownloadInfo*(pv: PkgTuple, options: Options, doPrompt: bool): (DownloadMethod, string, @@ -514,7 +524,7 @@ proc build(options: Options) = let deps = processDeps(pkginfo, options) let paths = deps.map(dep => dep.getRealDir()) var args = options.getCompilationFlags() - buildFromDir(pkgInfo, paths, args, options.getCompilationBinary()) + buildFromDir(pkgInfo, paths, args, options) proc execBackend(options: Options) = let @@ -917,7 +927,7 @@ proc developFromDir(dir: string, options: Options) = raiseNimbleError("Cannot develop dependencies only.") 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.") var pkgInfo = getPkgInfo(dir, options) @@ -970,7 +980,7 @@ proc developFromDir(dir: string, options: Options) = # Execute the post-develop hook. cd dir: - discard execHook(options, false) + discard execHook(options, actionDevelop, false) proc develop(options: Options) = if options.action.packages == @[]: @@ -1150,7 +1160,7 @@ proc doAction(options: Options) = of actionNil: assert false of actionCustom: - if not execHook(options, true): + if not execHook(options, actionCustom, true): display("Warning", "Pre-hook prevented further execution.", Warning, HighPriority) return @@ -1166,7 +1176,7 @@ proc doAction(options: Options) = if isPreDefined: test(options) # Run the post hook for `test` in case it exists. - discard execHook(options, false) + discard execHook(options, actionCustom, false) when isMainModule: var error = "" diff --git a/src/nimblepkg/nimscriptexecutor.nim b/src/nimblepkg/nimscriptexecutor.nim index bf8afd1..122c41c 100644 --- a/src/nimblepkg/nimscriptexecutor.nim +++ b/src/nimblepkg/nimscriptexecutor.nim @@ -6,12 +6,12 @@ import os, strutils, sets import packageparser, common, packageinfo, options, nimscriptwrapper, cli, version -proc execHook*(options: Options, before: bool): bool = +proc execHook*(options: Options, hookAction: ActionType, before: bool): bool = ## Returns whether to continue. result = true # For certain commands hooks should not be evaluated. - if options.action.typ in noHookActions: + if hookAction in noHookActions: return 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. let pkgInfo = getPkgInfoFromFile(nimbleFile, options) let actionName = - if options.action.typ == actionCustom: options.action.command - else: ($options.action.typ)[6 .. ^1] + if hookAction == actionCustom: options.action.command + else: ($hookAction)[6 .. ^1] let hookExists = if before: actionName.normalize in pkgInfo.preHooks else: actionName.normalize in pkgInfo.postHooks @@ -58,7 +58,7 @@ proc execCustom*(options: Options, HighPriority) return - if not execHook(options, false): + if not execHook(options, actionCustom, false): return return true diff --git a/tests/nimscript/nimscript.nimble b/tests/nimscript/nimscript.nimble index f27631a..39f3710 100644 --- a/tests/nimscript/nimscript.nimble +++ b/tests/nimscript/nimscript.nimble @@ -54,3 +54,9 @@ before install: after install: echo("After PkgDir: ", getPkgDir()) + +before build: + echo("Before build") + +after build: + echo("After build") \ No newline at end of file diff --git a/tests/tester.nim b/tests/tester.nim index 19c52cf..21c7011 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -286,12 +286,21 @@ suite "nimscript": cd "nimscript": let (output, exitCode) = execNimble(["install", "-y"]) check exitCode == QuitSuccess + check output.contains("Before build") + check output.contains("After build") let lines = output.strip.processOutput() check lines[0].startsWith("Before PkgDir:") check lines[0].endsWith("tests" / "nimscript") check lines[^1].startsWith("After PkgDir:") 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": cd "nimscript": let (output, exitCode) = execNimble("--verbose", "work")