Skip to content

Commit

Permalink
Fix thisDir() at compile time (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
genotrance authored Oct 9, 2020
1 parent 26167cd commit 5d9aed1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
36 changes: 17 additions & 19 deletions src/nimblepkg/nimscriptapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,43 @@ var
nimbleTasks: seq[string] = @[]
beforeHooks: seq[string] = @[]
afterHooks: seq[string] = @[]
commandLineParams*: seq[string] = @[]
flags: Table[string, seq[string]]
namedBin*: Table[string, string]

command = "e"
project = ""
success = false
retVal = true
scriptFile = ""
projectFile = ""
outFile = ""
actionName = ""

proc requires*(deps: varargs[string]) =
## Call this to set the list of requirements of your Nimble
## package.
for d in deps: requiresData.add(d)

proc getParams() =
proc getParams(): tuple[scriptFile, projectFile, outFile, actionName: string,
commandLineParams: seq[string]] =
# Called by nimscriptwrapper.nim:execNimscript()
# nim e --flags /full/path/to/file.nims /full/path/to/file.out action
# nim e --flags /full/path/to/file.nims /full/path/to/file.nimble /full/path/to/file.out action
for i in 2 .. paramCount():
let
param = paramStr(i)
if param[0] != '-':
if scriptFile.len == 0:
scriptFile = param
elif projectFile.len == 0:
projectFile = param
elif outFile.len == 0:
outFile = param
elif actionName.len == 0:
actionName = param.normalize
if result.scriptFile.len == 0:
result.scriptFile = param
elif result.projectFile.len == 0:
result.projectFile = param
elif result.outFile.len == 0:
result.outFile = param
elif result.actionName.len == 0:
result.actionName = param.normalize
else:
commandLineParams.add param
result.commandLineParams.add param
else:
commandLineParams.add param
result.commandLineParams.add param

const
# Command line values are const so that thisDir() works at compile time
(scriptFile, projectFile, outFile, actionName, commandLineParams*) = getParams()

proc getCommand*(): string =
return command
Expand Down Expand Up @@ -216,5 +216,3 @@ proc getPkgDir*(): string =
result = projectFile.rsplit(seps={'/', '\\', ':'}, maxsplit=1)[0]
proc thisDir*(): string = getPkgDir()
getParams()
5 changes: 4 additions & 1 deletion tests/nimscript/nimscript.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ bin = @["nimscript"]

requires "nim >= 0.12.1"

when thisDir().len != 0:
echo "thisDirCT: ", thisDir()

task work, "test description":
echo(5+5)

Expand Down Expand Up @@ -60,4 +63,4 @@ before build:
echo("Before build")

after build:
echo("After build")
echo("After build")
6 changes: 3 additions & 3 deletions tests/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ suite "nimscript":
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("Before PkgDir:")
check lines[1].endsWith("tests" / "nimscript")
check lines[^1].startsWith("After PkgDir:")
check lines[^1].endsWith("tests" / "nimbleDir" / "pkgs" / "nimscript-0.1.0")

Expand Down Expand Up @@ -269,6 +269,7 @@ suite "nimscript":
let (output, exitCode) = execNimble("api")
let lines = output.strip.processOutput()
check exitCode == QuitSuccess
check inLines(lines, "thisDirCT: " & getCurrentDir())
check inLines(lines, "PKG_DIR: " & getCurrentDir())
check inLines(lines, "thisDir: " & getCurrentDir())

Expand Down Expand Up @@ -807,7 +808,6 @@ suite "misc tests":
const buildDir = "./buildDir/"
const filesToBuild = [
"../src/nimble.nim",
"../src/nimblepkg/nimscriptapi.nim",
"./tester.nim",
]

Expand Down

0 comments on commit 5d9aed1

Please sign in to comment.