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) 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,
createDir(changeRoot(realDir, pkgDestDir, file.splitFile.dir)) proc (file: string) =
let dest = changeRoot(realDir, pkgDestDir, file) createDir(changeRoot(realDir, pkgDestDir, file.splitFile.dir))
filesInstalled.incl copyFileD(file, dest) let dest = changeRoot(realDir, pkgDestDir, file)
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,

View file

@ -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") ==

View file

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