Improves pre and post hooks. Fixes #524.

This commit is contained in:
Dominik Picheta 2018-08-23 00:33:06 +01:00
commit db222bbae1
4 changed files with 107 additions and 76 deletions

View file

@ -330,6 +330,13 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
## to the packages this package depends on.
## The return value of this function is used by
## ``processDeps`` to gather a list of paths to pass to the nim compiler.
# Handle pre-`install` hook.
if not options.depsOnly:
cd dir: # Make sure `execHook` executes the correct .nimble file.
if not execHook(options, true):
raise newException(NimbleError, "Pre-hook prevented further execution.")
var pkgInfo = getPkgInfo(dir, options)
let realDir = pkgInfo.getRealDir()
let binDir = options.getBinDir()
@ -433,6 +440,12 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
display("Success:", pkgInfo.name & " installed successfully.",
Success, HighPriority)
# Run post-install hook now that package is installed. The `execHook` proc
# 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)
proc getDownloadInfo*(pv: PkgTuple, options: Options,
doPrompt: bool): (DownloadMethod, string,
Table[string, string]) =
@ -472,17 +485,7 @@ proc install(packages: seq[PkgTuple],
let (downloadDir, downloadVersion) =
downloadPkg(url, pv.ver, meth, subdir, options)
try:
# Run pre-install hook in download directory now that package is downloaded
cd downloadDir:
if not execHook(options, true):
raise newException(NimbleError, "Pre-hook prevented further execution.")
result = installFromDir(downloadDir, pv.ver, options, url)
# Run post-install hook in installed directory now that package is installed
# Standard hooks run in current directory so it won't detect this new package
cd result.pkg.myPath.parentDir():
discard execHook(options, false)
except BuildFailed:
# The package failed to build.
# Check if we tried building a tagged version of the package.
@ -945,6 +948,10 @@ proc developFromDir(dir: string, options: Options) =
if options.depsOnly:
raiseNimbleError("Cannot develop dependencies only.")
cd dir: # Make sure `execHook` executes the correct .nimble file.
if not execHook(options, true):
raise newException(NimbleError, "Pre-hook prevented further execution.")
var pkgInfo = getPkgInfo(dir, options)
if pkgInfo.bin.len > 0:
if "nim" in pkgInfo.skipExt:
@ -994,6 +1001,10 @@ proc developFromDir(dir: string, options: Options) =
display("Success:", (pkgInfo.name & " linked successfully to '$1'.") %
dir, Success, HighPriority)
# Execute the post-develop hook.
cd dir:
discard execHook(options, false)
proc develop(options: Options) =
if options.action.packages == @[]:
developFromDir(getCurrentDir(), options)
@ -1064,10 +1075,6 @@ proc doAction(options: Options) =
if not existsDir(options.getPkgsDir):
createDir(options.getPkgsDir)
if not execHook(options, true):
display("Warning", "Pre-hook prevented further execution.", Warning,
HighPriority)
return
case options.action.typ
of actionRefresh:
refresh(options)
@ -1111,6 +1118,10 @@ proc doAction(options: Options) =
of actionNil:
assert false
of actionCustom:
if not execHook(options, true):
display("Warning", "Pre-hook prevented further execution.", Warning,
HighPriority)
return
let isPreDefined = options.action.command.normalize == "test"
var execResult: ExecutionResult[void]
@ -1125,9 +1136,6 @@ proc doAction(options: Options) =
# Run the post hook for `test` in case it exists.
discard execHook(options, false)
if options.action.typ != actionCustom:
discard execHook(options, false)
when isMainModule:
var error = ""
var hint = ""

View file

@ -362,7 +362,7 @@ proc execScript(scriptName: string, flags: Flags, options: Options): PSym =
# Ensure that "nimblepkg/nimscriptapi" is in the PATH.
block:
let t = options.getNimbleDir / "nimblecache"
let t = getTempDir() / "nimblecache"
let tmpNimscriptApiPath = t / "nimblepkg" / "nimscriptapi.nim"
createDir(tmpNimscriptApiPath.splitFile.dir)
writeFile(tmpNimscriptApiPath, nimscriptApi)

View file

@ -43,3 +43,9 @@ before hooks2:
task hooks2, "Testing the hooks again":
echo("Shouldn't happen")
before install:
echo("Before PkgDir: ", getPkgDir())
after install:
echo("After PkgDir: ", getPkgDir())

View file

@ -36,7 +36,10 @@ proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] =
quotedArgs.add("--nimbleDir:" & installDir)
quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\""))
result = execCmdEx(quotedArgs.join(" "))
let path = getCurrentDir().parentDir() / "src"
let cmd = "PATH=" & path & ":$PATH " & quotedArgs.join(" ")
result = execCmdEx(cmd)
checkpoint(result.output)
proc execNimbleYes(args: varargs[string]): tuple[output: string, exitCode: int]=
@ -235,71 +238,82 @@ test "package list can only have one source":
check inLines(lines, "Attempted to specify `url` and `path` for the same package list 'local'")
check exitCode == QuitFailure
test "can install nimscript package":
cd "nimscript":
check execNimble(["install", "-y"]).exitCode == QuitSuccess
suite "nimscript":
test "can install nimscript package":
cd "nimscript":
check execNimble(["install", "-y"]).exitCode == QuitSuccess
test "can execute nimscript tasks":
cd "nimscript":
let (output, exitCode) = execNimble("--verbose", "work")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check lines[^1] == "10"
test "before/after install pkg dirs are correct":
cd "nimscript":
let (output, exitCode) = execNimble(["install", "-y"])
check exitCode == QuitSuccess
let lines = output.strip.splitLines()
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 "can use nimscript's setCommand":
cd "nimscript":
let (output, exitCode) = execNimble("--verbose", "cTest")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check "Execution finished".normalize in lines[^1].normalize
test "can execute nimscript tasks":
cd "nimscript":
let (output, exitCode) = execNimble("--verbose", "work")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check lines[^1] == "10"
test "can use nimscript's setCommand with flags":
cd "nimscript":
let (output, exitCode) = execNimble("--debug", "cr")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check inLines(lines, "Hello World")
test "can use nimscript's setCommand":
cd "nimscript":
let (output, exitCode) = execNimble("--verbose", "cTest")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check "Execution finished".normalize in lines[^1].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 use nimscript's setCommand with flags":
cd "nimscript":
let (output, exitCode) = execNimble("--debug", "cr")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check inLines(lines, "Hello World")
test "can list nimscript tasks":
cd "nimscript":
let (output, exitCode) = execNimble("tasks")
check "work test description".normalize in output.normalize
check exitCode == QuitSuccess
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 use pre/post hooks":
cd "nimscript":
let (output, exitCode) = execNimble("hooks")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check inLines(lines, "First")
check inLines(lines, "middle")
check inLines(lines, "last")
test "can list nimscript tasks":
cd "nimscript":
let (output, exitCode) = execNimble("tasks")
check "work test description".normalize in output.normalize
check exitCode == QuitSuccess
test "pre hook can prevent action":
cd "nimscript":
let (output, exitCode) = execNimble("hooks2")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check(not inLines(lines, "Shouldn't happen"))
check inLines(lines, "Hook prevented further execution")
test "can use pre/post hooks":
cd "nimscript":
let (output, exitCode) = execNimble("hooks")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check inLines(lines, "First")
check inLines(lines, "middle")
check inLines(lines, "last")
test "nimble script api":
cd "nimscript":
let (output, exitCode) = execNimble("api")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check inLines(lines, "PKG_DIR: " & getCurrentDir())
test "pre hook can prevent action":
cd "nimscript":
let (output, exitCode) = execNimble("hooks2")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check(not inLines(lines, "Shouldn't happen"))
check inLines(lines, "Hook prevented further execution")
test "nimble script api":
cd "nimscript":
let (output, exitCode) = execNimble("api")
let lines = output.strip.splitLines()
check exitCode == QuitSuccess
check inLines(lines, "PKG_DIR: " & getCurrentDir())
test "can install packagebin2":
let args = ["install", "-y", "https://github.com/nimble-test/packagebin2.git"]
@ -491,6 +505,9 @@ test "can install diamond deps (#184)":
checkpoint(output)
check exitCode == 0
test "issues #280 and #524":
check execNimble("install", "-y", "https://github.com/nimble-test/issue280and524.git").exitCode == 0
suite "can handle two binary versions":
setup:
cd "binaryPackage/v1":