lib.sources.sourceByGlobs: address review comments

This commit is contained in:
zimbatm
2026-05-22 14:14:40 +02:00
committed by adisbladis
parent 59d55cbaa3
commit 01bdc186ae

View File

@@ -521,44 +521,50 @@ let
throw "repoRevToName: invalid kind"; throw "repoRevToName: invalid kind";
/** /**
Filter sources by a list of double star glob patterns. Filter a source tree by a list of doublestar-style glob patterns,
returning a source that only contains paths matching at least one
pattern. `*` matches a single path component, and `**` matches any
number of components.
# Inputs # Inputs
`src` `src`
: 1\. Function argument : The source tree to filter.
`patterns` `patterns`
: 2\. Function argument : List of glob patterns to include, e.g. `[ "*.py" "src/**" ]`.
A leading `**` (e.g. `**\/*.py` for all `.py` files at any depth)
is also supported; the `\` here is just a Nix string escape used
to avoid closing this comment.
# Examples # Examples
:::{.example} :::{.example}
## `sourceByGlobs` usage example ## `sourceByGlobs` usage example
- Include all .py files recursively - Include everything under a subdirectory
```nix ```nix
src = sourceByGlobs ./my-subproject ["**\/*.py" ] src = sourceByGlobs ./. [ "src/**" "tests/**" ]
``` ```
- Include all .py files in root directory only - Include all .py files in root directory only
```nix ```nix
src = sourceByGlobs ./my-subproject ["*.py" ] src = sourceByGlobs ./. [ "*.py" ]
``` ```
::: :::
*/ */
sourceByGlobs = sourceByGlobs =
let let
splitPath = path: filter isString (split "\/" path); splitPath = path: filter isString (split "/" path);
# Make component regex # Make component regex
mkRe = mkRe =
s: s:
if s == "**" then if s == "**" then
".*" # Has special handling below ".*" # Has special handling below
else else
concatStrings (map (tok: if isList tok then "[^\/]*" else escapeRegex tok) (split "\\*+" s)); concatStrings (map (tok: if isList tok then "[^/]*" else escapeRegex tok) (split "\\*+" s));
# Make a source filter function from pattern # Make a source filter function from pattern
mkMatcher = mkMatcher =