Skip to content

Commit

Permalink
add os.getCurrentPkgDir to get closest enclosing nimble pkg dir [back…
Browse files Browse the repository at this point in the history
…port]
  • Loading branch information
timotheecour committed Feb 3, 2019
1 parent 2b35a51 commit 42719b5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,16 @@ proc setLastModificationTime*(file: string, t: times.Time) {.noNimScript.} =
discard h.closeHandle
if res == 0'i32: raiseOSError(osLastError())

proc getCurrentPkgDirImpl(dir: string): string =
for dir2 in parentDirs(dir, fromRoot = false, inclusive = false):
for kind, path in walkDir(dir2, relative = true):
if kind == pcFile and path.endsWith ".nimble":
when defined(nimscript): return dir2
else: return normalizedPath dir2

template getCurrentPkgDir*(dir = instantiationInfo(-1, true).filename): string =
## Returns the path to the closest enclosing nimble package dir
getCurrentPkgDirImpl(dir)

when isMainModule:
assert quoteShellWindows("aaa") == "aaa"
Expand Down
2 changes: 2 additions & 0 deletions tests/stdlib/nimblepkg/baz/tbaz.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
block getCurrentPkgDir:
static: doAssert getCurrentPkgDir() == currentSourcePath.parentDir.parentDir
1 change: 1 addition & 0 deletions tests/stdlib/nimblepkg/foo.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# for tbaz.nim
7 changes: 7 additions & 0 deletions tests/stdlib/tos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Raises
# test os path creation, iteration, and deletion

import os, strutils, pathnorm
import "$nim/compiler/unittest_light"

block fileOperations:
let files = @["these.txt", "are.x", "testing.r", "files.q"]
Expand Down Expand Up @@ -333,3 +334,9 @@ block ospaths:
doAssert joinPath("", "lib") == "lib"
doAssert joinPath("", "/lib") == unixToNativePath"/lib"
doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib"

block getCurrentPkgDir:
const dir = currentSourcePath.parentDir / "nimblepkg"
static:
assertEquals getCurrentPkgDir(dir / "baz/tbaz.nim"), dir
assertEquals getCurrentPkgDir(dir / "baz"), dir

0 comments on commit 42719b5

Please sign in to comment.