-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add filterFiles to lib and lib.sources #188301
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,6 +229,57 @@ let | |
|
||
canCleanSource = src: src ? _isLibCleanSourceWith || !(pathHasContext (toString src)); | ||
|
||
/* | ||
Get all files and symlinks that pass a filter function, by scanning | ||
directories recursively, unconditionally. Unlike other source filtering | ||
functions, this will not include empty directories in the final derivation. | ||
|
||
Type: (String -> Bool) -> sourceLike -> Source | ||
|
||
Example: | ||
filterFiles ./. (lib.hasSuffix ".html") | ||
*/ | ||
filterFiles = filter: src: | ||
let | ||
pathToList = path: lib.splitString "/" (builtins.toString path); | ||
dirDepth = builtins.length (pathToList src); | ||
dropCommon = path: lib.drop dirDepth (pathToList path); | ||
|
||
includedPaths = | ||
builtins.filter | ||
filter | ||
(lib.filesystem.listFilesRecursive src); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not actually support all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just tried
in nixpkgs and it worked. |
||
|
||
dirs = | ||
let | ||
f = dir: filter': | ||
builtins.foldl' | ||
(acc: { name, value }: | ||
if value == "directory" then | ||
let recurse = f (dir + "/${name}") filter'; in | ||
if recurse == {} | ||
then acc | ||
else acc // { ${name} = recurse; } | ||
else if filter' name then | ||
acc // { ${name} = value; } | ||
else | ||
acc | ||
) | ||
{} | ||
(lib.mapAttrsToList lib.nameValuePair (builtins.readDir dir)); | ||
in | ||
f src filter; | ||
in | ||
cleanSourceWith { | ||
inherit src; | ||
name = "source"; | ||
|
||
filter = path: type: | ||
if type == "directory" | ||
then lib.hasAttrByPath (dropCommon path) dirs | ||
else filter path; | ||
}; | ||
|
||
# -------------------------------------------------------------------------- # | ||
# Internal functions | ||
# | ||
|
@@ -279,6 +330,8 @@ in { | |
sourceByRegex | ||
sourceFilesBySuffices | ||
|
||
filterFiles | ||
|
||
trace | ||
; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(assuming you're flipping the parameters as suggested below, and we fix
sourceLike
support)