Improvements to #385.

This commit is contained in:
Dominik Picheta 2017-08-12 14:50:32 +01:00
commit 612c084688
3 changed files with 69 additions and 62 deletions

View file

@ -449,10 +449,12 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
createDir(pkgDestDir)
# Copy this package's files based on the preferences specified in PkgInfo.
var filesInstalled = initSet[string]()
discard forEachInstallFile(realDir, pkgInfo, options) do(file: string) -> bool:
iterInstallFiles(realDir, pkgInfo, options,
proc (file: string) =
createDir(changeRoot(realDir, pkgDestDir, file.splitFile.dir))
let dest = changeRoot(realDir, pkgDestDir, file)
filesInstalled.incl copyFileD(file, dest)
)
# Copy the .nimble file.
let dest = changeRoot(pkgInfo.myPath.splitFile.dir, pkgDestDir,

View file

@ -431,30 +431,30 @@ proc checkInstallDir(pkgInfo: PackageInfo,
if thisDir[0] == '.': result = true
if thisDir == "nimcache": result = true
proc forEachFileWithExt(dir: string, pkgInfo: PackageInfo,
action: proc(f: string): bool): bool =
## Runs `action` for each filename of the files that should be copied.
## Stops if `action` returns `true`.
proc iterFilesWithExt(dir: string, pkgInfo: PackageInfo,
action: proc (f: string)) =
## Runs `action` for each filename of the files that have a whitelisted
## file extension.
for kind, path in walkDir(dir):
if kind == pcDir:
if forEachFileWithExt(path, pkgInfo, action): return true
iterFilesWithExt(path, pkgInfo, action)
else:
if path.splitFile.ext[1 .. ^1] in pkgInfo.installExt:
if action(path): return true
action(path)
proc forEachFileInDir(dir: string, action: proc(f: string): bool): bool =
proc iterFilesInDir(dir: string, action: proc (f: string)) =
## Runs `action` for each file in ``dir`` and any
## subdirectories that are in it. Stops if `action` returns `true`.
## subdirectories that are in it.
for kind, path in walkDir(dir):
if kind == pcDir:
if forEachFileInDir(path, action): return true
iterFilesInDir(path, action)
else:
if action(path): return true
action(path)
proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
options: Options, action: proc(f: string): bool): bool =
## Runs `action` for each file within the ``realDir`` that should be installed.
## Stops if `action` returns `true`.
proc iterInstallFiles*(realDir: string, pkgInfo: PackageInfo,
options: Options, action: proc (f: string)) =
## Runs `action` for each file within the ``realDir`` that should be
## installed.
let whitelistMode =
pkgInfo.installDirs.len != 0 or
pkgInfo.installFiles.len != 0 or
@ -467,7 +467,8 @@ proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
continue
else:
raise NimbleQuit(msg: "")
if action(src): return true
action(src)
for dir in pkgInfo.installDirs:
# TODO: Allow skipping files inside dirs?
@ -478,9 +479,9 @@ proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
else:
raise NimbleQuit(msg: "")
if forEachFileInDir(src, action): return true
iterFilesInDir(src, action)
if forEachFileWithExt(realDir, pkgInfo, action): return true
iterFilesWithExt(realDir, pkgInfo, action)
else:
for kind, file in walkDir(realDir):
if kind == pcDir:
@ -488,13 +489,13 @@ proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
if skip: continue
if forEachInstallFile(file, pkgInfo, options, action): return true
iterInstallFiles(file, pkgInfo, options, action)
else:
let skip = pkgInfo.checkInstallFile(realDir, file)
if skip: continue
if action(file): return true
action(file)
when isMainModule:
doAssert getNameVersion("/home/user/.nimble/libs/packagea-0.1") ==

View file

@ -80,7 +80,7 @@ proc validatePackageStructure(pkgInfo: PackageInfo, options: Options) =
else:
pkgInfo.name
discard forEachInstallFile(realDir, pkgInfo, options) do(path: string) -> bool:
proc onFile(path: string) =
# Remove the root to leave only the package subdirectories.
# ~/package-0.1/package/utils.nim -> package/utils.nim.
var trailPath = changeRoot(realDir, "", path)
@ -88,7 +88,9 @@ proc validatePackageStructure(pkgInfo: PackageInfo, options: Options) =
let (dir, file, ext) = trailPath.splitFile
# We're only interested in nim files, because only they can pollute our
# namespace.
if ext == (ExtSep & "nim"):
if ext != (ExtSep & "nim"):
return
if dir.len == 0:
if file != pkgInfo.name:
# A source file was found in the top level of srcDir that doesn't share
@ -127,6 +129,8 @@ proc validatePackageStructure(pkgInfo: PackageInfo, options: Options) =
[dir, pkgInfo.name, correctDir]
raiseNewValidationError(msg, true, hint, true)
iterInstallFiles(realDir, pkgInfo, options, onFile)
proc validatePackageInfo(pkgInfo: PackageInfo, options: Options) =
let path = pkgInfo.myPath
if pkgInfo.name == "":