lib.fileset.fileFilter: Predicate attribute for file extension
This commit is contained in:
parent
3d480d4ae7
commit
6816f28c96
3 changed files with 46 additions and 3 deletions
|
@ -573,6 +573,7 @@ in {
|
||||||
({
|
({
|
||||||
name :: String,
|
name :: String,
|
||||||
type :: String,
|
type :: String,
|
||||||
|
hasExt :: String -> Bool,
|
||||||
...
|
...
|
||||||
} -> Bool)
|
} -> Bool)
|
||||||
-> Path
|
-> Path
|
||||||
|
@ -583,7 +584,7 @@ in {
|
||||||
fileFilter (file: file.name == "default.nix") ./.
|
fileFilter (file: file.name == "default.nix") ./.
|
||||||
|
|
||||||
# Include all non-Nix files from the current directory
|
# Include all non-Nix files from the current directory
|
||||||
fileFilter (file: ! hasSuffix ".nix" file.name) ./.
|
fileFilter (file: ! file.hasExt "nix") ./.
|
||||||
|
|
||||||
# Include all files that start with a "." in the current directory
|
# Include all files that start with a "." in the current directory
|
||||||
fileFilter (file: hasPrefix "." file.name) ./.
|
fileFilter (file: hasPrefix "." file.name) ./.
|
||||||
|
@ -603,6 +604,12 @@ in {
|
||||||
- `type` (String, one of `"regular"`, `"symlink"` or `"unknown"`): The type of the file.
|
- `type` (String, one of `"regular"`, `"symlink"` or `"unknown"`): The type of the file.
|
||||||
This matches result of calling [`builtins.readFileType`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-readFileType) on the file's path.
|
This matches result of calling [`builtins.readFileType`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-readFileType) on the file's path.
|
||||||
|
|
||||||
|
- `hasExt` (String -> Bool): Whether the file has a certain file extension.
|
||||||
|
`hasExt ext` is true only if `hasSuffix ".${ext}" name`.
|
||||||
|
|
||||||
|
This also means that e.g. for a file with name `.gitignore`,
|
||||||
|
`hasExt "gitignore"` is true.
|
||||||
|
|
||||||
Other attributes may be added in the future.
|
Other attributes may be added in the future.
|
||||||
*/
|
*/
|
||||||
predicate:
|
predicate:
|
||||||
|
|
|
@ -52,6 +52,7 @@ let
|
||||||
concatStringsSep
|
concatStringsSep
|
||||||
substring
|
substring
|
||||||
stringLength
|
stringLength
|
||||||
|
hasSuffix
|
||||||
;
|
;
|
||||||
|
|
||||||
in
|
in
|
||||||
|
@ -796,9 +797,11 @@ rec {
|
||||||
if
|
if
|
||||||
predicate {
|
predicate {
|
||||||
inherit name type;
|
inherit name type;
|
||||||
|
hasExt = ext: hasSuffix ".${ext}" name;
|
||||||
|
|
||||||
# To ensure forwards compatibility with more arguments being added in the future,
|
# To ensure forwards compatibility with more arguments being added in the future,
|
||||||
# adding an attribute which can't be deconstructed :)
|
# adding an attribute which can't be deconstructed :)
|
||||||
"lib.fileset.fileFilter: The predicate function passed as the first argument must be able to handle extra attributes for future compatibility. If you're using `{ name, file }:`, use `{ name, file, ... }:` instead." = null;
|
"lib.fileset.fileFilter: The predicate function passed as the first argument must be able to handle extra attributes for future compatibility. If you're using `{ name, file, hasExt }:`, use `{ name, file, hasExt, ... }:` instead." = null;
|
||||||
}
|
}
|
||||||
then
|
then
|
||||||
type
|
type
|
||||||
|
|
|
@ -846,7 +846,7 @@ checkFileset 'fileFilter (file: abort "this is not needed") ./.'
|
||||||
|
|
||||||
# The predicate must be able to handle extra attributes
|
# The predicate must be able to handle extra attributes
|
||||||
touch a
|
touch a
|
||||||
expectFailure 'toSource { root = ./.; fileset = fileFilter ({ name, type }: true) ./.; }' 'called with unexpected argument '\''"lib.fileset.fileFilter: The predicate function passed as the first argument must be able to handle extra attributes for future compatibility. If you'\''re using `\{ name, file \}:`, use `\{ name, file, ... \}:` instead."'\'
|
expectFailure 'toSource { root = ./.; fileset = fileFilter ({ name, type, hasExt }: true) ./.; }' 'called with unexpected argument '\''"lib.fileset.fileFilter: The predicate function passed as the first argument must be able to handle extra attributes for future compatibility. If you'\''re using `\{ name, file, hasExt \}:`, use `\{ name, file, hasExt, ... \}:` instead."'\'
|
||||||
rm -rf -- *
|
rm -rf -- *
|
||||||
|
|
||||||
# .name is the name, and it works correctly, even recursively
|
# .name is the name, and it works correctly, even recursively
|
||||||
|
@ -894,6 +894,39 @@ expectEqual \
|
||||||
'toSource { root = ./.; fileset = union ./d/a ./d/b; }'
|
'toSource { root = ./.; fileset = union ./d/a ./d/b; }'
|
||||||
rm -rf -- *
|
rm -rf -- *
|
||||||
|
|
||||||
|
# Check that .hasExt checks for the file extension
|
||||||
|
# The empty extension is the same as a file ending with a .
|
||||||
|
tree=(
|
||||||
|
[a]=0
|
||||||
|
[a.]=1
|
||||||
|
[a.b]=0
|
||||||
|
[a.b.]=1
|
||||||
|
[a.b.c]=0
|
||||||
|
)
|
||||||
|
checkFileset 'fileFilter (file: file.hasExt "") ./.'
|
||||||
|
|
||||||
|
# It can check for the last extension
|
||||||
|
tree=(
|
||||||
|
[a]=0
|
||||||
|
[.a]=1
|
||||||
|
[.a.]=0
|
||||||
|
[.b.a]=1
|
||||||
|
[.b.a.]=0
|
||||||
|
)
|
||||||
|
checkFileset 'fileFilter (file: file.hasExt "a") ./.'
|
||||||
|
|
||||||
|
# It can check for any extension
|
||||||
|
tree=(
|
||||||
|
[a.b.c.d]=1
|
||||||
|
)
|
||||||
|
checkFileset 'fileFilter (file:
|
||||||
|
all file.hasExt [
|
||||||
|
"b.c.d"
|
||||||
|
"c.d"
|
||||||
|
"d"
|
||||||
|
]
|
||||||
|
) ./.'
|
||||||
|
|
||||||
# It's lazy
|
# It's lazy
|
||||||
tree=(
|
tree=(
|
||||||
[b]=1
|
[b]=1
|
||||||
|
|
Loading…
Reference in a new issue