Improvements to #385.
This commit is contained in:
parent
4050683a9c
commit
612c084688
3 changed files with 69 additions and 62 deletions
|
|
@ -449,10 +449,12 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
|
||||||
createDir(pkgDestDir)
|
createDir(pkgDestDir)
|
||||||
# Copy this package's files based on the preferences specified in PkgInfo.
|
# Copy this package's files based on the preferences specified in PkgInfo.
|
||||||
var filesInstalled = initSet[string]()
|
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))
|
createDir(changeRoot(realDir, pkgDestDir, file.splitFile.dir))
|
||||||
let dest = changeRoot(realDir, pkgDestDir, file)
|
let dest = changeRoot(realDir, pkgDestDir, file)
|
||||||
filesInstalled.incl copyFileD(file, dest)
|
filesInstalled.incl copyFileD(file, dest)
|
||||||
|
)
|
||||||
|
|
||||||
# Copy the .nimble file.
|
# Copy the .nimble file.
|
||||||
let dest = changeRoot(pkgInfo.myPath.splitFile.dir, pkgDestDir,
|
let dest = changeRoot(pkgInfo.myPath.splitFile.dir, pkgDestDir,
|
||||||
|
|
|
||||||
|
|
@ -431,30 +431,30 @@ proc checkInstallDir(pkgInfo: PackageInfo,
|
||||||
if thisDir[0] == '.': result = true
|
if thisDir[0] == '.': result = true
|
||||||
if thisDir == "nimcache": result = true
|
if thisDir == "nimcache": result = true
|
||||||
|
|
||||||
proc forEachFileWithExt(dir: string, pkgInfo: PackageInfo,
|
proc iterFilesWithExt(dir: string, pkgInfo: PackageInfo,
|
||||||
action: proc(f: string): bool): bool =
|
action: proc (f: string)) =
|
||||||
## Runs `action` for each filename of the files that should be copied.
|
## Runs `action` for each filename of the files that have a whitelisted
|
||||||
## Stops if `action` returns `true`.
|
## file extension.
|
||||||
for kind, path in walkDir(dir):
|
for kind, path in walkDir(dir):
|
||||||
if kind == pcDir:
|
if kind == pcDir:
|
||||||
if forEachFileWithExt(path, pkgInfo, action): return true
|
iterFilesWithExt(path, pkgInfo, action)
|
||||||
else:
|
else:
|
||||||
if path.splitFile.ext[1 .. ^1] in pkgInfo.installExt:
|
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
|
## 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):
|
for kind, path in walkDir(dir):
|
||||||
if kind == pcDir:
|
if kind == pcDir:
|
||||||
if forEachFileInDir(path, action): return true
|
iterFilesInDir(path, action)
|
||||||
else:
|
else:
|
||||||
if action(path): return true
|
action(path)
|
||||||
|
|
||||||
proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
|
proc iterInstallFiles*(realDir: string, pkgInfo: PackageInfo,
|
||||||
options: Options, action: proc(f: string): bool): bool =
|
options: Options, action: proc (f: string)) =
|
||||||
## Runs `action` for each file within the ``realDir`` that should be installed.
|
## Runs `action` for each file within the ``realDir`` that should be
|
||||||
## Stops if `action` returns `true`.
|
## installed.
|
||||||
let whitelistMode =
|
let whitelistMode =
|
||||||
pkgInfo.installDirs.len != 0 or
|
pkgInfo.installDirs.len != 0 or
|
||||||
pkgInfo.installFiles.len != 0 or
|
pkgInfo.installFiles.len != 0 or
|
||||||
|
|
@ -467,7 +467,8 @@ proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raise NimbleQuit(msg: "")
|
raise NimbleQuit(msg: "")
|
||||||
if action(src): return true
|
|
||||||
|
action(src)
|
||||||
|
|
||||||
for dir in pkgInfo.installDirs:
|
for dir in pkgInfo.installDirs:
|
||||||
# TODO: Allow skipping files inside dirs?
|
# TODO: Allow skipping files inside dirs?
|
||||||
|
|
@ -478,9 +479,9 @@ proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
|
||||||
else:
|
else:
|
||||||
raise NimbleQuit(msg: "")
|
raise NimbleQuit(msg: "")
|
||||||
|
|
||||||
if forEachFileInDir(src, action): return true
|
iterFilesInDir(src, action)
|
||||||
|
|
||||||
if forEachFileWithExt(realDir, pkgInfo, action): return true
|
iterFilesWithExt(realDir, pkgInfo, action)
|
||||||
else:
|
else:
|
||||||
for kind, file in walkDir(realDir):
|
for kind, file in walkDir(realDir):
|
||||||
if kind == pcDir:
|
if kind == pcDir:
|
||||||
|
|
@ -488,13 +489,13 @@ proc forEachInstallFile*(realDir: string, pkgInfo: PackageInfo,
|
||||||
|
|
||||||
if skip: continue
|
if skip: continue
|
||||||
|
|
||||||
if forEachInstallFile(file, pkgInfo, options, action): return true
|
iterInstallFiles(file, pkgInfo, options, action)
|
||||||
else:
|
else:
|
||||||
let skip = pkgInfo.checkInstallFile(realDir, file)
|
let skip = pkgInfo.checkInstallFile(realDir, file)
|
||||||
|
|
||||||
if skip: continue
|
if skip: continue
|
||||||
|
|
||||||
if action(file): return true
|
action(file)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
doAssert getNameVersion("/home/user/.nimble/libs/packagea-0.1") ==
|
doAssert getNameVersion("/home/user/.nimble/libs/packagea-0.1") ==
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ proc validatePackageStructure(pkgInfo: PackageInfo, options: Options) =
|
||||||
else:
|
else:
|
||||||
pkgInfo.name
|
pkgInfo.name
|
||||||
|
|
||||||
discard forEachInstallFile(realDir, pkgInfo, options) do(path: string) -> bool:
|
proc onFile(path: string) =
|
||||||
# Remove the root to leave only the package subdirectories.
|
# Remove the root to leave only the package subdirectories.
|
||||||
# ~/package-0.1/package/utils.nim -> package/utils.nim.
|
# ~/package-0.1/package/utils.nim -> package/utils.nim.
|
||||||
var trailPath = changeRoot(realDir, "", path)
|
var trailPath = changeRoot(realDir, "", path)
|
||||||
|
|
@ -88,7 +88,9 @@ proc validatePackageStructure(pkgInfo: PackageInfo, options: Options) =
|
||||||
let (dir, file, ext) = trailPath.splitFile
|
let (dir, file, ext) = trailPath.splitFile
|
||||||
# We're only interested in nim files, because only they can pollute our
|
# We're only interested in nim files, because only they can pollute our
|
||||||
# namespace.
|
# namespace.
|
||||||
if ext == (ExtSep & "nim"):
|
if ext != (ExtSep & "nim"):
|
||||||
|
return
|
||||||
|
|
||||||
if dir.len == 0:
|
if dir.len == 0:
|
||||||
if file != pkgInfo.name:
|
if file != pkgInfo.name:
|
||||||
# A source file was found in the top level of srcDir that doesn't share
|
# 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]
|
[dir, pkgInfo.name, correctDir]
|
||||||
raiseNewValidationError(msg, true, hint, true)
|
raiseNewValidationError(msg, true, hint, true)
|
||||||
|
|
||||||
|
iterInstallFiles(realDir, pkgInfo, options, onFile)
|
||||||
|
|
||||||
proc validatePackageInfo(pkgInfo: PackageInfo, options: Options) =
|
proc validatePackageInfo(pkgInfo: PackageInfo, options: Options) =
|
||||||
let path = pkgInfo.myPath
|
let path = pkgInfo.myPath
|
||||||
if pkgInfo.name == "":
|
if pkgInfo.name == "":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue