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:
createDir(changeRoot(realDir, pkgDestDir, file.splitFile.dir))
let dest = changeRoot(realDir, pkgDestDir, file)
filesInstalled.incl copyFileD(file, dest)
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,44 +88,48 @@ 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 dir.len == 0:
if file != pkgInfo.name:
# A source file was found in the top level of srcDir that doesn't share
# a name with the package.
let
msg = ("Package '$1' has an incorrect structure. " &
"The top level of the package source directory " &
"should contain at most one module, " &
"named '$2', but a file named '$3' was found. This " &
"will be an error in the future.") %
[pkgInfo.name, pkgInfo.name & ext, file & ext]
hint = ("If this is the primary source file in the package, " &
"rename it to '$1'. If it's a source file required by " &
"the main module, or if it is one of several " &
"modules exposed by '$4', then move it into a '$2' subdirectory. " &
"If it's a test file or otherwise not required " &
"to build the the package '$1', prevent its installation " &
"by adding `skipFiles = @[\"$3\"]` to the .nimble file. See " &
"https://github.com/nim-lang/nimble#libraries for more info.") %
[pkgInfo.name & ext, correctDir & DirSep, file & ext, pkgInfo.name]
raiseNewValidationError(msg, true, hint, true)
else:
assert(not pkgInfo.isMinimal)
# On Windows `pkgInfo.bin` has a .exe extension, so we need to normalize.
if not (dir.startsWith(correctDir & DirSep) or dir == correctDir):
let
msg = ("Package '$2' has an incorrect structure. " &
"It should contain a single directory hierarchy " &
"for source files, named '$3', but file '$1' " &
"is in a directory named '$4' instead. " &
"This will be an error in the future.") %
[file & ext, pkgInfo.name, correctDir, dir]
hint = ("If '$1' contains source files for building '$2', rename it " &
"to '$3'. Otherwise, prevent its installation " &
"by adding `skipDirs = @[\"$1\"]` to the .nimble file.") %
[dir, pkgInfo.name, correctDir]
raiseNewValidationError(msg, true, hint, true)
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
# a name with the package.
let
msg = ("Package '$1' has an incorrect structure. " &
"The top level of the package source directory " &
"should contain at most one module, " &
"named '$2', but a file named '$3' was found. This " &
"will be an error in the future.") %
[pkgInfo.name, pkgInfo.name & ext, file & ext]
hint = ("If this is the primary source file in the package, " &
"rename it to '$1'. If it's a source file required by " &
"the main module, or if it is one of several " &
"modules exposed by '$4', then move it into a '$2' subdirectory. " &
"If it's a test file or otherwise not required " &
"to build the the package '$1', prevent its installation " &
"by adding `skipFiles = @[\"$3\"]` to the .nimble file. See " &
"https://github.com/nim-lang/nimble#libraries for more info.") %
[pkgInfo.name & ext, correctDir & DirSep, file & ext, pkgInfo.name]
raiseNewValidationError(msg, true, hint, true)
else:
assert(not pkgInfo.isMinimal)
# On Windows `pkgInfo.bin` has a .exe extension, so we need to normalize.
if not (dir.startsWith(correctDir & DirSep) or dir == correctDir):
let
msg = ("Package '$2' has an incorrect structure. " &
"It should contain a single directory hierarchy " &
"for source files, named '$3', but file '$1' " &
"is in a directory named '$4' instead. " &
"This will be an error in the future.") %
[file & ext, pkgInfo.name, correctDir, dir]
hint = ("If '$1' contains source files for building '$2', rename it " &
"to '$3'. Otherwise, prevent its installation " &
"by adding `skipDirs = @[\"$1\"]` to the .nimble file.") %
[dir, pkgInfo.name, correctDir]
raiseNewValidationError(msg, true, hint, true)
iterInstallFiles(realDir, pkgInfo, options, onFile)
proc validatePackageInfo(pkgInfo: PackageInfo, options: Options) =
let path = pkgInfo.myPath